SQL for deleting records which are duplicate(consecutive), But storing there min date in Start date and max date as End Date

那年仲夏 提交于 2021-02-08 08:21:15

问题


I have below input data in a Sample table:

S_ID  C_ID  E_ID   ST_DT        ED_DT
100   A     11AS   01/01/2020   05/01/2020
100   A     11AS   06/01/2020   10/01/2020
100   A     11AS   11/01/2020   15/01/2020
100   A     11BT   16/01/2020   20/01/2020
100   A     11AS   21/01/2020   27/01/2020
100   A     11AS   28/01/2020   30/01/2020

Expected Output in below table:

S_ID  C_ID  E_ID   ST_DT        ED_DT
100   A     11AS   01/01/2020   15/01/2020
100   A     11BT   16/01/2020   20/01/2020
100   A     11AS   21/01/2020   30/01/2020

Database: Netezza Note: These are sample records from data. There are other E_ID in the table's as well.

Thanks


回答1:


This is a gaps-and-islands problem. Assuming you have no gaps, a simple way is the difference of row numbers:

select s_id, c_id, e_id, min(st_dt), max(ed_dt)
from (select t.*,
             row_number() over (partition by s_id, c_id order by st_dt) as seqnum,
             row_number() over (partition by s_id, c_id, e_id order by st_dt) as seqnum_2
      from t
     ) t
group by s_id, c_id, e_id, (seqnum - seqnum_2);



回答2:


It is in fact a Gaps-and-islands problem. The islands, as @Gordon Linoff calls them, are also called sessions in clickstream analysis and IoT data analysis for example.

I'll get a session identifier, and will group by it at the end.

Nesting full-SELECTs, each containing a different OLAP function, ending with a GROUP BY the obtained session id, should do the trick:

WITH
-- your input ...
input(s_id,c_id,e_id,st_dt,ed_dt) AS (
          SELECT 100  ,'A' , '11AS',DATE '2020-01-01', DATE '2020-01-05'
UNION ALL SELECT 100  ,'A' , '11AS',DATE '2020-01-06', DATE '2020-01-10'
UNION ALL SELECT 100  ,'A' , '11AS',DATE '2020-01-11', DATE '2020-01-15'
UNION ALL SELECT 100  ,'A' , '11BT',DATE '2020-01-16', DATE '2020-01-20'
UNION ALL SELECT 100  ,'A' , '11AS',DATE '2020-01-21', DATE '2020-01-27'
UNION ALL SELECT 100  ,'A' , '11AS',DATE '2020-01-28', DATE '2020-01-30'
)

-- add a change "flag" integer that is 0 when the e_id does not change and 1 if it does change ...

,
with_chg AS (
  SELECT
    *
  , CASE WHEN NVL(LAG(e_id) OVER(ORDER BY st_dt),'') <> e_id THEN 1 ELSE 0 END AS chg
  from input
)
-- SELECT * FROM with_chg; -- check query ....
-- out  s_id | c_id | e_id |   st_dt    |   ed_dt    | chg 
-- out ------+------+------+------------+------------+-----
-- out   100 | A    | 11AS | 2020-01-01 | 2020-01-05 |   1
-- out   100 | A    | 11AS | 2020-01-06 | 2020-01-10 |   0
-- out   100 | A    | 11AS | 2020-01-11 | 2020-01-15 |   0
-- out   100 | A    | 11BT | 2020-01-16 | 2020-01-20 |   1
-- out   100 | A    | 11AS | 2020-01-21 | 2020-01-27 |   1
-- out   100 | A    | 11AS | 2020-01-28 | 2020-01-30 |   0

-- get the running sum of the just obtained column chg , and you have a session identifier ...

,
with_session AS (
  SELECT
    s_id
  , c_id
  , e_id
  , st_dt
  , ed_dt
  , SUM(chg) OVER(ORDER BY st_dt) AS session
  FROM with_chg
)
-- SELECT * FROM with_session; -- test query ...
-- out  s_id | c_id | e_id |   st_dt    |   ed_dt    | session 
-- out ------+------+------+------------+------------+---------
-- out   100 | A    | 11AS | 2020-01-01 | 2020-01-05 |       1
-- out   100 | A    | 11AS | 2020-01-06 | 2020-01-10 |       1
-- out   100 | A    | 11AS | 2020-01-11 | 2020-01-15 |       1
-- out   100 | A    | 11BT | 2020-01-16 | 2020-01-20 |       2
-- out   100 | A    | 11AS | 2020-01-21 | 2020-01-27 |       3
-- out   100 | A    | 11AS | 2020-01-28 | 2020-01-30 |       3

-- Now, finally, GROUP BY s_id,c_id,e_id and the session, and get min(st_dt) and max(st_dt) ...

SELECT
  s_id
, c_id
, e_id
, MIN(st_dt) AS st_dt
, MAX(ed_dt) AS ed_dt
FROM with_session
GROUP BY
  s_id
, c_id
, e_id
, session
ORDER BY 4
;
-- out  s_id | c_id | e_id |   st_dt    |   ed_dt    
-- out ------+------+------+------------+------------
-- out   100 | A    | 11AS | 2020-01-01 | 2020-01-15
-- out   100 | A    | 11BT | 2020-01-16 | 2020-01-20
-- out   100 | A    | 11AS | 2020-01-21 | 2020-01-30



来源:https://stackoverflow.com/questions/63305315/sql-for-deleting-records-which-are-duplicateconsecutive-but-storing-there-min

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!