我正在寻找一种可能 Not Acceptable 逻辑。
但我的要求是我要根据上个月和当月计算客户数量(NewCustomers、repeatCustomers)
就像我想要的这些数据
DATE NAME
2016-01-01 A
2016-01-01 B
2016-01-01 C
2016-01-05 E
2016-01-05 F
2016-01-25 G
2016-01-25 H
2016-02-25 A
2016-02-25 E
2016-02-10 X
2016-02-11 Y
2016-02-13 F
这样输出
MONTH NewCustomer RepeatCustomer CustomerCount of refernece month (Like here is JAN)
FEB 2 3 7
下个月也一样
有什么建议吗?谢谢 !!
最佳答案
我不知道引用月份是几月,但您可以通过将您第一次看到客户的时间与每个月来访的客户相结合来获得前两列:
select date_format(c.date, '%Y-%m') as yyyymm,
count(distinct c.name) as NumCustomers,
sum(case when date_format(c.date, '%Y-%m') <> date_format(cc.start_date, '%Y-%m')
then 1 else 0
end) as NumRepeatCustomers
from customers c join
(select c.name, min(c.date) as start_date
from customers c
group by c.name
) cc
on c.name = cc.name
group by date_format(c.date, '%Y-%m')
order by yyyymm;
关于mysql - 在 mysql 中按月重复客户计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36218192/