簡體   English   中英

如何編寫SQL查詢來找出一年中每月至少購買兩次的客戶

[英]How to write a SQL query to find out customers who have purchased at least two times every month of the year

我有一個表,其中包含列order_id,customer_id,order_date和金額。

在此處輸入圖片說明

如何查找每年每個月至少訂購2次SQL(從1月到12月)的客戶?

我想你正在尋找這樣的東西

select
    order_date_year,
    customer_id
from 
(
    select  
        year(order_date) as order_date_year,
        month(order_date) as order_date_month,
        customer_id,
        count(*) as number_of_orders
    from
        tableName
    group by
        year(order_date),
        month(order_date),
        customer_id
    having
        count(*) >= 2
) as t
group by
    order_date_year,
    customer_id
having
    count(*) = 12

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM