这是我有一张桌子:
+----+---------------------+------------------+------------------+------------------+------------------+-----------------+-----------------+-----+
| id | time | gebr_laag_tarief | gebr_hoog_tarief | leve_laag_tarief | leve_hoog_tarief | huidig_verbruik | huidig_levering | gas |
+----+---------------------+------------------+------------------+------------------+------------------+-----------------+-----------------+-----+
| 1 | 2015-12-22 17:46:33 | 0 | 0 | 0 | 0 | 20 | 0 | 0 |
| 2 | 2015-12-22 17:46:38 | 0 | 0 | 0 | 0 | 30 | 0 | 0 |
| 3 | 2015-12-22 17:46:42 | 0 | 0 | 0 | 0 | 40 | 0 | 0 |
| 4 | 2015-12-22 17:46:45 | 0 | 0 | 0 | 0 | 50 | 0 | 0 |
| 5 | 2015-12-22 17:46:52 | 0 | 0 | 0 | 0 | 60 | 0 | 0 |
| 6 | 2015-12-22 17:46:56 | 0 | 0 | 0 | 0 | 70 | 0 | 0 |
| 7 | 2015-12-22 17:47:00 | 0 | 0 | 0 | 0 | 80 | 0 | 0 |
| 8 | 2015-12-22 17:47:02 | 0 | 0 | 0 | 0 | 90 | 0 | 0 |
| 9 | 2015-12-22 17:47:06 | 0 | 0 | 0 | 0 | 100 | 0 | 0 |
| 10 | 2015-12-22 17:47:11 | 0 | 0 | 0 | 0 | 95 | 0 | 0 |
| 11 | 2015-12-22 17:47:19 | 0 | 0 | 0 | 0 | 85 | 0 | 0 |
| 12 | 2015-12-22 17:47:22 | 0 | 0 | 0 | 0 | 75 | 0 | 0 |
| 13 | 2015-12-24 17:47:24 | 0 | 0 | 0 | 0 | 65 | 0 | 0 |
| 14 | 2015-12-25 17:47:27 | 0 | 0 | 0 | 0 | 55 | 0 | 0 |
| 15 | 2015-12-26 18:08:50 | 0 | 0 | 0 | 0 | 35 | 0 | 0 |
| 16 | 2015-12-26 21:37:57 | 10 | 20 | 0 | 0 | 0 | 0 | 0 |
| 17 | 2015-12-26 21:38:06 | 20 | 40 | 0 | 0 | 0 | 0 | 0 |
| 18 | 2015-12-26 21:38:14 | 25 | 45 | 0 | 0 | 0 | 0 | 0 |
| 19 | 2015-12-26 21:38:22 | 30 | 50 | 0 | 0 | 0 | 0 | 0 |
| 20 | 2015-12-27 14:47:27 | 31 | 52 | 0 | 0 | 10 | 0 | 0 |
| 21 | 2015-12-27 14:47:40 | 36 | 53 | 0 | 0 | 15 | 0 | 0 |
| 22 | 2015-12-27 14:47:49 | 37 | 53 | 0 | 0 | 5 | 0 | 0 |
| 23 | 2015-12-27 14:48:00 | 37 | 54 | 0 | 0 | 6 | 0 | 0 |
| 24 | 2015-12-27 14:48:15 | 40 | 55 | 0 | 0 | 30 | 0 | 0 |
+----+---------------------+------------------+------------------+------------------+------------------+-----------------+-----------------+-----+
这是我尝试实现的目标:
+----------+-----------------------+
| Day | diff_gebr_laag_tarief |
+----------+-----------------------+
| 25 | 0 |
+----------+-----------------------+
| 26 | 30 |
+----------+-----------------------+
| 27 | 10 |
+----------+-----------------------+
换句话说,我想要每天的数字,在当前月份(即当月的第26个月),自上一天的最后一个记录以来的差异。在此解释中,最后一条记录定义为在特定日期添加记录的最后一次,但它也应该是该特定日期的最大ID号(即14 | 2015-12-25 17:47: 27或19 | 2015-12-26 21:38:22)
我希望gebr_laag_tarief,gebr_hoog_tarief,leve_laag_tarief,leve_hoog_tarief和gas有所不同。可以存在0(NULL)值,但每行应包含与前一行相同或更高的数字。此外,每天都会添加记录,不会跳过任何日期。我刚刚添加了一些随机值来进行查询。
我正走在这条道路上:
SELECT Sub1.JustDate, (b.gebr_laag_tarief - a.gebr_laag_tarief) AS Verschil
-> FROM (
-> SELECT DATE_FORMAT(FROM_UNIXTIME(time), '%Y-%m-%d') AS JustDate, MIN(time) AS MinTimeStamp, MAX(time) AS MaxTimeStamp
-> FROM meetwaarden
-> GROUP BY JustDate) Sub1
-> INNER JOIN meetwaarden a on Sub1.MinTimeStamp = a.time
-> INNER JOIN meetwaarden b on Sub1.MaxTimeStamp = b.time
-> ;
但它并没有完全按照我的想法行事,是日数的回报,差异是总差异而不是每日差异。
希望我足够清楚!谢谢你和我一起思考。第一篇帖子对我来说很容易。我有搜索stackoverflow和网络,但只是无法找到答案 - 或者 - 不能让它工作。
答案 0 :(得分:1)
您的查询已经接近,您只需要在连接条件中指定当天是第二次加入的前一天
SELECT t.my_time,DATE(b.my_time), MAX(a.tar) - MAX(b.tar) as diff
FROM
( SELECT MAX(id) as max_id, MAX(my_time) as my_time
FROM test
GROUP BY DATE(my_time)
) t
LEFT JOIN test a ON a.id = t.max_id
LEFT JOIN test b ON DATE(b.my_time) + INTERVAL 1 DAY = DATE(a.my_time)
WHERE b.my_time is not null
GROUP BY t.my_time
<强> sql fiddle 强>
将其翻译成您的表格/查询
SELECT DATE(t.my_time), MAX(a.gebr_laag_tarief) - MAX(b.gebr_laag_tarief) as diff_gebr_laag_tarief
FROM
( SELECT MAX(id) as max_id, MAX(time) as my_time
FROM meetwaarden
GROUP BY DATE(time)
) t
LEFT JOIN meetwaarden a ON a.id = t.max_id
LEFT JOIN meetwaarden b ON DATE(b.time) + INTERVAL 1 DAY = DATE(a.time)
WHERE b.time is not null
GROUP BY t.my_time
输出:
+-----+-----------------------+
| day | diff_gebr_laag_tarief |
+-----+-----------------------+
| 25 | 0 |
| 26 | 30 |
| 27 | 10 |
+-----+-----------------------+
答案 1 :(得分:0)
DATE
分组并选择最大gebr_laag_tarief
。然后它从前一天减去最大值。
SELECT
DATE(m.time) as date,
MAX(m.gebr_laag_tarief) - (
SELECT
MAX(mm.gebr_laag_tarief)
FROM
meetwaarden as mm
WHERE
DATE(mm.time) = DATE_SUB(DATE(m.time), INTERVAL 1 DAY)
) as gebr_diff
FROM
meetwaarden as m
GROUP BY
date
;
<强>附录强>
如果您担心@JohnRuddell的表现,我可以理解。也许您应该考虑存储过程。它会是这样的:
通过选择所有MAX(gebr)
列并按DATE(m.time)
对其进行分组来创建临时表。在日期列上添加索引。
将此表与自己联系,其中日期列每天不同,并减去相应的gebr
列。