Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Snowflake – Left Join not working as expected

I’m looking to reconcile dates between two tables. In other words, I’m looking for where the date column is NULL from the right table.

The first query generates a table with sequence of dates between 2017-2022:

    select -1 + row_number() over(order by 0) i, start_date + i generated_date 
from (select '2017-01-01'::date start_date, '2022-12-31'::date end_date)
join table(generator(rowcount => 10000 )) x
qualify i < 1 + end_date - start_date

Sample of the table below:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I Generate_Date
0 2021-01-01
1 2017-01-02

The second query generates a tables generates with a date along with other items.

select distinct date
from table 
where i.id = id

Sample of the table below:

ID Date
ID1 2021-01-01
ID2 2017-01-02

I join both queries:

WITH calendar_table as (
select -1 + row_number() over(order by 0) i, start_date + i generated_date 
from (select '2017-01-01'::date start_date, '2022-12-31'::date end_date)
join table(generator(rowcount => 10000 )) x
qualify i < 1 + end_date - start_date)

select distinct t.generated_date, i.date
from calendar_table t 
left join table i on t.date = i.date
where i.id = 'id'
order by t.generated_date desc

I would expect the result to show this:

Generated_date Date
2021-05-02 2021-05-02
2021-05-03 NULL

However, this is what appears. I thought a left join is supposed to bring back everything from the FROM clause. I know that 2021-05-03 exist in the left table. Can I please get suggestions on how to fix this?

Generated_date Date
2021-05-01 2021-05-01
2021-05-02 2021-05-02
2021-05-04 2021-05-04
2021-05-05 2021-05-05

>Solution :

When performing LEFT JOIN and later using WHERE with column from right part causes the query to behave as INNER JOIN:

from calendar_table t 
left join table i 
  on t.date = i.date
where i.id = 'id'    -- here

behaves as:

from calendar_table t 
inner join table i 
  on t.date = i.date
where i.id = 'id'    -- here

The i.id = '...' condition should be moved either as subquery or ON clause:

-- a)
from calendar_table t 
left join table i 
  on t.date = i.date
 and i.id = 'id'    

-- b)
from calendar_table t 
left join (SELECT * FROM table WHERE id = 'id') AS i 
  on t.date = i.date  
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading