简体   繁体   English

如何编写SQL查询来找出一年中每月至少购买两次的客户

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

I have a table with columns order_id, customer_id, order_date and Amount. 我有一个表,其中包含列order_id,customer_id,order_date和金额。

在此处输入图片说明

How to find the customers who have ordered at least 2 times each month of the year in SQL (from Jan to Dec)? 如何查找每年每个月至少订购2次SQL(从1月到12月)的客户?

I think you are looking for something like this 我想你正在寻找这样的东西

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.

相关问题 SQL:筛选出过去一年未购买的客户 - SQL: Filtering out customers who have not purchased over the past year SQL 如何查询自开始采购以来每年至少进行一次采购的客户? - SQL How to query customers who have made a purchase at least once a year since they first start purchasing? SQL查询:查找在一年中购买了多本书的客户 - SQL Query: find customers who purchased more than one book in a given year Postgresql - 查询连续3个月每月至少购买一次杂货的客户列表 - Postgresql - Query the List of customers who have bought grocery at least once every month for 3 continuous months SQL 查询获得在两个不同季度购买过相同产品的客户 - 日期范围 - SQL query to get customers who have purchased same product in two different quarters - Date Ranges SQL Query查找在同一天注册和购买的客户 - SQL Query to find customers who registered and purchased on the same day SQL 查询获取购买其他客户购买产品的客户 - SQL query to get customers who purchased product purchased by other customers 编写查询以查找至少租用一部电影且属于阿灵顿市的客户的全名 - Write a query to find the full names of those customers who have rented at least one movie and belong to the city Arlington SQL - 查找去年没有订购的客户以及他们订购了什么 - SQL - Find customers who have not ordered in the last year and what they ordered SQL查找每月至少订购一次的客户 - SQL to find customers that have ordered at least once a month
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM