2017-07-01 37 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中KW8了狀態30 KW 1和變爲狀態40。 現在我的願望是KW 2,3,4,5,6,7狀態30將被計數。

Interpretation

由於SRQNS仍處於KW 3或4狀態30,這應是在解釋爲好。

感謝您的解決方案。

+0

什麼是KWS的數量和它從何而來? – 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