简体   繁体   English

mysql行之间的区别

[英]Difference between mysql rows

I have a mysql table that's something like this: 我有一个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..

I'd like to get the difference between the amount each product sold for every hour, sorted by the descending difference, so the result for the above would be: 我想得出每小时销售的每件产品的数量之间的差异,按降序差异排序,因此上述结果将是:

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

I've tried a few things, like joining the table with itself, but I can't get it right. 我尝试了一些事情,比如加入桌子,但我无法做到。 How would I go about doing this? 我该怎么做呢?

You could use a query like this: 你可以使用这样的查询:

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

Please see fiddle here . 请看这里的小提琴。

In the subquery I'm joining yourtable with itself, on the same product ID and with the condition that t1.date>t2.date . 在子查询中,我正在使用相同的产品ID和条件t1.date>t2.date Grouping by t1.date, productID and sold you can get the previous datetime, which is MAX(t2.date) . 按t1.date,productID分组并出售,您可以获得之前的日期时间,即MAX(t2.date) I'm then joining this subquery with yourtable again, in order to get the value of sold of the previous day. 然后我再次与yourtable加入这个子查询,以获得前一天的sold价值。

EDIT 编辑

You might also want to use this: 您可能还想使用此:

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

here I'm returning the difference between sold and sold of the previous hour. 在这里,我将返回前一小时的soldsold之间的差额。

Fiddle is here . 小提琴就在这里

It's easy with temporary tables. 临时表很容易。 First summarize your data via: 首先通过以下方式总结您的数据:

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;

From that summary table, you can create your report: 从该摘要表中,您可以创建报告:

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;

which returns: 返回:

+------------+---------------------+---------------------+-----------+-----------+-----------+
| 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 |
+------------+---------------------+---------------------+-----------+-----------+-----------+

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

This can be done simply with a correlated subquery, although it might not be as efficient on large tables: 这可以通过相关子查询简单地完成,尽管它在大型表上可能效率不高:

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;

One caveat: That query, and the others in the other answers rely on one thing: the data in the table already being grouped by hour, which will not be the case in most scenarios. 需要注意的是:该查询以及其他答案中的其他问题依赖于一件事:表中的数据已按小时分组,在大多数情况下情况并非如此。

Here's the fiddle . 这是小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM