简体   繁体   中英

SQL Query for Inactive Users

I have table which tracks user activity in a game. Activity information is stored into category and sub-category fields. For every activity that the user performs, a record gets created in the this table. Now I want to find the users who are inactive for the last 30 days.

Table Columns

  • ID
  • Username
  • Group_name
  • Activity_Category
  • Activity_Subcategory
  • Activity_Date

Please let me know if more information is required.

Use aggregation:

select user_name
from t
group by user_name
having max(activity_date) <= current_date - interval '30 day';

Note that date/time functions are highly database specific. So this exact syntax may not work in your database, but the idea should work.

If you have a separate table of users, it is probably faster to use not exists :

select u.*
from users u
where not exists (select 1
                  from activity a
                  where a.username = u.username and
                        a.activity_date <= current_date - interval '30 day'
                 );

No activity within the last 30 days means the latest activity was over 30 days ago:

select user_name, max(activity_date)
from tab
group by username
having max(activity_date) < current_date - interval '30' day;

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