簡體   English   中英

通過更改元素的 T-SQL 分組

[英]T-SQL grouping by changing elements

希望你們一切安好!

我的第一篇文章,我需要一些關於 T-SQL 的幫助。

如何標記(計算發生了多少 PICK_STATE_CHANGES)從一個事件到另一個事件的事件列表?

例如,我想將oel_class列中 'LABM_PICK_SCAN_LOCATION' 和第一個 'LABM_PICK_DECLARE_TROLLEY_FULL' 之間的所有行標記(創建虛擬容器)為 1,然后每個下一個 'LABM_PICK_SCAN_LOCATION' 到 'LABM_PICK_DECLARE_FULL1' 前一個:

user    event_time          oel_class
102480  2020-03-25 12:09:58 LABM_PICK_SCAN_LOCATION <-- this is the starting point of the first virtual container (1)
102480  2020-03-25 12:10:23 TM_TROLLEY_COLLECT -  (1)
102480  2020-03-25 12:12:16 PICK_STATE_CHANGE -  (1)
102480  2020-03-25 12:12:39 PICK_STATE_CHANGE -  (1)
102480  2020-03-25 12:13:44 PICK_STATE_CHANGE -  (1)
102480  2020-03-25 12:14:09 PICK_STATE_CHANGE - etc.
102480  2020-03-25 12:14:39 PICK_STATE_CHANGE
102480  2020-03-25 12:15:20 PICK_STATE_CHANGE
102480  2020-03-25 12:15:20 PICK_STATE_CHANGE
102480  2020-03-25 12:16:17 PICK_STATE_CHANGE
102480  2020-03-25 12:16:51 PICK_STATE_CHANGE
102480  2020-03-25 12:17:27 PICK_STATE_CHANGE
102480  2020-03-25 12:18:02 PICK_STATE_CHANGE
102480  2020-03-25 12:18:02 LABM_PICK_DECLARE_TROLLEY_FULL <-- this is the end of the virtual container
102480  2020-03-25 12:18:48 TM_LOC_CHANGE <-- ignore this
102480  2020-03-25 12:19:28 LABM_PICK_DECLARE_TROLLEY_FULL <-- ignore this
102480  2020-03-25 12:21:40 TM_TROLLEY_PARK <-- ignore this
102480  2020-03-25 12:21:40 LABM_PICK_SELECT_PACK_AREA <-- ignore this
102480  2020-03-25 12:21:48 LABM_PICK_SCAN_LOCATION <-- this is the start of the second virtual container (2)
102480  2020-03-25 12:21:55 TM_TROLLEY_COLLECT -  (2)
102480  2020-03-25 12:24:57 PICK_STATE_CHANGE -  (2)
102480  2020-03-25 12:25:55 PICK_STATE_CHANGE -  (2)
102480  2020-03-25 12:26:33 PICK_STATE_CHANGE -  (2)
102480  2020-03-25 12:27:15 PICK_STATE_CHANGE -  (2)
102480  2020-03-25 12:27:52 PICK_STATE_CHANGE -  (2)
102480  2020-03-25 12:28:47 PICK_STATE_CHANGE -  (2)
102480  2020-03-25 12:29:38 PICK_STATE_CHANGE -  (2)
102480  2020-03-25 12:29:38 PICK_STATE_CHANGE -  (2)
102480  2020-03-25 12:30:03 PICK_STATE_CHANGE -  (2)
102480  2020-03-25 12:30:53 PICK_STATE_CHANGE -  (2)
102480  2020-03-25 12:33:16 PICK_STATE_CHANGE -  (2)
102480  2020-03-25 12:35:52 PICK_STATE_CHANGE -  (2)
102480  2020-03-25 12:35:52 LABM_PICK_DECLARE_TROLLEY_FULL <-- this is the end of the virtual container
102480  2020-03-25 12:44:05 TM_TROLLEY_PARK <-- ignore this

etc. 3, 4, 5, ...

有任何想法嗎? 我想做一個 CASE 但我怎么說在特定事件旁邊放一個 1 然后在“虛擬容器”上放 +1?

我正在使用 SQL Server 2017。

如果我正確地跟隨您,您可以使用lead()和累積sum()

select t.*,
    case when oel_class in ('TM_TROLLEY_COLLECT', 'PICK_STATE_CHANGE')
        then sum(case when oel_class = 'TM_TROLLEY_COLLECT' and lead_oel_class = 'PICK_STATE_CHANGE' then 1 else 0 end) over(partition by user order by event_time)
    end as grp
from (
    select t.*, 
        lead(oel_class) over(partition by user order by event_time) lead_oel_class
    from mytable t
) t

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM