如何计算连续行中值的差异?

z9zf31ra  于 2021-06-24  发布在  Mysql
关注(0)|答案(3)|浏览(328)

我想计算连续行中两个值的差。例如:计算燃油价格与前一天的差价。如何使用sql查询解决这个问题(下面给出了值)。请帮助我

Date        Speed      Petrol      Diesel
2018-04-01  79.22       76.41       68.89   
2018-04-02  79.32       76.52       69.01   
2018-04-04  79.45       76.64       69.15   
2018-04-05  79.48       76.67       69.18   
2018-04-06  79.48       76.67       69.21   
2018-04-07  79.5        76.69       69.25   
2018-04-08  79.53       76.72       69.3    
2018-04-09  79.49       76.69       69.27   
2018-04-10  79.48       76.67       69.3    
2018-04-11  79.48       76.67       69.3    
2018-04-12  79.44       76.63       69.27   
2018-04-13  79.4        76.6        69.3
flvtvl50

flvtvl501#

因为在我看来这是一种商业逻辑。我相信,在代码中这样做比在sql查询中更好。
您可以获取行对象列表中的所有数据,并可以找到差异。

dxxyhpgq

dxxyhpgq2#

如果您的版本不支持窗口功能,您可以使用子查询来获取昨天的价格

select `date`,speed,
          speed - ifnull((select speed from t t1 where t1.date < t.date order by t1.date desc limit 1),speed) yesterdayspeed,
          petrol,
          petrol - ifnull((select petrol from t t1 where t1.date < t.date order by t1.date desc limit 1),petrol) yesterdayspetrol,
          diesel,
          diesel - ifnull((select diesel from t t1 where t1.date < t.date order by t1.date desc limit 1),diesel) yesterdaysdiesel
from t;

+------------+-------+----------------+--------+------------------+--------+------------------+
| date       | speed | yesterdayspeed | petrol | yesterdayspetrol | diesel | yesterdaysdiesel |
+------------+-------+----------------+--------+------------------+--------+------------------+
| 2018-04-01 | 79.22 |           0.00 |  76.41 |             0.00 |  68.89 |             0.00 |
| 2018-04-02 | 79.32 |           0.10 |  76.52 |             0.11 |  69.01 |             0.12 |
| 2018-04-04 | 79.45 |           0.13 |  76.64 |             0.12 |  69.15 |             0.14 |
| 2018-04-05 | 79.48 |           0.03 |  76.67 |             0.03 |  69.18 |             0.03 |
| 2018-04-06 | 79.48 |           0.00 |  76.67 |             0.00 |  69.21 |             0.03 |
| 2018-04-07 | 79.50 |           0.02 |  76.69 |             0.02 |  69.25 |             0.04 |
| 2018-04-08 | 79.53 |           0.03 |  76.72 |             0.03 |  69.30 |             0.05 |
| 2018-04-09 | 79.49 |          -0.04 |  76.69 |            -0.03 |  69.27 |            -0.03 |
| 2018-04-10 | 79.48 |          -0.01 |  76.67 |            -0.02 |  69.30 |             0.03 |
| 2018-04-11 | 79.48 |           0.00 |  76.67 |             0.00 |  69.30 |             0.00 |
| 2018-04-12 | 79.44 |          -0.04 |  76.63 |            -0.04 |  69.27 |            -0.03 |
| 2018-04-13 | 79.40 |          -0.04 |  76.60 |            -0.03 |  69.30 |             0.03 |
+------------+-------+----------------+--------+------------------+--------+------------------+
12 rows in set (0.00 sec)
r55awzrz

r55awzrz3#

看起来是使用mysql 8.0中新窗口功能的好机会
假设其中一个与燃料价格有关,您希望结果按日期升序排列,并生成一个如下所示的表

CREATE TABLE tspeed (
  `id` INT auto_increment primary key,
  `Date` DATE NOT NULL,
  `Speed` DECIMAL(5,2),
  `Petrol` DECIMAL(5,2),
  `Diesel` DECIMAL(5,2)
);

可以使用lag()函数来获取任何列的差异。
例如。

SELECT `id`, `Date`, `Speed`,
  LAG(`Speed`) OVER w AS 'Prev Speed',
  `Speed` - LAG(`Speed`) OVER w AS 'Diff Speed',
  `Petrol`,
  LAG(`Petrol`) OVER w AS 'Prev Petrol',
  `Petrol` - LAG(`Petrol`) OVER w AS 'Diff Petrol',
  `Diesel`,
  LAG(`Diesel`) OVER w AS 'Prev Diesel',
   `Diesel` - LAG(`Diesel`) OVER w AS 'Diff Diesel'
FROM tspeed
WINDOW w AS (ORDER BY `Date` ASC);

https://www.db-fiddle.com/f/nltrhsukely6ypmsaekrcn/5

相关问题