Postgres重叠日期范围的视图

时间:2018-05-12 04:17:07

标签: sql postgresql

我有三张桌子

Table 1
start_date    end_date     val
01-01-2000     31-01-2000   APPLE
01-02-2000                  ORANGE

table 2
start_date    end_date     val
01-01-2000     15-01-2000   TOMATO
16-01-2000                  LETTUCE

table 3
start_date    end_date     val
01-12-1999                  CAR

现在我可以说表格中有以下内容:

start_date     end_date      val_table_1    val_table_2    val_table_3
01-12-1999     31-12-1999       null           null           CAR
01-01-2000     15-01-2000       APPLE          TOMATO         CAR
16-01-2000     31-01-2000       APPLE          LETTUCE        CAR
01-02-2000                      ORANGE         LETTUCE        CAR

我希望将上述三个表放入具有最小/最大日期的视图中。看起来像这样:

IBigData

1 个答案:

答案 0 :(得分:2)

我能够通过下面的查询实现想要的结果。也可以在SQL Fiddle处获得演示。如果创建一个视图,则省略最后的order by子句,此处包含它只是为了明智地呈现结果。

PostgreSQL 9.6架构设置

CREATE TABLE Table1
    ("start_date" timestamp, "end_date" timestamp, "val" varchar(20))
;

INSERT INTO Table1
    ("start_date", "end_date", "val")
VALUES
    ('2000-01-01 00:00:00', '2000-01-31', 'APPLE'),
    ('2000-02-01 00:00:00', NULL, 'ORANGE')
;

CREATE TABLE Table2
    ("start_date" timestamp, "end_date" timestamp, "val" varchar(20))
;

INSERT INTO Table2
    ("start_date", "end_date", "val")
VALUES
    ('2000-01-01', '2000-01-15', 'TOMATO'),
    ('2000-01-16', NULL, 'LETTUCE')
;

CREATE TABLE Table3
    ("start_date" timestamp, "end_date" timestamp, "val" varchar(3))
;

INSERT INTO Table3
    ("start_date", "end_date", "val")
VALUES
    ('1999-01-12 00:00:00', NULL, 'CAR')
;

查询1

with ends as (
          select end_date from Table1 where end_date is not null union
          select end_date from Table2 where end_date is not null union
          select end_date from Table3 where end_date is not null
          )
select
       d.start_date
     , least(e.end_date, lead(d.start_date,1) over(order by d.start_date) - INTERVAL '1 DAY') as end_date
     , table1.val as t1_val
     , table2.val as t2_val
     , table3.val as t3_val
from (
    select start_date from Table1 union
    select start_date from Table2 union
    select start_date from Table3
    ) d
left join lateral (
  select ends.end_date from ends where ends.end_date > d.start_date
  order by end_date
  limit 1
  ) e on true
left join table1 on d.start_date between table1.start_date and coalesce(table1.end_date,current_date)
left join table2 on d.start_date between table2.start_date and coalesce(table2.end_date,current_date)
left join table3 on d.start_date between table3.start_date and coalesce(table3.end_date,current_date)
order by
       start_date, end_date

<强> Results

|           start_date |             end_date | t1_val |  t2_val | t3_val |
|----------------------|----------------------|--------|---------|--------|
| 1999-01-12T00:00:00Z | 1999-12-31T00:00:00Z | (null) |  (null) |    CAR |
| 2000-01-01T00:00:00Z | 2000-01-15T00:00:00Z |  APPLE |  TOMATO |    CAR |
| 2000-01-16T00:00:00Z | 2000-01-31T00:00:00Z |  APPLE | LETTUCE |    CAR |
| 2000-02-01T00:00:00Z |               (null) | ORANGE | LETTUCE |    CAR |