Sql 对列和行执行操作
我有两张桌子 1) 纵队 2) 划船 最终输出应如下所示:Sql 对列和行执行操作,sql,pivot,unpivot,Sql,Pivot,Unpivot,我有两张桌子 1) 纵队 2) 划船 最终输出应如下所示: 01.Row 1 2 3 4 5 6 7 8 9 02.----------- ---- ---- ---- ---- ---- ---- ---- ---- ---- 03.10 x x x 04.20 x x x x
01.Row 1 2 3 4 5 6 7 8 9
02.----------- ---- ---- ---- ---- ---- ---- ---- ---- ----
03.10 x x x
04.20 x x x x
05.77 x x
06.99 x x x
07.100 x x x x
08.101 x
09.104 x x x x
挑战在于,当且仅当行值可被col值整除时,用X值标记坐标,即它的模为零。附加要求是:最终查询必须使用随机行值,并且应使用pivot运算符。以下内容应满足您在sql server中的要求。 使用CTE确定x的位置,然后从该CTE旋转
with mq as(select a.rowid
,b.columnid
,case when (a.rowid % b.columnid) = 0 then 'X' else null end as coord
from row_table a
inner join column_table b on 1=1)
select rowid,[1], [2], [3], [4],[5], [6], [7], [8], [9]
from mq
pivot( max(coord) for columnid in ([1], [2], [3], [4],[5], [6], [7], [8], [9])) as pv
这是家庭作业问题吗?看起来很眼熟。
with mq as(select a.rowid
,b.columnid
,case when (a.rowid % b.columnid) = 0 then 'X' else null end as coord
from row_table a
inner join column_table b on 1=1)
select rowid,[1], [2], [3], [4],[5], [6], [7], [8], [9]
from mq
pivot( max(coord) for columnid in ([1], [2], [3], [4],[5], [6], [7], [8], [9])) as pv