按月返回新用户数

sczxawaw  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(309)

表orders包含以下列order\u id、order\u date、ship\u date、ship\u mode、customer\u id、country、quantity
我只需要密码。这是我的作品。

SELECT order_date, COUNT (Customer_Id) As new_usercount
FROM (
    SELECT Customer_Id, DATE(MIN(Order_Date)) AS order_date
    FROM Orders
    GROUP BY Customer_Id
) AS ch
GROUP BY order_date

谢谢

nfeuvbwi

nfeuvbwi1#

你的问题几乎是正确的。
在汇总以获取每个客户的最小订单日期后,必须按日期的年和月分组,而不是按日期分组:

SELECT YEAR(order_date) year, 
       MONTH(order_date) month, 
       COUNT(*) new_usercount 
FROM ( 
  SELECT Customer_Id, MIN(Order_Date) order_date 
  FROM Orders 
  GROUP BY Customer_Id 
) t 
GROUP BY YEAR(order_date), MONTH(order_date)

相关问题