with cte as ( select uid,count(*) as times, count(distinct login_date) as days, max(login_date) as rec_date, max(max(login_date))over() as cur_date from user_login_tb group by uid ) select uid,days,times from cte where (times>=4 or days>=3) and datediff(cur_date,rec_date) >29 order by days desc ,times desc