2017-06-07 69 views
0

我嘗試根據O_Diagnosis操作生成組編號。我想將當前和下一個O_Diagnosis操作之間的操作分組。SQL生成組編號

問題是這個數據沒有任何分組功能的列。所以我需要從頭開始生成這個列。我想達到的是以下幾點:

Site SFC Date Time   Operation   Times_processed Diagnosis_group_nr 
P500 B218YW 2017-03-21 10:16:23 O_SF_WRAP   1    1 
P500 B218YW 2017-03-23 06:07:53 O_SF_WRAP_CURE  1    1 
P500 B218YW 2017-03-23 14:23:41 O_DIAGNOSIS  1    2 
P500 B218YW 2017-03-28 10:07:55 O_SF_WRAP   2    2 
P500 B218YW 2017-03-28 15:02:21 O_SF_WRAP_CURE  2    2 
P500 B218YW 2017-03-29 20:38:06 O_DIAGNOSIS  2    3 
P500 B218YW 2017-03-29 20:39:15 O_DIAGNOSIS  3    4 
P500 B218YW 2017-04-27 08:43:53 O_SF_WRAP   3    4 
P500 B218YW 2017-04-27 12:43:25 O_SF_WRAP_CURE  3    4 
P500 B218YW 2017-04-27 13:05:16 O_SF_PCA_ASSEMBLY 1    4 
P500 B218YW 2017-04-27 13:19:15 O_DIAGNOSIS  4    5 
P500 B218YW 2017-04-27 15:23:09 O_SF_PCA_ASSEMBLY 2    5 
P500 B218YW 2017-04-27 16:10:32 O_SF_ELEC_INT_TEST 1    1 
P500 B218YW 2017-04-27 22:16:23 O_RECORD_REVIEW 1    1 

Times處理的列計算SFC通過操作的頻率。這就是爲什麼你會看到O_SF_WRAP爲1的例子,當下一次從O_Diagnosis發回SFC時,這將變爲2。

是否有可能使一個函數或SQL視圖來實現這一點?我使用SQL Server 2012的

什麼我到目前爲止是這樣的,對於相同的SFC:

Site SFC Date Time   Operation   Times_processed Diagnosis_group_nr 
P500 B218YW 2017-03-21 10:16:23 O_SF_WRAP   1    1 
P500 B218YW 2017-03-23 06:07:53 O_SF_WRAP_CURE  1    1 
P500 B218YW 2017-03-23 14:23:41 O_DIAGNOSIS  1    2 
P500 B218YW 2017-03-28 10:07:55 O_SF_WRAP   2    2 
P500 B218YW 2017-03-28 15:02:21 O_SF_WRAP_CURE  2    2 
P500 B218YW 2017-03-29 20:38:06 O_DIAGNOSIS  2    3 
P500 B218YW 2017-03-29 20:39:15 O_DIAGNOSIS  3    3 
P500 B218YW 2017-04-27 08:43:53 O_SF_WRAP   3    3 
P500 B218YW 2017-04-27 12:43:25 O_SF_WRAP_CURE  3    3 
P500 B218YW 2017-04-27 13:05:16 O_SF_PCA_ASSEMBLY 1    4 
P500 B218YW 2017-04-27 13:19:15 O_DIAGNOSIS  4    4 
P500 B218YW 2017-04-27 15:23:09 O_SF_PCA_ASSEMBLY 2    2 
P500 B218YW 2017-04-27 16:10:32 O_SF_ELEC_INT_TEST 1    1 
P500 B218YW 2017-04-27 22:16:23 O_RECORD_REVIEW 1    1 

這是我到目前爲止的代碼:

WITH 
    cteNextRow 
    AS 
    (
    SELECT 
     opl.[PL SITE] 
     , opl.[PL SFC] 
     , opl.[Pl End Time Local] 
     , opl.[PL OPERATION] 
     , LEAD(opl.[PL OPERATION], 1) OVER (PARTITION BY opl.[PL SFC] ORDER BY opl.[Pl End Time Local]) AS NEXT_OPERATION 
     , LEAD(opl.[Pl Times Processed], 1) OVER (PARTITION BY opl.[PL SFC] ORDER BY opl.[Pl End Time Local]) AS NEXT_TIMES_PROCESSED 
     , max(opl.[Pl Times Processed]) OVER (PARTITION BY opl.[PL SFC] ORDER BY opl.[PL SFC], opl.[Pl End Time Local] 
     ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) TOTAL_TIMES_PROCESSED 
     ,LAST_VALUE(opl.[PL OPERATION]) OVER (PARTITION BY opl.[PL SFC] ORDER BY opl.[PL SFC], opl.[Pl End Time Local] 
     ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING 
     ) as LAST_OPERATION 
     ,case when LAST_VALUE(opl.[PL OPERATION]) OVER (PARTITION BY opl.[PL SFC] ORDER BY opl.[PL SFC], opl.[Pl End Time Local] 
     ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) not like '%review%' 
     then 0 
     when LAST_VALUE(opl.[PL OPERATION]) OVER (PARTITION BY opl.[PL SFC] ORDER BY opl.[PL SFC], opl.[Pl End Time Local] 
     ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) like '%review%' and max(opl.[Pl Times Processed]) OVER (PARTITION BY opl.[PL SFC] ORDER BY opl.[PL SFC], opl.[Pl End Time Local] 
     ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) = 1 
     then 1 
     else 9999 
     end as REVIEW_IND 
    FROM Report1 opl 
    where [pl sfc] = 'B218YW' 
) 
select 
    opl.[pl Site] AS PROCESS_SITE 
     , opl.[PL SFC] AS PROCESS_SFC 
     , opl.[Pl End Time Local] PROCESS_DATE_TIME 
     , opl.[PL OPERATION] AS PROCESS_OPERATION 
     , cte.NEXT_OPERATION AS PROCESS_NEXT_OPERATION 
     , opl.[Pl Times Processed] AS PROCESS_TIMES_PROCESSED 
      , CASE 
      when cte.REVIEW_IND = 9999 and cte.NEXT_TIMES_PROCESSED = opl.[Pl Times Processed] and cte.NEXT_OPERATION like '%DIAG%' 
       then LEAD(opl.[Pl Times Processed], 1) OVER (PARTITION BY opl.[PL SFC] ORDER BY opl.[Pl End Time Local]) 
      when cte.REVIEW_IND = 9999 and cte.NEXT_TIMES_PROCESSED < opl.[Pl Times Processed] 
       then opl.[Pl Times Processed] 
      when cte.REVIEW_IND = 9999 and NEXT_TIMES_PROCESSED is null 
       then opl.[Pl Times Processed] 
      when cte.REVIEW_IND = 9999 and cte.NEXT_TIMES_PROCESSED is not null 
       then cte.NEXT_TIMES_PROCESSED 
      else REVIEW_IND 
     END AS Diagnosis_group_nr 
FROM Report1 opl 
join cteNextRow cte on cte.[PL SITE] = opl.[pl Site] and cte.[PL sfc] = opl.[PL SFC] and cte.[pl operation] = opl.[pl OPERATION] and opl.[Pl End Time Local] = cte.[Pl End Time Local] 
+0

爲什麼在所需輸出的最後兩行中,分組重置爲「1」? – iamdave

+0

在診斷步驟之間的那些行被視爲廢料和勞動力,其餘的被視爲好產品。所以我需要以不同的方式來計算。但我可以在報告解決方案中做到這一點。 – hvdbunte

回答

0

我不知道爲什麼您的分組編號在您想要的輸出結束時重置爲1,但如果這是一個錯字,您可以使用簡單的窗口條件row_numbersum分別獲得Times_processed和您需要的「Diagnosis_group_nr」的編號:

declare @t table(Site nvarchar(50),SFC nvarchar(50),DateValue datetime,Operation nvarchar(50)); 
insert into @t values 
('P500','B218YW','2017-03-21 10:16:23','O_SF_WRAP') 
,('P500','B218YW','2017-03-23 06:07:53','O_SF_WRAP_CURE') 
,('P500','B218YW','2017-03-23 14:23:41','O_DIAGNOSIS') 
,('P500','B218YW','2017-03-28 10:07:55','O_SF_WRAP') 
,('P500','B218YW','2017-03-28 15:02:21','O_SF_WRAP_CURE') 
,('P500','B218YW','2017-03-29 20:38:06','O_DIAGNOSIS') 
,('P500','B218YW','2017-03-29 20:39:15','O_DIAGNOSIS') 
,('P500','B218YW','2017-04-27 08:43:53','O_SF_WRAP') 
,('P500','B218YW','2017-04-27 12:43:25','O_SF_WRAP_CURE') 
,('P500','B218YW','2017-04-27 13:05:16','O_SF_PCA_ASSEMBLY') 
,('P500','B218YW','2017-04-27 13:19:15','O_DIAGNOSIS') 
,('P500','B218YW','2017-04-27 15:23:09','O_SF_PCA_ASSEMBLY') 
,('P500','B218YW','2017-04-27 16:10:32','O_SF_ELEC_INT_TEST') 
,('P500','B218YW','2017-04-27 22:16:23','O_RECORD_REVIEW'); 

select * 
    ,row_number() over (partition by Operation order by DateValue) as Times_processed 
    ,sum(case when Operation = 'O_DIAGNOSIS' then 1 else 0 end) over (order by DateValue)+1 as Diagnosis_group_nr 
from @t 
order by DateValue; 

輸出:

+------+--------+-------------------------+--------------------+-----------------+--------------------+ 
| Site | SFC |  DateValue  |  Operation  | Times_processed | Diagnosis_group_nr | 
+------+--------+-------------------------+--------------------+-----------------+--------------------+ 
| P500 | B218YW | 2017-03-21 10:16:23.000 | O_SF_WRAP   |    1 |     1 | 
| P500 | B218YW | 2017-03-23 06:07:53.000 | O_SF_WRAP_CURE  |    1 |     1 | 
| P500 | B218YW | 2017-03-23 14:23:41.000 | O_DIAGNOSIS  |    1 |     2 | 
| P500 | B218YW | 2017-03-28 10:07:55.000 | O_SF_WRAP   |    2 |     2 | 
| P500 | B218YW | 2017-03-28 15:02:21.000 | O_SF_WRAP_CURE  |    2 |     2 | 
| P500 | B218YW | 2017-03-29 20:38:06.000 | O_DIAGNOSIS  |    2 |     3 | 
| P500 | B218YW | 2017-03-29 20:39:15.000 | O_DIAGNOSIS  |    3 |     4 | 
| P500 | B218YW | 2017-04-27 08:43:53.000 | O_SF_WRAP   |    3 |     4 | 
| P500 | B218YW | 2017-04-27 12:43:25.000 | O_SF_WRAP_CURE  |    3 |     4 | 
| P500 | B218YW | 2017-04-27 13:05:16.000 | O_SF_PCA_ASSEMBLY |    1 |     4 | 
| P500 | B218YW | 2017-04-27 13:19:15.000 | O_DIAGNOSIS  |    4 |     5 | 
| P500 | B218YW | 2017-04-27 15:23:09.000 | O_SF_PCA_ASSEMBLY |    2 |     5 | 
| P500 | B218YW | 2017-04-27 16:10:32.000 | O_SF_ELEC_INT_TEST |    1 |     5 | 
| P500 | B218YW | 2017-04-27 22:16:23.000 | O_RECORD_REVIEW |    1 |     5 | 
+------+--------+-------------------------+--------------------+-----------------+--------------------+ 

如果你想在一個以上的SiteSFC運行上面的腳本,只要這些列添加到partition by兩個窗口功能,他們會正確計算整個數據集。

+0

非常感謝。我不知道有可能使用總和和病例陳述。 這讓它變得如此簡單! – hvdbunte