I have table B
that looks like the following:
DATE | STORE | PRODUCT | UNITS_SOLD |
-------------------------------------------
2020-01-01 | 1 | Beans | 10 |
2020-01-03 | 1 | Beans | 5 |
2020-01-01 | 2 | Carrots | 6 |
... | ... | ... | ... |
The important point being that there is not a row present for every date.
I'd like to essentially 'fill in' the missing dates and have null values in the other columns. I've been trying to do this by left joining with a table A
that is just a list of continuous calendar dates, i.e.
DATE |
------------
2019-01-01 |
2019-01-02 |
2019-01-03 |
...
The SQL I have written for this is (and I tried to keep it simple by limiting to one store and one product):
SELECT
A.DATE
, B.STORE
, B.PRODUCT
, B.UNITS_SOLD
FROM
B AS SALES
LEFT JOIN
A AS DATES ON
DATES.DATE = SALES.DATE
WHERE
B.STORE = 1
AND B.PRODUCT = 'Beans'
But it's just returning rows where the date is present in B
. I've tried moving the conditions in the WHERE clause to inside the join too, and adding '...OR IS NULL'.
What I'd like is:
DATE | STORE | PRODUCT | UNITS_SOLD |
-------------------------------------------
2020-01-01 | 1 | Beans | 10 |
2020-01-02 | null | null | null |
2020-01-03 | 1 | Beans | 5 |
... | ... | ... | ... |
What am I missing?