简体   繁体   中英

Partition by consecutive values

I have the following data set:

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

I'm tryin to get a value that equals the first txn_date in the set of operations. I'm using:

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

The problem is that my partition will pick up the first txn_date from the first set of operations if they occur more than once (like operation 600). I'm thinking I need to do something with this table to indicate the consecutive operation numbers so that I can add that to my partition.

What could I do in a SELECT to indicate this level of partitioning?

You need to group rows by sets of operation.

One method is to use a running total that increases when it reaches a new "set", as in:

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

Then you can use analytics partitioning by the new column, this should give you what you want:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM