数据透视功能添加的行比预期的多

时间:2018-09-09 20:30:18

标签: sql sql-server tsql pivot

我想不出更好的头衔,如果有人能想到一个更好的头衔,我会提出建议。

我有一个看起来像这样的表:

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

当我运行此查询时:

select [a], [b], [c], [d], [e]
from (select [source], [rowcount] from ParentStaged) as src_tbl
pivot
(
    avg([rowcount])
    for source in ([a], [b], [c], [d], [e])
) as pivot_tbl;

我得到这个结果:

  a       b        c      d       e
500     750     1000    200     200

当我运行此查询时:

select [a], [b], [c], [d], [e]
from (select [source], [TableName], [rowcount] from ParentStaged) as src_tbl
pivot
(
    avg([rowcount])
    for source in ([a], [b], [c], [d], [e])
) as pivot_tbl;

我得出这个结果:

   a        b        c       d       e
 500     null     null    null    null
null      750     null    null    null
null     null     1000    null    null
null     null     null     200    null
null     null     null    null     200

这是为什么?

0 个答案:

没有答案