| Name | Date | Hours | Count |
|---|---|---|---|
| Mills | 2022-07-17 | 23 | 12 |
| Mills | 2022-07-18 | 00 | 15 |
| Mills | 2022-07-18 | 01 | 20 |
| Mills | 2022-07-18 | 02 | 22 |
| Mills | 2022-07-18 | 03 | 25 |
| Mills | 2022-07-18 | 04 | 20 |
| Mills | 2022-07-18 | 05 | 22 |
| Mills | 2022-07-18 | 06 | 25 |
| MIKE | 2022-07-18 | 00 | 15 |
| MIKE | 2022-07-18 | 01 | 20 |
| MIKE | 2022-07-18 | 02 | 22 |
| MIKE | 2022-07-18 | 03 | 25 |
| MIKE | 2022-07-18 | 04 | 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.
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 | 23 | 12 | 0 |
| Mills | 2022-07-18 | 00 | 15 | 3 |
| Mills | 2022-07-18 | 01 | 20 | 5 |
| Mills | 2022-07-18 | 02 | 22 | 2 |
| Mills | 2022-07-18 | 03 | 25 | 3 |
| Mills | 2022-07-18 | 04 | 20 | 5 |
| Mills | 2022-07-18 | 05 | 22 | 2 |
| Mills | 2022-07-18 | 06 | 25 | 3 |
| MIKE | 2022-07-18 | 00 | 15 | 0 |
| MIKE | 2022-07-18 | 01 | 20 | 5 |
| MIKE | 2022-07-18 | 02 | 22 | 2 |
| MIKE | 2022-07-18 | 03 | 25 | 3 |
| MIKE | 2022-07-18 | 04 | 20 | 5 |
| MIKE | 2022-07-18 | 05 | 22 | 2 |
| MIKE | 2022-07-18 | 06 | 25 | 3 |
Please suggest what I'm missing.