I have a table with information and dates, which have some missing ones, so I want to join that table with a calendar table to fill missing dates and set values in another column in the same row to null. This is an example:
Steps | Date
10 | 2018-04-30
20 | 2018-04-28
And it want to do the following:
Steps | Date
10 | 2018-04-30
null | 2018-04-29
20 | 2018-04-28
This is what I tried (real query, so you can point out if I'm doing something wrong):
SELECT sum(steps), date(from_unixtime(u.in_date)) as stepdate
FROM userdata u
RIGHT JOIN
time_dimension td
ON date(from_unixtime(u.in_date)) = td.db_date
AND user_id = 8
GROUP BY day(from_unixtime(in_date))
ORDER BY stepdate DESC;
I expected this query to do what I wanted, but it doesn't. The table time_dimension
and its column db_date
have all dates (ranging from 2017-01-01 to 2030-01-01), which is the one I'm trying to join userdata
's in_date
column (which is in unix_time).
Edit: I checked the following questions in SO:
Edit, regarding the duplicate: That question in particular is using intervals and date_add to compare against their table. I am using a calendar table instead to join them. While similar, I don't think they won't have the same solution.
Solution: Thanks to xQBert, who pointed out the mistake:
PROBLEM: Having the group by be on the userdata table as well as the select, you're basically ignoring the time dimension data. There is no 2018-4-29 date in Userdata right (for user 8) Fix the select & group by to source from time dimension data and problem solved.
So, I changed GROUP BY day(from_unixtime(in_date))
to GROUP BY td.db_date
.