MYSQL series (5) electricity providers commonly used indicators inquiry

表:order ,字段:orderid, userid, isPaid, price, paidTime

Table: user, field: userid, sex, birth

1. The number of different single-month statistics

select month(date_format(paidtime, '%Y-%m-%d')), count(distinct userid)
from order
where isPaid = '已支付'
group by month(date_format(paidtime, '%Y-%m-%d'));

2. Statistics March repo rate and the re-purchase rate

Re-purchase rate of 2.1 in March

Re-purchase rate: The number in March to buy more than twice the total number of purchase in March of proportion

The number of purchase in March

select userid, count(userid) as count_userid
from order
where ispaid = '已支付'
and month(date_format(paidtime, '%Y-%m-%d')) = 3
group by userid;

The number of re-buy purchase rate of more than twice = March / March to purchase the number of complex statistical deduplication

select count(t.userid) as 购买人数, count(if(t.count_userid>1, 1, null)) as '复购人数',
       count(if(t.count_userid>1, 1, null)) / count(t.userid) as '复购率'
from (select userid, count(userid) as count_userid
     from order
     where ispaid = '已支付'
     and month(date_format(paidtime, '%Y-%m-%d')) = 3
     group by userid
      ) t

2.2 User repo rate

The use of sub-queries, filter out the second month to buy records of the case, then count

select t1.m, count(t1.userid), count(t2.userid)
from    (select userid), date_format(paidtime, '%Y-%m-01') as m
        from order 
        where ispaid = '已支付'
        group by userid, date_format(paidtime, '%Y-%m-01')
        ) as t1
left join
        (select userid, date_format(paidtime, '%Y-%m-01') as m
        from order
        where ispaid = '已支付'
        group by userid,  date_format(paidtime, '%Y-%m-01')
        ) as t2
on      t1.userid = t2.userid and t1.m = date_sub(t2.m, INTERVAL 1 month)
group by t1.m
  1. Statistics for the first time and last time consumer long distance

    select userid, datediff(max(paidtime), min(paidtime))
    from order
    where ispaid = '已支付'
    group by userid
    having count(1) > 1

Guess you like

Origin www.cnblogs.com/yangjiez/p/12105693.html