0

我正在尝试创建一个 SQL 查询,以帮助我从以下数据中获得正确的有序输出。

表中数据:

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   

查询的预期输出:

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 语句来完成上述工作。是否可以使用单个 SQL 语句执行上述操作?是否有其他方法可以执行上述操作。

4

3 回答 3

1
SELECT
  Customer,
  `number` AS Number,
  MIN(Eff_Date) AS Eff_Date,
  MAX(Exp_date) AS Exp_date
FROM tablename
GROUP BY Customer, number
于 2012-12-25T13:52:20.933 回答
0

这适用于 postgres,但它可以通过微小的变化适应 oracle,恕我直言。注意:我稍微更改了数据,因为重叠间隔对我来说看起来不合理。

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
        ;

结果:

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)
于 2012-12-25T15:05:06.163 回答
0

在 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>
于 2012-12-30T16:18:08.873 回答