【问题标题】:Query to get list of customers whose orders are less than the previous order查询获得订单少于上一个订单的客户列表
【发布时间】:2016-10-12 23:31:54
【问题描述】:

我正在尝试编写一个 sql 来获取总订购量始终小于前一个订单的客户列表。如第 n 阶总数量小于 n-1 阶总数量等

用于创建和填充表的 SQL

create table orders (order_id int, customer_id varchar(5), order_date date, product_id varchar(5), quantity int);

Insert into orders values(01,'C1','2000-01-01','P1',10);
Insert into orders values(02,'C2','2002-01-01','P2',15);
Insert into orders values(03,'C3','2002-04-01','P3',17);
Insert into orders values(04,'C4','2003-04-01','P1',20);
Insert into orders values(05,'C4','2006-01-01','P2',1);
Insert into orders values(06,'C1','2006-05-01','P5',7);

我假设我需要根据 Order_id 序列号编写一个过程和 LOOP。一旦进入循环,我需要选择与 order_id 对应的 product_id 和数量 Q。然后检查对于相同的 order_id 是否有任何其他数量 Q1 小于 Q。如果是,那么我打印 customer_id。 如果不是,则 order_id 移动到下一个 order_id。

我不知道如何实现我检查数量列以检查是否有任何其他数量Q1

请澄清

【问题讨论】:

  • 输出应该是什么样子的?
  • OUTPUT 将是客户列表。 C1 C4 由于这两个客户的订单都少于 C1 之前的订单 (7
  • 您只想查看客户的最后一个订单和倒数第二个订单吗?还是您通常在寻找在某个时间的订单少于之前的订单的客户? (后者可以通过一个简单的EXISTS 子句来实现。)

标签: mysql sql database stored-procedures data-warehouse


【解决方案1】:

您应该根据 order_date 计算每个 customer_id 的 row_numbers。然后,您必须将每个客户的第 n 行和第 n-1 行连接起来,并检查他们是否有至少一个数量少于前一个订单的订单。

SQL Fiddle

select t1.customer_id
from (select o.*,
      @rn:=if(@previous=customer_id,@rn,0) + 1 as rownum,
      @previous:=customer_id
      from orders o, (select @rn:=0,@previous:=NULL) t
      order by customer_id,order_date) t1
join (select o.*,
      @rn:=if(@previous=customer_id,@rn,0) + 1 as rownum,
      @previous:=customer_id
      from orders o
      order by customer_id,order_date) t2 
on t1.customer_id=t2.customer_id and t1.rownum=t2.rownum-1
group by t1.customer_id
having count(case when t2.quantity < t1.quantity then 1 end) >= 1

【讨论】:

    【解决方案2】:

    一种方法使用相关子查询:

    select o.*
    from (select o.*,
                 (select o2.quantity
                  from orders o2
                  where o2.customer_id = o.customer_id and
                        o2.product_id = o.product_id and
                        o2.order_id < o.order_id
                  order by o2.order_id desc
                  limit 1
                 ) prev_quantity
          from orders o
         ) o
    where o.prev_quantity > o.quantity;
    

    【讨论】:

    • 不起作用。我尝试将 customerid 更新为 customer_id,并进行其他更新以检查是否提供了任何记录,但仍然失败
    • @user1940212 。 . .它是如何“不工作”的?这是非常模糊的。
    【解决方案3】:

    我相信一个简单的连接本身可以为您做到这一点,以及一个嵌入式查询,以根据日期识别要连接的正确行。

    SELECT o1.customer_id, o2.order_date, o1.quantity - o2.quantity AS quantity_less
    FROM orders o1
    INNER JOIN orders o2 ON o2.customer_id = o1.customer_id AND
                            o2.order_date = (SELECT MIN(order_date) FROM orders
                                             WHERE order_date > o1.order_date AND customer_id = o1.customer_id)
    WHERE o2.quantity < o1.quantity
    

    【讨论】:

      【解决方案4】:
      SELECT Customer_ID
      FROM
      (
      SELECT c.customer_id,c.order_date,c.quantity,p.quantity AS PreviousQuantity FROM orders c left join orders p ON c.customer_id = p.customer_id AND c.order_date> p.order_date
      WHERE c.quantity< p.quantity AND c.order_date>p.order_date
      )A
      EXCEPT
      SELECT Customer_ID
      FROM
      (
      SELECT c.customer_id,c.order_date,c.quantity,p.quantity AS PreviousQuantity FROM orders c left join orders p ON c.customer_id = p.customer_id AND c.order_date> p.order_date
      WHERE c.quantity> p.quantity  AND c.order_date>p.order_date
      )B
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-11-16
        • 2019-12-20
        • 1970-01-01
        • 1970-01-01
        • 2020-08-29
        • 2022-06-15
        • 1970-01-01
        相关资源
        最近更新 更多