简体   繁体   中英

SQL Query - join on less than or equal date

I'm having trouble figuring out how to format the following query. I want to take Table A and left join Table B . Not only on a.country = b.country but also on the max start_date that is less than equal to join_date .

Postgres (Redshift)

Table A

  ID      join_date     country          email     
 -----    -----------   ---------        ------
  124     '2013-10-03'     US         john@doe.com
  423     '2013-04-21'     CA         bob@doe.com
  412     '2013-03-30'     US         test@test.com

Table B

  start_date        country          joined_from
-------------     -----------       --------------
'2013-08-21'          US                google
'2014-01-02'          CA                yahoo
'2013-03-02'          CA                microsoft
'2013-02-10'          US                facebook
'2013-09-01'          US                yahoo

End Result

  ID     join_date     country      email         start_date     joined_from
------  -----------    ---------   ---------     ------------   -------------
 124    '2013-10-03'     US        john@doe.com   '2013-09-01'     yahoo
 423    '2013-04-21'     CA        bob@doe.com    '2013-03-02'    microsoft
 412    '2013-03-30'     US        test@test.com  '2013-02-10'    facebook

Not pretty, but putting your condition directly in the JOIN should work:

SELECT a.ID, a.join_date, a.country, a.email, b.start_date, b.joined_from
FROM a LEFT JOIN b ON a.country = b.country 
      AND b.start_date = (
          SELECT MAX(start_date) FROM b b2 
          WHERE b2.country = a.country AND b2.start_date <= a.join_date
      );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM