如何解决如何在一年多的一天内找到超过 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
分享有关各个表结构和预期输出的更多详细信息以获得更具体的答案。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。