如何在一年多的一天内找到超过 1 个订单的客户

问题描述

我有一个查询,用于查找在一天内下过 2 个或更多订单的客户。我有以下查询

    SELECT h.order_number,h.date,t.customer_number,t.first_name,t.last_name,t.Address,t.city,t.state,c.customer_number,c.first_name,c.last_name,c.address,c.city,c.state     
         FROM Order_Table h
         JOIN Customer c ON h.customer_number = c.customer_number   
         JOIN OrderShipping s ON h.order_number = s.order_number    
         JOIN Customer t ON s.customer_number = t.customer_number 
    WHERE h.date > '2021/01/01' 
    GROUP BY h.date,h.order_number,c.first,c.last,c.apartment,c.address2,c.state,c.zip,t.first,t.last,t.apartment,t.Address2,t.zip 
    HAVING COUNT(c.customer_number) > 1

我很难想出如何通过多个 c.customer_number 订单吸引 1 个客户。

我还想要通过 c.customer_number 订购但运送到 t.customer_number 的订单。这就是我添加 ordrshipping 和第二个客户表的原因。

提前致谢。

解决方法

如果您想计算特定客户编号的订单,请从 group by 子句中删除 order_number:

SELECT h.date,t.customer_number,t.first_name,t.last_name,t.Address,t.city,t.state,c.customer_number,c.first_name,c.last_name,c.address,c.city,c.state
FROM Order_Table h
JOIN Customer c ON h.customer_number = c.customer_number
JOIN OrderShipping s ON h.order_number = s.order_number
JOIN Customer t ON s.customer_number = t.customer_number 
WHERE h.date > '2021/01/01' 
GROUP BY h.date,c.first,c.last,c.apartment,c.address2,c.state,c.zip,t.first,t.last,t.apartment,t.Address2,t.zip 
HAVING COUNT(c.order_number) > 1

分享有关各个表结构和预期输出的更多详细信息以获得更具体的答案。