Joining missing dates from calendar table [duplicate]

与世无争的帅哥 提交于 2021-01-28 04:04:06

问题


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:

  • Join to Calendar Table - 5 Business Days

  • What's the difference between INNER JOIN, LEFT JOIN, RIGHT JOIN and FULL JOIN?

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.


回答1:


You need left join rather than right join or you may also change the position of tables

SELECT sum(steps), date(from_unixtime(td.db_date)) as stepdate
FROM time_dimension td  
LEFT JOIN userdata u
     ON date(from_unixtime(u.in_date)) = td.db_date 
WHERE user_id = 8
GROUP BY date(from_unixtime(td.db_date))
ORDER BY stepdate DESC;

However, this assumes time_dimension table treating as calender table.



来源:https://stackoverflow.com/questions/50137130/joining-missing-dates-from-calendar-table

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!