如何编写SQL查询以查找每月至少购买两次的客户
我有一个表,列有order\u id、customer\u id、order\u date和Amount如何编写SQL查询以查找每月至少购买两次的客户,sql,sql-server,Sql,Sql Server,我有一个表,列有order\u id、customer\u id、order\u date和Amount 如何找到从1月到12月每月至少订购2次SQL的客户?我想您正在寻找这样的客户 select order_date_year, customer_id from ( select year(order_date) as order_date_year, month(order_date) as order_date_month,
如何找到从1月到12月每月至少订购2次SQL的客户?我想您正在寻找这样的客户
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
我想你在找这样的东西
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
标记DBMS,即MySQL、SQL Server等。。您正在使用的。每个月或每个月..?标准1:客户应在1月至12月期间每月购买标准2:对于每个月,客户应至少交易两次标记DBMS,即MySQL、SQL Server等。。您正在使用的。每个月或每个月..?标准1:客户应在1月至12月期间每月购买标准2:对于每个月,客户应至少交易两次