使用2个分组依据进行计数,而无需子查询/ CTE

问题描述

-每月有多少客户下订单?

表格:客户

enter image description here

所需的输出

enter image description here

提供所需输出的我的代码

with abc as (select concat(year(order_date),'/',month(order_date)) "date",customer_id
from customer
group by 1,2
order by 1)

select date,count(*) "customers_who_ordered"
from abc
group by 1;

我不需要子查询或CTE查询。有没有办法可以在单个查询中获得相同的输出

解决方法

您可以尝试以下-

select concat(year(order_date),'/',month(order_date)) "date",count(distinct customer_id)
from customer
group by concat(year(order_date),month(order_date))