简体   繁体   中英

I require to create an SQL or PLSQL Query for merging and ordering data in a table

I am trying to create a SQL query that will help me get a proper ordered output from the below data.

Data in table :

Cust   num     Eff_Date     Exp_date           
1001   1234    10-01-2010   20-06-2010      
1001   1234    20-06-2010   25-06-2010       
1001   1234    25-06-2010   12-02-2011          
1001   1234    12-02-2011   12-02-2011            
1001   3456    12-02-2011   25-07-2012      
1001   3456    25-07-2012   25-07-2012      
1001   1234    25-07-2012   25-07-2012      
1001   1234    25-07-2012   31-12-4700   

Expected output of Query :

Cust   num     Eff_Date     Exp_date           
1001   1234    10-01-2010   12-02-2011      
1001   3456    12-02-2011   25-07-2012      
1001   1234    25-07-2012   31-12-4700   

I would prefer to be able to do the above using a single SQL statement. Is it possible to do the above using a single SQL statement? Is there an alternate way to do the above.

SELECT
  Customer,
  `number` AS Number,
  MIN(Eff_Date) AS Eff_Date,
  MAX(Exp_date) AS Exp_date
FROM tablename
GROUP BY Customer, number

This works for postgres, but it could be adapted to oracle with minor changes, IMHO. Note: I changed the data a bit, because overlapping intervals don't look plausible to me.

DROP SCHEMA tmp CASCADE;
CREATE SCHEMA tmp ;
SET search_path=tmp;

CREATE TABLE lutser
        ( cust INTEGER NOT NULL
        , num iNTEGER NOT NULL
        , eff_date DATE NOT NULL
        , exp_date DATE NOT NULL
        , PRIMARY KEY (cust, num, eff_date)
        );

SET datestyle=german;

INSERT INTO lutser(cust,num,eff_date,exp_date) VALUES
 (1001,1234,'10-01-2010', '20-06-2010' )
,(1001,1234,'20-06-2010', '25-06-2010' )
,(1001,1234,'25-06-2010', '12-02-2011' )
,(1001,1234,'12-02-2011', '12-02-2011' )
,(1001,3456,'12-02-2011', '25-07-2012' )
,(1001,3456,'25-07-2012', '25-07-2012' )
,(1001,1234,'25-07-2012', '25-08-2012' ) -- added a month to get unique PK
,(1001,1234,'25-08-2012', '31-12-4700' ) -- and here as well
        ;

VACUUM ANALYZE lutser;

-- SELECT * FROM lutser ORDER BY cust,num,eff_date;

-- EXPLAIN ANALYZE
WITH RECURSIVE island AS
        ( SELECT cust,num,eff_date,exp_date
        FROM lutser l0
        WHERE NOT EXISTS
                ( SELECT *
                FROM lutser nx
                WHERE nx.cust = l0.cust AND nx.num = l0.num
                AND nx.eff_date < l0.eff_date
                AND nx.exp_date >= l0.eff_date
                )
        UNION -- ALL
        SELECT isl.cust,isl.num, isl.eff_date,l1.exp_date
        FROM lutser l1
        JOIN island isl ON isl.cust = l1.cust AND isl.num = l1.num
                AND isl.eff_date < l1.eff_date
                AND isl.exp_date >= l1.eff_date
        )
SELECT DISTINCT ON (cust,num,eff_date) *
FROM island
ORDER BY cust,num,eff_date
        ;

Result:

NOTICE:  drop cascades to table tmp.lutser
DROP SCHEMA
CREATE SCHEMA
SET
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "lutser_pkey" for table "lutser"
CREATE TABLE
SET
INSERT 0 8
VACUUM
 cust | num  |  eff_date  |  exp_date  
------+------+------------+------------
 1001 | 1234 | 10.01.2010 | 20.06.2010
 1001 | 1234 | 25.07.2012 | 25.08.2012
 1001 | 3456 | 12.02.2011 | 25.07.2012
(3 rows)

in Oracle, we can use analytic functions to group islands together:

SQL> select c.cust, c.num, min(eff_date) eff_date, max(exp_Date) exp_date
  2    from (select c.cust, c.num, c.eff_date, c.exp_date, max(rn) over (partition by cust, num order by eff_date) grp
  3            from (select c.cust, c.num, c.eff_date, c.exp_date,
  4                         case
  5                           when lag(exp_date, 1) over (partition by cust, num order by eff_date) != eff_date
  6                           then
  7                             row_number() over (partition by cust, num order by eff_date)
  8                           when row_number() over (partition by cust, num order by eff_date) = 1
  9                           then
 10                             1
 11                        end rn
 12                    from cust c) c) c
 13    group by c.cust, c.num, grp
 14    order by eff_date;

      CUST        NUM EFF_DATE   EXP_DATE
---------- ---------- ---------- ----------
      1001       1234 10-01-2010 12-02-2011
      1001       3456 12-02-2011 25-07-2012
      1001       1234 25-07-2012 31-12-4700

SQL>

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