【问题标题】:Count with 2 Group by(s) without subquery/CTE计数 2 Group by(s) 没有子查询/CTE
【发布时间】:2020-12-22 07:27:31
【问题描述】:

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

表格:客户

所需的输出:

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

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 查询。有没有办法在单个查询中获得相同的输出?

【问题讨论】:

  • 用您正在使用的数据库标记您的问题。

标签: sql group-by count subquery sql-date-functions


【解决方案1】:

你可以试试下面的-

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多