将开始日期和结束日期从2个表组合到单个表中

时间:2015-05-05 07:36:17

标签: sql oracle oracle11g psql

表A包含如下值:

start_date  end_date    column_A
1-Jan-15    10-Jan-15   2
11-Jan-15   21-Jan-15   3
22-Jan-15   31-Jan-15   4
1-Feb-15    10-Feb-15   5
11-Feb-15   12-Feb-15   6

表B包含如下值:

start_date  end_date    Column_B
1-Jan-15    20-Jan-15   4
21-Jan-15   31-Jan-15   5
1-Feb-15    5-Feb-15    6
6-Feb-15    12-Feb-15   7

我需要一个查询,它将结合上面的两个表数据,并提供如下结果。

Start_date  end_date    column_A    Column_B
1-Jan-15    10-Jan-15   2            4
11-Jan-15   20-Jan-15   3            4
21-Jan-15   21-Jan-15   3            5 
22-Jan-15   31-Jan-15   4            5
1-Feb-15    5-Feb-15    5            6
6-Feb-15    10-Feb-15   5            7
11-Feb-15   12-Feb-15   6            7

2 个答案:

答案 0 :(得分:1)

我不确定你的约束,但试试这个:

select greatest(a.start_date, b.start_date) start_date, 
least(a.end_date, b.end_date) end_date, 
column_a, column_b
from table_a a, table_b b
where b.start_date <= a.end_date and b.end_date >= a.start_date
order by 1, 2

答案 1 :(得分:0)

SQL Fiddle

Oracle 11g R2架构设置

CREATE TABLE A AS
          SELECT TO_DATE( '1-Jan-15', 'DD-MON-YY' ) AS start_date,  TO_DATE( '10-Jan-15', 'DD-MON-YY' ) AS end_date, 2 AS column_A FROM DUAL
UNION ALL SELECT TO_DATE( '11-Jan-15', 'DD-MON-YY' ) AS start_date,  TO_DATE( '21-Jan-15', 'DD-MON-YY' ) AS end_date, 3 AS column_A FROM DUAL
UNION ALL SELECT TO_DATE( '22-Jan-15', 'DD-MON-YY' ) AS start_date,  TO_DATE( '31-Jan-15', 'DD-MON-YY' ) AS end_date, 4 AS column_A FROM DUAL
UNION ALL SELECT TO_DATE( '1-Feb-15', 'DD-MON-YY' ) AS start_date,  TO_DATE( '10-Feb-15', 'DD-MON-YY' ) AS end_date, 5 AS column_A FROM DUAL
UNION ALL SELECT TO_DATE( '11-Feb-15', 'DD-MON-YY' ) AS start_date,  TO_DATE( '12-Feb-15', 'DD-MON-YY' ) AS end_date, 6 AS column_A FROM DUAL;

CREATE TABLE B AS
          SELECT TO_DATE( '1-Jan-15', 'DD-MON-YY' ) AS start_date,  TO_DATE( '20-Jan-15', 'DD-MON-YY' ) AS end_date, 4 AS column_B FROM DUAL
UNION ALL SELECT TO_DATE( '21-Jan-15', 'DD-MON-YY' ) AS start_date,  TO_DATE( '31-Jan-15', 'DD-MON-YY' ) AS end_date, 5 AS column_B FROM DUAL
UNION ALL SELECT TO_DATE( '1-Feb-15', 'DD-MON-YY' ) AS start_date,  TO_DATE( '5-Feb-15', 'DD-MON-YY' ) AS end_date, 6 AS column_B FROM DUAL
UNION ALL SELECT TO_DATE( '6-Feb-15', 'DD-MON-YY' ) AS start_date,  TO_DATE( '12-Feb-15', 'DD-MON-YY' ) AS end_date, 7 AS column_B FROM DUAL;

查询1

WITH all_dates AS (
  SELECT start_date AS "date", 0 AS isEnd FROM A
  UNION
  SELECT end_date, 1 FROM A
  UNION
  SELECT start_date, 0 FROM B
  UNION
  SELECT end_date, 1 FROM B
  ORDER BY 1, 2
),
all_paired_dates AS (
  SELECT "date" AS start_date,
         LEAD( "date" ) OVER ( ORDER BY "date" ) AS end_date
  FROM   all_dates
)
SELECT d.start_date,
       d.end_date,
       a.column_A,
       b.column_B
FROM   all_paired_dates d
       INNER JOIN A
       ON (    d.start_date BETWEEN a.start_date AND a.end_date
           AND d.end_date   BETWEEN a.start_date AND a.end_date )
       INNER JOIN B
       ON (    d.start_date BETWEEN b.start_date AND b.end_date
           AND d.end_date   BETWEEN b.start_date AND b.end_date )

<强> Results

|                 START_DATE |                   END_DATE | COLUMN_A | COLUMN_B |
|----------------------------|----------------------------|----------|----------|
|  January, 01 2015 00:00:00 |  January, 10 2015 00:00:00 |        2 |        4 |
|  January, 11 2015 00:00:00 |  January, 20 2015 00:00:00 |        3 |        4 |
|  January, 21 2015 00:00:00 |  January, 21 2015 00:00:00 |        3 |        5 |
|  January, 22 2015 00:00:00 |  January, 31 2015 00:00:00 |        4 |        5 |
| February, 01 2015 00:00:00 | February, 05 2015 00:00:00 |        5 |        6 |
| February, 06 2015 00:00:00 | February, 10 2015 00:00:00 |        5 |        7 |
| February, 11 2015 00:00:00 | February, 12 2015 00:00:00 |        6 |        7 |