select count( distinct a.userid),sum(b.money) from (select userid,date from tableA where date>='20161201' and date<'20170101') a join tableB b on a.userid=b.userid and datediff(b.date,a.date)<=302
select (case when money<500 then '(0,500)' when money>=5000 then '[5000,max)' else '[500,5000)' end) 'category', count(distinct b.userid),sum(b.money) from tableB b where date>'20160101' group by (case when money<500 then '(0,500)' when money>=5000 then '[5000,max)' else '[500,5000)' end)
select
count(distinct b.uid) paycn,
sum(b.money) gmv
from (
select userid,date
from tableA
where date rlike '2016-12-%'
) a
left join tableB b
on a.userid = b.userid and date_add(a.date,interval 30 day) >= b.date
;
select
case when gmv > 0 and gmv < 500 then gmv_0_500
when gmv >= 500 and gmv < 5000 then gmv_500_5000
when gmv >= 5000 then gmv_5000
else null end as gmv_range,
count(userid) as cn,
sum(gmv) as sumgmv
from (
select
userid,
sum(money) as gmv
from tableB
where date >= '2016-01-01'
group by userid
)
group by
case when gmv > 0 and gmv < 500 then gmv_0_500
when gmv >= 500 and gmv < 5000 then gmv_500_5000
when gmv >= 5000 then gmv_5000
else null end
Question B.
select cat,count(userid) as '付费人数',sum(money) as '付费金额' from (select *, case when money<500 then '(0,500)' when money>=500 and money<5000 then '[500,5000)' when money>=5000 then '[5000,)' end as cat from (select userid, sum(money) as 'money' from tableB where year(date)>=2016 group by userid ) t) t1 group by cat