select uid, t2 as month, sum(n4) as coin from( select uid, date_format(t1,'%Y%m') as t2, case when n3 % 7 = 3 then 3 when n3 % 7 = 0 then 7 else 1 end as n4 from( select uid, t1, row_number() over(partition by uid,n2 order by t1) as n3 from ( select uid, t1, date_sub(t1,interval n1 day) as n2 from( ...