Home > front end >  How to make LEFT JOIN to rows having max date in BQ?
How to make LEFT JOIN to rows having max date in BQ?

Time:12-07

I have 2 tables in Big Query:

TABLE A

ID Name Date_A field_x field_y field_z
xxx tata 10/11/2021 a 0 1
xxx tata 11/11/2021 a 1 1
zzz tutu 01/11/2021 b 0 1
zzz tutu 05/11/2021 b 1 1
yyy titi 02/11/2021 c 0 1
uuu tata 08/11/2021 d 0 0

TABLE B

ID Name Date_B field_A field_B
xxx tata 13/11/2021 AA BB
zzz tutu 01/11/2021 CC DD
yyy titi 11/11/2021 AA BB
uuu tata 05/11/2021 DD DD

And I would like to link (left join on ID and Name) rows from table B to the max date of table A, to get :

ID Name Date_A field_x field_y field_z field_A field_B
xxx tata 10/11/2021 a 0 1 NULL NULL
xxx tata 11/11/2021 a 1 1 AA BB
zzz tutu 01/11/2021 b 0 1 NULL NULL
zzz tutu 05/11/2021 b 1 1 CC DD
yyy titi 02/11/2021 c 0 1 AA BB
uuu tata 08/11/2021 d 0 0 DD DD

How can I do that in SQL (Big Query) please ? Thanks

CodePudding user response:

I didn't tested it but I think you should left join the b table to a table in which the max date is indicated. Usage of condition pure on the left table is somewhat unusual though from the definition of left join I expect it to work.

select a_ranked.ID, a_ranked.Name, a_ranked.Date_A
     , a_ranked.field_x, a_ranked.field_y, a_ranked.field_z
     , b.field_A, b.field_B
from (
  select a.*, rank() over (partition by ID, Name order by Date_A desc) as r
  from a
) a_ranked
left join b on a_ranked.ID = b.ID and a_ranked.Name = b.Name and a_ranked.r = 1

CodePudding user response:

Consider below approach

select a.*, 
  (if(row_number() over win = 1, b, null)).* except(id, name, date_b)
from table_a a 
left join table_b b 
using(id, name)
window win as (partition by a.id, a.name order by date_a desc)    

if applied to sample data in your question - output is

enter image description here

  • Related