如何GROUP BY连续数据(在这种情况下的日期)

时间:2013-04-26 06:25:31

标签: mysql sql group-by

我有一个products表和一个sales表,记录每个日期期间给定产品的销售数量。当然,并非所有产品每天都有销售。

我需要生成一份报告,告诉我产品有多少连续天销售(从最近日期到过去)以及仅在这些日期内销售的商品数量。

我想告诉你到目前为止我尝试了多少事情,但唯一成功(缓慢,递归)的是我的应用程序内部的解决方案,而不是SQL内部,这就是我想要的。

我也曾在SO上浏览了几个类似的问题,但我没有找到一个能让我清楚了解我真正需要的问题。

我已经设置SQLFiddle here来向您展示我正在谈论的内容。在那里你会看到我能想到的唯一一个查询,它没有给我我需要的结果。我还在那里添加了评论,显示了查询结果应该是什么。

我希望有人知道如何实现这一目标。提前感谢您的任何意见!

旧金山

3 个答案:

答案 0 :(得分:3)

http://sqlfiddle.com/#!2/20108/1

这是执行作业的商店程序

CREATE PROCEDURE myProc()
BEGIN
    -- Drop and create the temp table
    DROP TABLE IF EXISTS reached;
    CREATE TABLE reached (
    sku CHAR(32) PRIMARY KEY,
    record_date date,
    nb int,
    total int)
   ENGINE=HEAP;

-- Initial insert, the starting point is the MAX sales record_date of each product
INSERT INTO reached 
SELECT products.sku, max(sales.record_date), 0, 0
FROM products
join sales on sales.sku = products.sku
group by products.sku;

-- loop until there is no more updated rows
iterloop: LOOP
    -- Update the temptable with the values of the date - 1 row if found
    update reached
    join sales on sales.sku=reached.sku and sales.record_date=reached.record_date
    set reached.record_date = reached.record_date - INTERVAL 1 day, 
        reached.nb=reached.nb+1, 
        reached.total=reached.total + sales.items;

    -- If no more rows are updated it means we hit the most longest days_sold
    IF ROW_COUNT() = 0 THEN
        LEAVE iterloop;
    END IF;
END LOOP iterloop;

-- select the results of the temp table
SELECT products.sku, products.title, products.price, reached.total as sales, reached.nb as days_sold 
from reached
join products on products.sku=reached.sku;

END//

然后你只需做

call myProc()

答案 1 :(得分:3)

纯SQL中没有存储过程的解决方案:Fiddle

SELECT sku
     , COUNT(1) AS consecutive_days
     , SUM(items) AS items
FROM
(
  SELECT sku
       , items
       -- generate a new guid for each group of consecutive date
       -- ie : starting with day_before is null
       , @guid := IF(@sku = sku and day_before IS NULL, UUID(), @guid) AS uuid
       , @sku := sku AS dummy_sku
  FROM 
  (
    SELECT currents.sku
         , befores.record_date as day_before
         , currents.items
    FROM sales currents
      LEFT JOIN sales befores 
        ON currents.sku = befores.sku 
        AND currents.record_date = befores.record_date + INTERVAL 1 DAY
    ORDER BY currents.sku, currents.record_date
  )  AS main_join
    CROSS JOIN (SELECT @sku:=0) foo_sku
    CROSS JOIN (SELECT @guid:=UUID()) foo_guid
) AS result_to_group
GROUP BY uuid, sku

查询真的不那么难。通过cross join (SELECT @type:=0) type声明变量。然后在选择中,您可以逐行设置变量值。模拟Rank函数是必要的。

答案 2 :(得分:-1)

select
  p.*,
  sum(s.items) sales,
  count(s.record_date) days_sold
from
  products p
join
  sales s
  on
  s.sku = p.sku
where record_date between '2013-04-18 00:00:00' and '2013-04-26 00:00:00'
group by sku;