mysql行之间的区别

时间:2013-05-10 22:26:17

标签: mysql

我有一个类似这样的mysql表:

date               product ID    sold
-----              ----------    ----
2013-04-20 09:00   ABC           10
2013-04-20 09:00   DEF           15
2013-04-20 09:00   HIJ           15
2013-04-20 10:00   ABC           5
2013-04-20 10:00   DEF           10
2013-04-20 10:00   HIJ           20
and so on..

我想得出每小时销售的每件商品的数量之间的差异,按降序差异排序,因此上述结果将是:

2013-04-20 10:00   HIJ           5
2013-04-20 10:00   ABC           -5
2013-04-20 10:00   DEF           -5

我尝试了一些事情,比如加入桌子,但我无法做到。我该怎么做呢?

3 个答案:

答案 0 :(得分:1)

你可以使用这样的查询:

SELECT t.date, t.productID, t.sold-tp.sold
FROM (
  SELECT t1.date, t1.productID, t1.sold, MAX(t2.date) date_prec
  FROM
    yourtable t1 INNER JOIN yourtable t2
    ON t1.productID=t2.productID AND t1.date>t2.date
  GROUP BY
    t1.date, t1.productID, t1.sold) t INNER JOIN yourtable tp
  ON t.productID=tp.productID and t.date_prec=tp.date

请参阅小提琴here

在子查询中,我将使用相同的产品ID和t1.date>t2.date的条件加入yourtable。按t1.date,productID分组并销售,您可以获得之前的日期时间,即MAX(t2.date)。然后,我再次使用yourtable加入此子查询,以获得前一天sold的值。

修改

您可能还想使用此功能:

SELECT t.date, t.productID, t.sold-tp.sold
FROM
  yourtable t INNER JOIN yourtable tp
  ON t.productID = tp.productID
     AND t.date = tp.date + INTERVAL 1 HOUR

此处我将返回前一小时的soldsold之间的差异。

小提琴是here

答案 1 :(得分:0)

临时表很容易。首先通过以下方式总结您的数据:

CREATE TABLE sales_summary
(
    id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
    product_id CHAR(3) NOT NULL,
    date DATETIME NOT NULL,
    sold INT NOT NULL
)
SELECT
    NULL AS id,
    product_id,
    DATE_FORMAT(date, '%Y-%m-%d %H:00:00') date,
    SUM(sold) AS sold
FROM
    sales
GROUP BY
    product_id,
    DATE_FORMAT(date, '%Y-%m-%d %H:00:00')
ORDER BY
    1,2;

从该摘要表中,您可以创建报告:

SELECT
    b.product_id,
    a.date AS prev_hour,
    b.date AS this_hour,
    b.sold - a.sold AS diff_sold,
    a.sold AS prev_sold,
    b.sold AS this_sold
FROM
    sales_summary a
INNER JOIN
    sales_summary b ON b.id = a.id + 1 AND b.product_id = a.product_id 
ORDER BY
    a.product_id,
    a.date DESC;

返回:

+------------+---------------------+---------------------+-----------+-----------+-----------+
| product_id | prev_hour           | this_hour           | diff_sold | prev_sold | this_sold |
+------------+---------------------+---------------------+-----------+-----------+-----------+
| ABC        | 2013-04-20 10:00:00 | 2013-04-20 11:00:00 |        40 |        10 |        50 |
| ABC        | 2013-04-20 09:00:00 | 2013-04-20 10:00:00 |       -10 |        20 |        10 |
| DEF        | 2013-04-20 09:00:00 | 2013-04-20 10:00:00 |       -10 |        30 |        20 |
| HIJ        | 2013-04-20 09:00:00 | 2013-04-20 10:00:00 |        10 |        30 |        40 |
+------------+---------------------+---------------------+-----------+-----------+-----------+

一个工作示例位于http://sqlfiddle.com/#!2/897d9/1/0

答案 2 :(得分:0)

这可以通过相关子查询简单地完成,尽管它在大型表上可能效率不高:

select `date`, `productId`, ifnull(sold-(
  select sold
  from sales i
  where i.date < s.date
  and i.productId = s.productId
  limit 1
), sold) as diff
from sales s;

一个警告:该查询以及其他答案中的其他问题依赖于一件事:表中的数据已按小时分组,在大多数情况下情况并非如此。

这是fiddle