计算新用户订阅量MySQL

Calculate new users subscription amount MySQL

我有一个数据集,我需要在其中找出新订阅者的收入。

这些订阅者根据订阅情况按周或按月付费。

唯一标识符是 "customer",数据处于时间戳级别,但我希望它按月汇总。

现在,对于每个月,我们只需要找出新订阅者的收入。 基本上,假设客户正在 monthly/weekly 订阅,我们只希望在此处计算他们的首次付款。

这是一个样本数据集,

created            customer                  amount
16-Feb-18 14:03:55  cus_BwcisIF1YR1UlD  33300
16-Feb-18 14:28:13  cus_BpLsCvjuubYZAe  156250
15-Feb-18 19:19:14  cus_C3vT6uVBqJC1wz  50000
14-Feb-18 23:00:24  cus_BME5vNeXAeZSN2  162375
9-Feb-18 14:27:26   cus_BpLsCvjuubYZAe  156250

...等等...

这是最终需要的输出

yearmonth new_amount
Jan - 2018   100000
Feb - 2018   2000
Dec - 2017   100002

这需要在MySQL界面中完成。

基本上,您想将数据筛选到第一个客户。执行此操作的一种方法涉及相关子查询。

其余只是按年和月汇总。所以,总的来说查询并没有那么复杂,但它确实由两个不同的部分组成:

select year(created) as yyyy, month(created) as mm,
       count(*) as num_news,
       sum(amount) as amount_news
from t
where t.created = (select min(t2.created)
                   from t t2
                   where t2.customer = t.customer
                  )
group by yyyy, mm
   We can have sql subquery for only the 1st payment of the new customer with   
   amount for every month and year

   The query is as follows

  SELECT month(created) as mm,year(created) as yyyy,
  sum(amount) as new_amount
  FROM t
  WHERE t.created=(select min(t2.created) from t t2 where 
    t2.customer=t.customer)