select yearreported
, theleadcraft
, count(NotStage3)
, count(NotStage2)
, count(NotStage1)
from
(
select extract(year from reportdate) as YearReported
, Nvl(leadcraft, 'NONE') as TheLeadCraft
, CASE when status not in ('CAN', 'CLOSE') then 1 else 0 END as NotStage3
, CASE when status not in ('CAN', 'CLOSE', 'COMP') then 1 else 0 END as NotStage2
, CASE when status not in ('CAN', 'CLOSE', 'COMP', 'WORKDONE') then 1 else 0 END as NotStage1
from workorder
) query
group by yearreported, theleadcraft;
;
提前感谢您的帮助!
答案 0 :(得分:9)
1和0都COUNT()相同 - 可能你想要SUM(),或COUNT()1或null。
答案 1 :(得分:0)
Count不计算NULL -s。试试这个:
, CASE when status not in ('CAN', 'CLOSE') then 1 END as NotStage3
, CASE when status not in ('CAN', 'CLOSE', 'COMP') then 1 END as NotStage2
, CASE when status not in ('CAN', 'CLOSE', 'COMP', 'WORKDONE') then 1 END as NotStage1
答案 2 :(得分:0)
您不应该使用decode
。
您编写查询的方式,您真的需要sum()
,而不是count()
:
select yearreported, theleadcraft, sum(NotStage3), sum(NotStage2), sum(NotStage1)
函数count()
在应用于列时具有误导性名称(在我看来)。它计算非NULL值的数量。由于“1”和“0”都是非NULL,因此它们会被计算在内。
答案 3 :(得分:0)
是的,您可以通过上述声明中的简单修改来实现
试试这个:
select yearreported
, theleadcraft
, count(decode (NotStage3, 1,1) )
, count(decode (NotStage2, 1,1) )
, count(decode (NotStage1, 1,1) )
from
(
select extract(year from reportdate) as YearReported
, Nvl(leadcraft, 'NONE') as TheLeadCraft
, CASE when status not in ('CAN', 'CLOSE') then 1 else 0 END as NotStage3
, CASE when status not in ('CAN', 'CLOSE', 'COMP') then 1 else 0 END as NotStage2
, CASE when status not in ('CAN', 'CLOSE', 'COMP', 'WORKDONE') then 1 else 0 END as NotStage1
from workorder
) query
group by yearreported, theleadcraft;
的问候,