I have the date stored in a int field and when I select it I use
strftime('%Y-%m-%d', table.date/1000, 'unixepoch') as date
I need to have data for the past 30 days for charting purposes, this means that I need to have in my final query something like:
2017-01-18 ,2
2017-01-17, 0
2017-01-16, 0
2017-01-15, 1
In my table I only have rows for 18th and 15th Jan. So running a query against it, will only return rows for the days that exists,
2017-01-18 ,2
2017-01-15, 1
However, I need to have rows, with 0 value, for the past 30 days that don't have any values, as a final result. In order to have this, I need a way to union my data with a query that returns
now-1 day, 0
now-2 days ,0
now-3 days, 0 ....
for the past 30 days. In this way, when I combine the 2 queries, will results in the desired results.
Using from https://stackoverflow.com/a/32987070/379865
I managed to get the data
WITH RECURSIVE dates(date) AS (
VALUES(date('now'))
UNION ALL
SELECT date(date, '-1 day')
FROM dates
limit 30
)
SELECT date, 0 as value FROM dates;
However I don't know how to union it with my other query since WITH does not seem to go as part of a union all