简体   繁体   English

提取 MySQL 5.7 连续值的差异

[英]Extract difference in consecutive values for MySQL 5.7

Name姓名 Date日期 Hours小时 Count数数
Mills米尔斯 2022-07-17 2022-07-17 23 23 12 12
Mills米尔斯 2022-07-18 2022-07-18 00 00 15 15
Mills米尔斯 2022-07-18 2022-07-18 01 01 20 20
Mills米尔斯 2022-07-18 2022-07-18 02 02 22 22
Mills米尔斯 2022-07-18 2022-07-18 03 03 25 25
Mills米尔斯 2022-07-18 2022-07-18 04 04 20 20
Mills米尔斯 2022-07-18 2022-07-18 05 05 22 22
Mills米尔斯 2022-07-18 2022-07-18 06 06 25 25
MIKE麦克风 2022-07-18 2022-07-18 00 00 15 15
MIKE麦克风 2022-07-18 2022-07-18 01 01 20 20
MIKE麦克风 2022-07-18 2022-07-18 02 02 22 22
MIKE麦克风 2022-07-18 2022-07-18 03 03 25 25
MIKE麦克风 2022-07-18 2022-07-18 04 04 20 20

My current input table stores information for counts recorded in each hour of the day consecutively.我当前的输入表存储了一天中每个小时连续记录的计数信息。 I need to extract the difference in values for consecutive counts but I'm having trouble doing it since I'm forced to use MySQL 5.7.我需要提取连续计数的值差异,但由于我被迫使用 MySQL 5.7,所以我无法做到这一点。

I have written the query as follows:我编写了如下查询:

SET @cnt := 0;
SELECT Name, Date, Hours, Count, (@cnt := @cnt - Count) AS DiffCount
FROM Hourly
ORDER BY Date;

which is not giving exact results.这没有给出确切的结果。

I expect to have the following output:我希望有以下输出:

Name姓名 Date日期 Hours小时 Count数数 Diff差异
Mills米尔斯 2022-07-17 2022-07-17 23 23 12 12 0 0
Mills米尔斯 2022-07-18 2022-07-18 00 00 15 15 3 3
Mills米尔斯 2022-07-18 2022-07-18 01 01 20 20 5 5
Mills米尔斯 2022-07-18 2022-07-18 02 02 22 22 2 2
Mills米尔斯 2022-07-18 2022-07-18 03 03 25 25 3 3
Mills米尔斯 2022-07-18 2022-07-18 04 04 20 20 5 5
Mills米尔斯 2022-07-18 2022-07-18 05 05 22 22 2 2
Mills米尔斯 2022-07-18 2022-07-18 06 06 25 25 3 3
MIKE麦克风 2022-07-18 2022-07-18 00 00 15 15 0 0
MIKE麦克风 2022-07-18 2022-07-18 01 01 20 20 5 5
MIKE麦克风 2022-07-18 2022-07-18 02 02 22 22 2 2
MIKE麦克风 2022-07-18 2022-07-18 03 03 25 25 3 3
MIKE麦克风 2022-07-18 2022-07-18 04 04 20 20 5 5
MIKE麦克风 2022-07-18 2022-07-18 05 05 22 22 2 2
MIKE麦克风 2022-07-18 2022-07-18 06 06 25 25 3 3

Please suggest what I'm missing.请提出我所缺少的。

Try the following:尝试以下操作:

SET @count=(select Count_ from Hourly order by Name,Date_,Hours LIMIT 1);
Set @Name=(select Name from Hourly order by Name,Date_,Hours LIMIT 1);

select  Name,Date_,Hours,Count_,
ABS(curr_count-lag_count) as DiffCount
From
(
  select Name,Date_,Hours,Count_, 
  Case When @Name = Name Then @count Else Count_ End as lag_count
  , @count:=Count_ curr_count, @Name:=Name
  From Hourly order by Name,Date_,Hours
) D
Order By Name,Date_,Hours;

See a demo from db-fiddle.查看db-fiddle 的演示。

In MySQL 5.7 you can update your variable inline in order to contain your updated value of " Count ".MySQL 5.7中,您可以更新内联变量以包含更新后的“ Count ”值。 Since when the value of " Name " changes you need to reset your variable, you can use another variable that contains the previous " Name " value.由于当“ Name ”的值发生变化时,您需要重新设置变量,您可以使用另一个包含先前“ Name ”值的变量。 Then you use an IF function to check:然后使用IF函数检查:

  • if your previous name is equal to your current name如果您以前的姓名与您现在的姓名相同
  • then take the difference in counts然后计算计数的差异
  • else assign 0否则分配 0

It would be combined with the ABS function, which applies the absolute value on the difference.它将与ABS函数结合使用,该函数将绝对值应用于差值。

SET @cnt  := NULL;
SET @name := NULL;

SELECT Date,
       Hours,
       ABS(IF(@name = Name, 
              @cnt := @cnt - Count,
              0)                    ) AS DiffCount,
       (@name := Name)                AS Name,
       (@cnt := Count)                AS Count
FROM tab
ORDER BY Name DESC,
         Date, 
         Hours;

Check the demo here .此处查看演示。


In MySQL 8.0 you can use a window fuction like LAG to get your output smoothly.MySQL 8.0中,您可以使用LAG之类的窗口函数来平滑输出。 It would be combined with:它将与:

  • ABS that applies the absolute value on the difference,将绝对值应用于差值的ABS
  • COALESCE employed for removing the first null value. COALESCE用于删除第一个空值。
SELECT *, 
       COALESCE(ABS(
           Count - LAG(Count) OVER(
                       PARTITION BY Name
                       ORDER     BY Date, Hours
                   )
       ), 0) AS Diff 
FROM tab
ORDER BY Name DESC, 
         Date, 
         Hours

Check the demo here .此处查看演示。

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

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