postgresql - Join 2 tables where two sets of numbers overlap within the joining columns -


i need join 2 tables postgresql 2 sets of numbers overlap within joining columns.


the image below explains - needing take table of congresspeople , party affiliation , join table of districts (based on when districts drawn or redrawn). result rows show dates district, state , congressperson same. wherever there dates of district known , congressperson dates unknown, dates known district filled portion, , dates congressperson left blank - , vice versa.

enter image description here


for example, first rows in tables:


congressperson table:

arkansas, district 5, republican: 1940-1945 

district table:

arkansas, district 5: 1942-1963 

results in following combinations (start_comb , end_comb):

1940-1942 1942-1945 

and combination district unknown (1940-1942), district dates left blank.

the final set of date columns (gray) combinations district (this super easy).


in case you're wondering for, creating animated map, kind of this, congressional districts on time: https://www.youtube.com/watch?v=vqdyn04vtf8

i'll end there map every known district, there known or unknown party.


haven't got far, did:

select * congressperson join districts on start_dist between start_cong , end_cong district.a = district.b or end_dist between start_cong , start_dist or start_cong = start_dist or end_cong= end_dist; 

the idea make list of unique dates both tables first. each such date find next date (in particular case dates grouped state, district, , next date looked particular state, district).
have list of ranges looking for. can join (for paticular task left join) other tables required conditions:

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 not null start_comb end final_start,     case when d.district not null end_comb end final_end (     dates (         select             *         (             select                  c.state,                 c.district,                 start_cong date             congressperson c             union              select                 c.state,                 c.district,                  end_cong             congressperson c             union              select                  d.state,                 d.district,                 start_dist             district d              union              select                 d.state,                 d.district,                  end_dist             district d          ) dates         group              state,             district,             date         order              state,             district,                 date     )      select         dates.state,         dates.district,         dates.date start_comb,     (select          d.date               dates d             d.state = dates.state ,         d.district = dates.district ,         d.date > dates.date     order          d.date     limit 1     ) end_comb              dates) r left join congressperson c on                              c.state = r.state ,                             c.district = r.district ,                             start_comb between c.start_cong , c.end_cong ,                              end_comb between c.start_cong , c.end_cong left join district d on                          d.state = r.state ,                         d.district = r.district ,                         start_comb between d.start_dist , d.end_dist ,                          end_comb between d.start_dist , d.end_dist     end_comb not null  order      r.state, coalesce(c.district, d.district), start_comb, end_comb, start_cong, end_cong 

Comments

Popular posts from this blog

c# - ajax - How to receive data both html and json from server? -

swift - Button on Table View Cell connected to local function -