简体   繁体   English

我需要创建一个SQL或PLSQL查询来合并和排序表中的数据

[英]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. 我正在尝试创建一个SQL查询,该查询将帮助我从以下数据中获取适当的有序输出。

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 : 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. 我希望能够使用单个SQL语句完成上述操作。 Is it possible to do the above using a single SQL statement? 是否可以使用单个SQL语句完成上述操作? 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. 这适用于postgres,但可以进行细微更改以适应Oracle,恕我直言。 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: 在Oracle中,我们可以使用分析功能将孤岛分组在一起:

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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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