我嘗試根據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]
爲什麼在所需輸出的最後兩行中,分組重置爲「1」? – iamdave
在診斷步驟之間的那些行被視爲廢料和勞動力,其餘的被視爲好產品。所以我需要以不同的方式來計算。但我可以在報告解決方案中做到這一點。 – hvdbunte