Oracle - 计算CASE语句的结果

Sea*_*een 2 sql oracle plsql case count

目标/背景

  • 我正在梳理一个工作订单系统,看看工人是否符合某些标准
    • (例如,如果他们不是第3阶段,第2阶段,甚至第1阶段).
  • 这些"阶段"已由管理层定义.
  • 我想按照报告的年份,然后是工艺分组,然后看看在这3个"非阶段"的每一个中,该分组有多少工单.

查询

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;
;
Run Code Online (Sandbox Code Playgroud)

问题/问题

  • 这似乎有效,但是对于notstage1,notstage2和notstage1的所有计数都是相同的,尽管查询某些情况并找到一些我知道不同的情况.
  • 这是实现我想要计算的案例陈述的正确方法吗?
  • 我应该使用DECODE()吗?

在此先感谢您的帮助!

Dav*_*dge 9

1和0都是COUNT()相同 - 可能你想要SUM(),或COUNT()1或null.