加入2个表,其中两组数字在连接列中重叠

时间:2017-09-18 20:58:42

标签: postgresql

我需要使用postgresql连接2个表,其中两组数字在连接列中重叠。

下面的图片解释了这一点 - 我需要拿一张国会议员及其党派的表格,并加入一张地区表(根据地区的绘制或重绘时间)。结果将是显示区,州和国会议员相同日期的行。如果某个地区的日期已知且议员日期不详,则该地区已知的日期将填入该部分,并且会议员的日期将留空 - 反之亦然。

enter image description here

例如,对于表格中的第一行:

国会议员表:

Arkansas, District 5, Republican: 1940-1945

地区表:

Arkansas, District 5: 1942-1963

以下组合的结果(Start_Comb和End_Comb):

1940-1942
1942-1945

对于区域未知的组合(1940-1942),区日期留空。

最后一组日期列(灰色)只是区域的组合(这非常容易)。

如果您想知道这是为了什么,我正在创建一个类似于此的动画地图,但对于国会选区而言: https://www.youtube.com/watch?v=vQDyn04vtf8

我最终会得到一张地图,其中有一张地图,对于每个已知的地区,都有一个已知或未知的派对。

Haven走得很远,这就是我所做的:

SELECT *
FROM congressperson
JOIN districts
ON Start_Dist BETWEEN Start_Cong AND End_Cong
WHERE district.A = district.B
OR End_Dist BETWEEN Start_Cong AND Start_Dist
OR Start_Cong = Start_Dist OR End_Cong= End_Dist;

1 个答案:

答案 0 :(得分:1)

我们的想法是首先从两个表中列出唯一日期。然后,对于每个这样的日期,找到下一个日期(在这个特定情况下,日期按州,区分组,下一个日期查找特定州,区)。
所以现在我们有我们正在寻找的范围列表。现在我们可以根据需要的条件加入(对于这个特定的任务保持加入)其他表:

select
    r.state,
    c.start_cong,
    c.end_cong,
    c.party,
    coalesce(c.district, d.district) district,
    d.start_dist,
    d.end_dist,
    start_comb,
    end_comb,
    case when d.district is not null then start_comb end final_start,
    case when d.district is not null then end_comb end final_end
from (
    with dates as (
        select
            *
        from (
            SELECT 
                c.state,
                c.district,
                start_cong date
            FROM congressperson c
            union 
            SELECT
                c.state,
                c.district, 
                end_cong
            FROM congressperson c
            union 
            SELECT 
                d.state,
                d.district,
                start_dist
            FROM district d 
            union 
            SELECT
                d.state,
                d.district, 
                end_dist
            FROM district d 
        ) DATES
        group by 
            state,
            district,
            date
        order by 
            state,
            district,    
            date
    ) 
    select
        dates.state,
        dates.district,
        dates.date start_comb,
    (select 
        d.date 
    from 
        dates d
    where
        d.state = dates.state and
        d.district = dates.district and
        d.date > dates.date
    order by 
        d.date
    limit 1
    ) end_comb
    from 
        dates) r
left join congressperson c on 
                            c.state = r.state and
                            c.district = r.district and
                            start_comb between c.start_cong and c.end_cong and 
                            end_comb between c.start_cong and c.end_cong
left join district d on 
                        d.state = r.state and
                        d.district = r.district and
                        start_comb between d.start_dist and d.end_dist and 
                        end_comb between d.start_dist and d.end_dist
where
    end_comb is not null 
order by 
    r.state, coalesce(c.district, d.district), start_comb, end_comb, start_cong, end_cong