2017-07-01 5 views
0

特定のタスクの値が変更されていなくても、その値をカウントしようとしています。 ステータスが変わるまで(毎週の新しいステータスをカウントする必要があります)、毎週のステータスをカウントする必要があります。 基本値の結果として次の表が得られました。特定の列が変更されるまで数週間Oracle SQLカウント値

 
SRQNR  KW Status 
TB-17-0002 1 Status 05 
TB-17-0002 1 Status 20 
TB-17-0002 1 Status 25 
TB-17-0002 1 Status 30 
TB-17-0002 8 Status 40 
TB-17-0002 8 Status 44 
TB-17-0002 8 Status 45 
TB-17-0004 1 Status 05 
TB-17-0004 1 Status 20 
TB-17-0004 2 Status 25 
TB-17-0004 2 Status 30 
TB-17-0004 2 Status 40 
TB-17-0004 2 Status 44 
TB-17-0004 4 Status 70 
TB-17-0004 4 Status 85 
TB-17-0004 4 Status 90 
TB-17-0005 1 Status 05 
TB-17-0005 1 Status 20 
TB-17-0005 1 Status 25 
TB-17-0005 1 Status 30 
TB-17-0005 2 Status 40 
TB-17-0005 2 Status 44 
TB-17-0005 6 Status 45 
TB-17-0006 1 Status 05 
TB-17-0006 1 Status 20 
TB-17-0006 1 Status 25 
TB-17-0006 1 Status 30 
TB-17-0006 1 Status 40 
TB-17-0006 11 Status 44 
TB-17-0006 11 Status 45 
TB-17-0007 1 Status 05 
TB-17-0007 1 Status 20 
TB-17-0007 1 Status 25 
TB-17-0007 1 Status 30 
TB-17-0007 2 Status 40 
TB-17-0007 2 Status 44 
TB-17-0007 2 Status 45 
TB-17-0008 1 Status 05 
TB-17-0008 1 Status 20 
TB-17-0008 2 Status 25 
TB-17-0008 2 Status 30 
TB-17-0008 2 Status 40 
TB-17-0008 2 Status 44 
TB-17-0008 2 Status 45 
TB-17-0009 1 Status 05 
TB-17-0009 1 Status 20 
TB-17-0009 1 Status 25 
TB-17-0009 1 Status 30 
TB-17-0009 1 Status 40 
TB-17-0009 15 Status 44 
TB-17-0009 15 Status 45 
TB-17-0010 1 Status 05 
TB-17-0010 1 Status 20 
TB-17-0010 1 Status 25 
TB-17-0010 1 Status 30 
TB-17-0010 1 Status 40 
TB-17-0010 1 Status 44 
TB-17-0010 5 Status 45 
TB-17-0011 1 Status 05 
TB-17-0011 1 Status 20 
TB-17-0011 1 Status 25 
TB-17-0011 11 Status 30 
TB-17-0011 11 Status 40 
TB-17-0011 11 Status 44 
TB-17-0011 11 Status 70 
TB-17-0011 11 Status 85 
TB-17-0011 20 Status 90 

たとえば、srqnr TB-17-0002はKW 1のステータス30を取得し、KW8のステータス40に変更します。 私の願望は、KW 2,3,4,5,6,7ステータス30がカウントされることです。

Interpretation

SRQNSはステータス30にKW 3または4のままであり、これは、同様の解釈である必要がありますので。

あなたのソリューションに感謝します。

+0

キーワード、数とそれがどこから来たのは何ですか? – Serg

+1

希望の出力に値を記入できますか?あなたはすべてゼロを望んでいないと思っていますが、あなたが期待している値は不明です。 – Degan

+0

この質問のタイトルと要求された結果セットの形式との間の接続が見当たりません。 –

答えて

0

派生した範囲のセットを行として、KWを列のセットとして使用すると、これらの範囲でグループ化し、次にKW値でピボットする必要があります。このクエリを使用して

+----------+------+------+------+------+------+------+------+------+------+------+----+------+------+------+------+------+------+------+------+------+ 
| RANGES | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 
+----------+------+------+------+------+------+------+------+------+------+------+----+------+------+------+------+------+------+------+------+------+ 
| <=29  | 31 | 4 | null | null | null | null | null | null | null | null | 1 | null | null | null | null | null | null | null | null | null | 
| 30 to 69 | 4 | 10 | null | null | 1 | 1 | null | 3 | null | null | 4 | null | null | null | 2 | null | null | null | null | null | 
| 70 to 84 | null | null | null | 1 | null | null | null | null | null | null | 1 | null | null | null | null | null | null | null | null | null | 
| 85 to 90 | null | null | null | 2 | null | null | null | null | null | null | 1 | null | null | null | null | null | null | null | null | 1 | 
+----------+------+------+------+------+------+------+------+------+------+------+----+------+------+------+------+------+------+------+------+------+ 

:この結果は生成された特定のサンプルデータから、私は試していない

SELECT 
    * 
FROM (
     select 
      case when substr(status,-2,2) < '30' then '<=29' 
       when substr(status,-2,2) between '30' and '69' then '30 to 69' 
       when substr(status,-2,2) between '70' and '84' then '70 to 84' 
       when substr(status,-2,2) between '85' and '90' then '85 to 90' 
       else '???' 
       end as ranges 
     , KW 
     , count(*) as counter 
     from mytable t 
     group by 
      case when substr(status,-2,2) <= '30' then '<=29' 
       when substr(status,-2,2) between '30' and '69' then '30 to 69' 
       when substr(status,-2,2) between '70' and '84' then '70 to 84' 
       when substr(status,-2,2) between '85' and '90' then '85 to 90' 
       else '???' 
       end 
     , KW 
     ) 
PIVOT (
    SUM(counter) 
    FOR 
     (KW) 
    IN 
     (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20) 
    ) 
; 

注範囲にステータスから取り扱いサブと注意します。その列の実際のデータに応じて、IN(...)を使用することをお勧めします。

dbfiddle here

関連する問題