2

目標/背景

  • 作業指示書が特定の基準を満たしているかどうかを確認するために、作業指示書システムを調べています
    • (たとえば、ステージ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;
;

問題/質問

  • これは機能しているように見えますが、notstage1、notstage2、およびnotstage1のすべてのカウントは、いくつかの状況を照会し、私が異なることがわかっている状況を見つけたにもかかわらず、同じ結果になります。
  • これは、カウントしたいcaseステートメントを実装する正しい方法ですか?
  • 代わりにDECODE()を使用する必要がありますか?

助けてくれてありがとう!

4

4 に答える 4

9

1と0はどちらも同じCOUNT()です。SUM()、またはCOUNT()を1またはnullにする必要がある場合があります。

于 2013-01-08T13:50:16.533 に答える
0

カウントは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
于 2013-01-08T13:49:47.090 に答える
0

を使用しないでくださいdecode

sum()クエリを作成する方法ではなく、本当に必要ですcount()

select yearreported, theleadcraft, sum(NotStage3), sum(NotStage2), sum(NotStage1)

この関数count()を列に適用すると、誤解を招くような名前が付けられます(私の意見では)。NULL以外の値の数をカウントしています。「1」と「0」はどちらもNULL以外であるため、カウントされます。

于 2013-01-08T14:10:54.567 に答える
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;

よろしく、

于 2013-01-08T16:30:16.937 に答える