pivot函数返回所有零

dbf7pr2w  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(349)

我有一张如下的table:

TestCaseID  |  currentResultVerdict
    1                Passed
    2                Failed
    3                Blocked
    4                Blocked
    5                Inprogress
    6                Passed
    7                Passed

我想产生这样的结果:

Passed | Failed | Blocked | Inprogress
  3         1        2          1

我曾经 PIVOT 查询如下:

select  *
from
(select TestCaseID, currentResultVerdict from R_4_TestCases) As SourceTable
PIVOT
(
count(TestCaseID)
for currentResultVerdict in (['Passed'],['Failed'],['Blocked'],['Inprogress'])
) As PivotTabe

我的输出是:

'Passed' | 'Failed' | 'Blocked' | 'Inprogress'
  0           0          0            0

为什么我得到零?
为了便于阅读,我的表中没有其他列

nbysray5

nbysray51#

作为替代 pivot 使用条件聚合可以获得相同的结果:

declare @tmp table (TestCaseID  int,  currentResultVerdict varchar(50))
insert into @tmp
values
 (1, 'Passed')
,(2, 'Failed')
,(3, 'Blocked')
,(4, 'Blocked')
,(5, 'Inprogress')
,(6, 'Passed')
,(7, 'Passed')

select 
     sum (case when currentResultVerdict='Passed'     then 1 else 0 end) as Passed
    ,sum (case when currentResultVerdict='Failed'     then 1 else 0 end) as Failed
    ,sum (case when currentResultVerdict='Blocked'    then 1 else 0 end) as Blocked
    ,sum (case when currentResultVerdict='Inprogress' then 1 else 0 end) as Inprogress
from @tmp

结果:

相关问题