SQL Server中多列上的透视表
我有一张表,上面显示了这样的数据 我需要这种格式的数据SQL Server中多列上的透视表,sql,sql-server,Sql,Sql Server,我有一张表,上面显示了这样的数据 我需要这种格式的数据 GIN APINV AR Rec Requisitions Total 8 11 77 Pending 7 6 77 New 1 77 0 Approved 0 5
GIN APINV AR Rec Requisitions
Total 8 11 77
Pending 7 6 77
New 1 77 0
Approved 0 5 0
Rejected 1 0 0
等等
我已经知道的是我必须使用
PIVOT
,但我只使用了一列PIVOT
。交叉应用和PIVOT
的组合应该可以:
select *
from (
select t.appname, x.status, x.val
from your_table t
cross apply (
values
('Total', t.total),
('Pending', t.pending),
('New', t.New),
('Approved', t.Approved),
('Rejected', t.Rejected)
) x (status, val)
) t pivot (
sum(val)
for appname in (
[GIN],[APINV],....
)
);
交叉应用
和枢轴
的组合应能:
select *
from (
select t.appname, x.status, x.val
from your_table t
cross apply (
values
('Total', t.total),
('Pending', t.pending),
('New', t.New),
('Approved', t.Approved),
('Rejected', t.Rejected)
) x (status, val)
) t pivot (
sum(val)
for appname in (
[GIN],[APINV],....
)
);
您预期输出中的列数是固定的,还是会变化/未知?@TimBiegeleisen请关注此链接,它也在工作。您预期输出中的列数是固定的,还是会变化/未知?@TimBiegeleisen请关注此链接,它也在工作。