计数 2 分组依据没有 subquery/CTE

Count with 2 Group by(s) without subquery/CTE

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

Table:客户

期望输出:

我的代码给出了所需的输出:

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))