简体   繁体   中英

SQL join that not in grouped date

I have this user_activity table

user_id   date
1         2020-01-01 08:00:00
2         2020-01-01 08:05:00
1         2020-01-02 08:00:00

and user table

user_id
1
2
3

I want to know the user that don't have activity on all date. like this

date        user_id
2020-01-01  3
2020-01-02  2
2020-01-02  3

I'm trying this but can't figure it out how to grouped it with date

SELECT a.* FROM user_activity a
LEFT OUTER JOIN user u on a.user_id= u.user_id
where a.user_id is null

I already looking for the answer but there is no answer with grouped date

SELECT u.user_id, all_dates.date
FROM user u
CROSS JOIN (SELECT DISTINCT DATE(date) as date FROM user_activity) as all_dates
WHERE NOT EXISTS (
  SELECT 1
  FROM user_activity ua
  WHERE ua.user_id = u.user_id
    AND DATE(ua.date) = all_dates.date
)

->

user_id     date
3           2020-01-01
3           2020-01-02
2           2020-01-02

Also created a fiddle for you to play with

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM