SQL查询,按日期创建组

时间:2015-09-21 08:21:44

标签: sql oracle oracle11g

这是我的初始表,(日期是DD / MM / YY格式)

  ID DAY_INI    DAY_END        TYPE_ID TYPE  NUM START_DATE END_DATE   
---- ---------  ---------      ------- ---- ---- ---------  ---------
4241 15/09/15   16/09/15             2    1   66  01/01/00   31/12/99  
4241 17/09/15   18/09/15             9    1   59  17/09/15   18/09/15  
4241 19/09/15   20/09/15             2    1   66  01/01/00   31/12/99  

4241 15/09/15   15/09/15             3    2   63  01/01/00   31/12/99  
4241 16/09/15   17/09/15             8    2  159  16/09/15   17/09/15  
4241 18/09/15   20/09/15             3    2   63  01/01/00   31/12/99   

2134 15/09/15   16/09/15             2    1   66  01/01/00   31/12/99  
2134 17/09/15   18/09/15             9    1   59  17/09/15   18/09/15  
2134 19/09/15   20/09/15             2    1   66  01/01/00   31/12/99  

2134 15/09/15   15/09/15             3    2   63  01/01/00   31/12/99  
2134 16/09/15   17/09/15             8    2  159  16/09/15   17/09/15  
2134 18/09/15   20/09/15             3    2   63  01/01/00   31/12/99  

我要创建具有初始DAY和结束DAY的组,以获得相同的ID和TYPE。

我不想白天分组,我需要在每次TYPE_ID更改时创建一个组,基于初始顺序(ID,TYPE,DAY ASC)

这是我想要实现的结果:

$this->element()

你能否提供关于如何做到这一点的任何线索?谢谢!

2 个答案:

答案 0 :(得分:0)

将枚举添加到原始数据集(使用Row_Number或rownum)。为每个组添加MIN(枚举)。然后按枚举对组进行排序。

答案 1 :(得分:0)

SQL Fiddle

Oracle 11g R2架构设置

CREATE TABLE TEST ( ID, DAY, TYPE_ID, TYPE, NUM, START_DATE, END_DATE ) AS
          SELECT 4241, DATE '2015-09-15', 2, 1, 66, DATE '2000-01-01', DATE '1999-12-31' FROM DUAL  
UNION ALL SELECT 4241, DATE '2015-09-16', 2, 1, 66, DATE '2000-01-01', DATE '1999-12-31' FROM DUAL  
UNION ALL SELECT 4241, DATE '2015-09-17', 9, 1, 59, DATE '2015-09-17', DATE '2015-09-18' FROM DUAL  
UNION ALL SELECT 4241, DATE '2015-09-18', 9, 1, 59, DATE '2015-09-17', DATE '2015-09-18' FROM DUAL  
UNION ALL SELECT 4241, DATE '2015-09-19', 2, 1, 66, DATE '2000-01-01', DATE '1999-12-31' FROM DUAL  
UNION ALL SELECT 4241, DATE '2015-09-20', 2, 1, 66, DATE '2000-01-01', DATE '1999-12-31' FROM DUAL  
UNION ALL SELECT 4241, DATE '2015-09-15', 3, 2, 63, DATE '2000-01-01', DATE '1999-12-31' FROM DUAL  
UNION ALL SELECT 4241, DATE '2015-09-16', 8, 2, 159, DATE '2015-09-16', DATE '2015-09-17' FROM DUAL  
UNION ALL SELECT 4241, DATE '2015-09-17', 8, 2, 159, DATE '2015-09-16', DATE '2015-09-17' FROM DUAL 
UNION ALL SELECT 4241, DATE '2015-09-18', 3, 2, 63, DATE '2000-01-01', DATE '1999-12-31' FROM DUAL   
UNION ALL SELECT 4241, DATE '2015-09-19', 3, 2, 63, DATE '2000-01-01', DATE '1999-12-31' FROM DUAL  
UNION ALL SELECT 4241, DATE '2015-09-20', 3, 2, 63, DATE '2000-01-01', DATE '1999-12-31' FROM DUAL  
UNION ALL SELECT 2134, DATE '2015-09-15', 2, 1, 66, DATE '2000-01-01', DATE '1999-12-31' FROM DUAL  
UNION ALL SELECT 2134, DATE '2015-09-16', 2, 1, 66, DATE '2000-01-01', DATE '1999-12-31' FROM DUAL  
UNION ALL SELECT 2134, DATE '2015-09-17', 9, 1, 59, DATE '2015-09-17', DATE '2015-09-18' FROM DUAL  
UNION ALL SELECT 2134, DATE '2015-09-18', 9, 1, 59, DATE '2015-09-17', DATE '2015-09-18' FROM DUAL  
UNION ALL SELECT 2134, DATE '2015-09-19', 2, 1, 66, DATE '2000-01-01', DATE '1999-12-31' FROM DUAL  
UNION ALL SELECT 2134, DATE '2015-09-20', 2, 1, 66, DATE '2000-01-01', DATE '1999-12-31' FROM DUAL  
UNION ALL SELECT 2134, DATE '2015-09-15', 3, 2, 63, DATE '2000-01-01', DATE '1999-12-31' FROM DUAL  
UNION ALL SELECT 2134, DATE '2015-09-16', 8, 2, 159, DATE '2015-09-16', DATE '2015-09-17' FROM DUAL  
UNION ALL SELECT 2134, DATE '2015-09-17', 8, 2, 159, DATE '2015-09-16', DATE '2015-09-17' FROM DUAL 
UNION ALL SELECT 2134, DATE '2015-09-18', 3, 2, 63, DATE '2000-01-01', DATE '1999-12-31' FROM DUAL   
UNION ALL SELECT 2134, DATE '2015-09-19', 3, 2, 63, DATE '2000-01-01', DATE '1999-12-31' FROM DUAL  
UNION ALL SELECT 2134, DATE '2015-09-20', 3, 2, 63, DATE '2000-01-01', DATE '1999-12-31' FROM DUAL  

查询1

WITH group_changes AS (
  SELECT t.*,
         CASE TYPE_ID WHEN LAG( TYPE_ID ) OVER ( PARTITION BY ID, TYPE ORDER BY DAY ) THEN 0 ELSE 1 END AS HAS_CHANGED_GROUP
  FROM   TEST t
),
groups AS (
  SELECT ID, DAY, TYPE_ID, TYPE, NUM, START_DATE, END_DATE,
         SUM( HAS_CHANGED_GROUP ) OVER ( PARTITION BY ID, TYPE ORDER BY DAY ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) AS GRP
  FROM   group_changes
)
SELECT ID,
       MIN( DAY ) AS DAY_INI,
       MAX( DAY ) AS DAY_END,
       MIN( TYPE_ID ) AS TYPE_ID,
       TYPE,
       MIN( NUM ) AS NUM,
       MIN( START_DATE ) AS START_DATE,
       MIN( END_DATE ) AS END_DATE
FROM   groups
GROUP BY ID, TYPE, GRP

<强> Results

|   ID |                     DAY_INI |                     DAY_END | TYPE_ID | TYPE | NUM |                  START_DATE |                    END_DATE |
|------|-----------------------------|-----------------------------|---------|------|-----|-----------------------------|-----------------------------|
| 4241 | September, 17 2015 00:00:00 | September, 18 2015 00:00:00 |       9 |    1 |  59 | September, 17 2015 00:00:00 | September, 18 2015 00:00:00 |
| 2134 | September, 15 2015 00:00:00 | September, 15 2015 00:00:00 |       3 |    2 |  63 |   January, 01 2000 00:00:00 |  December, 31 1999 00:00:00 |
| 2134 | September, 18 2015 00:00:00 | September, 20 2015 00:00:00 |       3 |    2 |  63 |   January, 01 2000 00:00:00 |  December, 31 1999 00:00:00 |
| 4241 | September, 15 2015 00:00:00 | September, 16 2015 00:00:00 |       2 |    1 |  66 |   January, 01 2000 00:00:00 |  December, 31 1999 00:00:00 |
| 4241 | September, 19 2015 00:00:00 | September, 20 2015 00:00:00 |       2 |    1 |  66 |   January, 01 2000 00:00:00 |  December, 31 1999 00:00:00 |
| 4241 | September, 15 2015 00:00:00 | September, 15 2015 00:00:00 |       3 |    2 |  63 |   January, 01 2000 00:00:00 |  December, 31 1999 00:00:00 |
| 4241 | September, 16 2015 00:00:00 | September, 17 2015 00:00:00 |       8 |    2 | 159 | September, 16 2015 00:00:00 | September, 17 2015 00:00:00 |
| 2134 | September, 17 2015 00:00:00 | September, 18 2015 00:00:00 |       9 |    1 |  59 | September, 17 2015 00:00:00 | September, 18 2015 00:00:00 |
| 2134 | September, 15 2015 00:00:00 | September, 16 2015 00:00:00 |       2 |    1 |  66 |   January, 01 2000 00:00:00 |  December, 31 1999 00:00:00 |
| 2134 | September, 19 2015 00:00:00 | September, 20 2015 00:00:00 |       2 |    1 |  66 |   January, 01 2000 00:00:00 |  December, 31 1999 00:00:00 |
| 2134 | September, 16 2015 00:00:00 | September, 17 2015 00:00:00 |       8 |    2 | 159 | September, 16 2015 00:00:00 | September, 17 2015 00:00:00 |
| 4241 | September, 18 2015 00:00:00 | September, 20 2015 00:00:00 |       3 |    2 |  63 |   January, 01 2000 00:00:00 |  December, 31 1999 00:00:00 |