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

Posted

技术标签:

【中文标题】如何在一年多的时间内找到一天内订单超过 1 个的客户【英文标题】:How to find Customers with more than 1 order in a day over a year 【发布时间】:2021-02-27 17:20:39 【问题描述】:

我有一个查询,用于查找在一天内完成了 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.customer_number, c.first, c.last, c.address, c.apartment, c.address2, c.city, c.state, c.zip,
             t.customer_number, t.first, t.last, t.Address, t.apartment, t.Address2, t.city, t.state, t.zip 
    HAVING COUNT(c.customer_number) > 1

我很难想出如何用多个 c.customer_number 订单吸引一位客户。

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

提前致谢。

【问题讨论】:

如果您要对所有这些列进行分组,我怀疑COUNT 表达式的返回值是否会超过1 请与示例数据和期望的结果共享表结构。 请解释您所说的“一年多于一天的订单”是什么意思。是不是超过365个订单?那一天有两个订单吗? 为什么运输与您的回答相关?为什么要加入客户两次?我建议您不要 - 从针对 Order_Table 的简单查询重新开始 - 忽略对客户信息的需求。现在 - 似乎您需要在客户/日期的基础上计算(提示)一些东西作为开始。如果可行,则(或可能不)添加额外的运输标准(或者与 行集无关?)。 【参考方案1】:

如果要计算特定客户编号的订单,请从 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.customer_number, c.first, c.last, c.address, c.apartment, 
c.address2, c.city, c.state, c.zip, t.customer_number, t.first, t.last, t.Address, 
t.apartment, t.Address2, t.city, t.state, t.zip 
HAVING COUNT(c.order_number) > 1

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

【讨论】:

以上是关于如何在一年多的时间内找到一天内订单超过 1 个的客户的主要内容,如果未能解决你的问题,请参考以下文章

拆分数据库的技巧

Mongoose/MongoDB 获取一天内查看次数最多的文章

如何在一天内的特定时间段内应用累积python函数,然后从第二天开始重置

在一天内为实体生成唯一的序列号

如何在一天内为你的实验室做一个网站

每周出勤总结一次