我有一个看起来像这样的表:
Source TableName Detail active_status RowCount
a table_one stuff active 500
b table_two stuff_2 active 750
c table_three stuff_3 inactive 1000
d table_four stuff_4 active 200
e table_five stuff_5 inactive 200
我想要的是这个结果:
result a b c d e
result 500 750 1000 200 2000
这是我正在使用的SQL查询:
select 'results' as results, ['a'], ['b'], ['c'], ['d'], ['e']
from (select [source], [rowcount] from ParentStaged) as src_tbl
pivot
(
sum([rowcount])
for source in (['a'], ['b'], ['c'], ['d'], ['e'])
) as pivot_tbl;
这将产生此结果:
results a b c d e
results null null null null null
我在做什么错了?
答案 0 :(得分:3)
您需要指定用作标题的列值,而在数据透视表中没有引号,
例如:
select 'results' as results, a, b, c, d, e
from (select [source], rowcnt from ParentStaged) as src_tbl
pivot
(
sum(rowcnt)
for source in (a, b, c, d, e)
) as pivot_tbl;
OR
select 'results' as results, [a], [b], [c], [d], [e]
from (select [source], rowcnt from ParentStaged) as src_tbl
pivot
(
sum(rowcnt)
for source in ([a], [b], [c], [d], [e])
) as pivot_tbl;