gpt4 book ai didi

sql - 按连续值分区

转载 作者:行者123 更新时间:2023-12-02 04:52:09 24 4
gpt4 key购买 nike

我有以下数据集:

Material  Operation  Txn_Date
M1004 100 8/25/2013 8:22:05 PM
M1004 100 8/25/2013 8:34:37 PM
M1004 100 8/29/2013 9:03:01 PM
M1004 600 8/29/2013 11:48:01 PM
M1004 600 8/30/2013 7:48:34 AM
M1004 600 8/30/2013 8:32:00 AM
M1004 500 8/30/2013 9:38:35 AM
M1004 500 8/30/2013 9:54:52 AM
M1004 500 8/30/2013 10:07:35 AM
M1004 500 8/30/2013 11:53:28 AM
M1004 500 9/2/2013 2:30:56 AM
M1004 200 9/2/2013 2:59:20 AM
M1004 900 9/2/2013 3:30:11 AM
M1004 600 9/3/2013 10:35:01 AM
M1004 600 9/3/2013 10:48:24 AM
M1004 600 9/3/2013 11:17:00 AM

我正在尝试获取等于操作集中第一个 txn_date 的值。我正在使用:

SELECT 
Material,
Operation,
Txn_Date,
MIN(Txn_Date) OVER(PARTITION BY Material, Operation) AS first_txn_date
FROM oper_tab
WHERE Material = 'M1004'
ORDER BY Txn_Date

问题是,如果它们发生不止一次(如操作 600),我的分区将从第一组操作中选取第一个 txn_date。我在想我需要对这个表做些什么来指示连续的操作编号,以便我可以将其添加到我的分区中。

我可以在 SELECT 中做什么来指示这种分区级别?

最佳答案

您需要按操作集对行进行分组。

一种方法是使用一个运行总数,当它达到一个新的“集合”时会增加,如:

SQL> SELECT mat, op, dt,
2 SUM(change_set) over (PARTITION BY mat ORDER BY dt) set_group
3 FROM (SELECT mat, op, dt,
4 CASE WHEN op != lag(op) over (PARTITION BY mat
5 ORDER BY dt)
6 THEN 1
7 END change_set
8 FROM DATA);

MAT OP DT SET_GROUP
----- ---------- ----------- ----------
M1004 100 25/08/2013
M1004 100 25/08/2013
M1004 100 29/08/2013
M1004 600 29/08/2013 1
M1004 600 30/08/2013 1
M1004 600 30/08/2013 1
M1004 500 30/08/2013 2
M1004 500 30/08/2013 2
M1004 500 30/08/2013 2
M1004 500 30/08/2013 2
M1004 500 02/09/2013 2
M1004 200 02/09/2013 3
M1004 900 02/09/2013 4
M1004 600 03/09/2013 5
M1004 600 03/09/2013 5
M1004 600 03/09/2013 5

然后您可以使用按新列进行的分析分区,这应该可以满足您的需求:

SQL> SELECT mat, op, dt, MIN(dt) over (PARTITION BY mat, set_group) first_txn
2 FROM (SELECT mat, op, dt,
3 SUM(change_set) over (PARTITION BY mat ORDER BY dt) set_group
4 FROM (SELECT mat, op, dt,
5 CASE WHEN op != lag(op) over (PARTITION BY mat
6 ORDER BY dt)
7 THEN 1
8 END change_set
9 FROM DATA));

MAT OP DT FIRST_TXN
----- ---------- -------------------- --------------------
M1004 600 29/08/2013 23:48:01 29/08/2013 23:48:01
M1004 600 30/08/2013 07:48:34 29/08/2013 23:48:01
M1004 600 30/08/2013 08:32:00 29/08/2013 23:48:01
M1004 500 30/08/2013 09:38:35 30/08/2013 09:38:35
M1004 500 30/08/2013 09:54:52 30/08/2013 09:38:35
M1004 500 30/08/2013 11:53:28 30/08/2013 09:38:35
M1004 500 02/09/2013 02:30:56 30/08/2013 09:38:35
M1004 500 30/08/2013 10:07:35 30/08/2013 09:38:35
M1004 200 02/09/2013 02:59:20 02/09/2013 02:59:20
M1004 900 02/09/2013 03:30:11 02/09/2013 03:30:11
M1004 600 03/09/2013 10:35:01 03/09/2013 10:35:01
M1004 600 03/09/2013 10:48:24 03/09/2013 10:35:01
M1004 600 03/09/2013 11:17:00 03/09/2013 10:35:01
M1004 100 25/08/2013 20:22:05 25/08/2013 20:22:05
M1004 100 25/08/2013 20:34:37 25/08/2013 20:22:05
M1004 100 29/08/2013 21:03:01 25/08/2013 20:22:05

关于sql - 按连续值分区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18639570/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com