Reputation: 999
I have the following table:
| variable | varDate | match |
|----------|------------|-------|
| var1 | 15/02/2020 | 98 |
| var1 | 16/02/2020 | 99 |
| var1 | 17/02/2020 | 97 |
| var2 | 15/02/2020 | 95 |
I would like to populate var2, for the dates 16/02/2020 and 17/02/2020 (those that are missing, and if null, populate with the value 100).
I have a table that contains the last three days in a table, that looks like so:
| date |
|------------|
| 15/02/2020 |
| 16/02/2020 |
| 17/02/2020 |
My attempt was to use a left join on varDate = date, however this didn't produce the result I expected like below:
| variable | date | match |
|----------|------------|-------|
| var1 | 15/02/2020 | 98 |
| var1 | 16/02/2020 | 99 |
| var1 | 17/02/2020 | 97 |
| var2 | 15/02/2020 | 95 |
| var2 | 16/02/2020 | 100 |
| var2 | 17/02/2020 | 100 |
Upvotes: 0
Views: 74
Reputation: 222432
Consider:
select
v.variable,
d.date,
coalesce(t.match, 100) match
from (select distinct variable from mytable) v
cross join mydates d
left join mytable t
on t.varDate = d.date
and t.variable = v.variable
This works by first cross join
ing the distinct values of variable
available in the table with the table that references the dates (I called it mydates
). This generates all possible combinations of dates and variables.
Then, we bring in the original table (I assumed mytable
) with a left join
. Finally, we use coalesce()
to assign a default value for missing match
es.
Ideally, you should have a reference table for the variable
s as well, which would avoid the need for the distinct
subquery:
select
v.variable,
d.date,
coalesce(t.match, 100) match
from myvariables v
cross join mydates d
left join mytable t
on t.varDate = d.date
and t.variable = v.variable
Upvotes: 2