{"id":1280,"date":"2015-03-10T06:56:33","date_gmt":"2015-03-10T06:56:33","guid":{"rendered":"http:\/\/www.nikola-breznjak.com\/blog\/?p=1280"},"modified":"2015-08-10T06:49:16","modified_gmt":"2015-08-10T06:49:16","slug":"group-by-hour-in-mysql-but-include-the-first-value-from-the-hour1-also","status":"publish","type":"post","link":"https:\/\/nikola-breznjak.com\/blog\/stack-overflow\/group-by-hour-in-mysql-but-include-the-first-value-from-the-hour1-also\/","title":{"rendered":"Group by hour in mysql but include the first value from the hour+1 also"},"content":{"rendered":"<p><a href=\"http:\/\/stackoverflow.com\/users\/534755\/nikola\"><img loading=\"lazy\" decoding=\"async\" title=\"profile for Nikola at Stack Overflow, Q&amp;A for professional and enthusiast programmers\" src=\"http:\/\/stackoverflow.com\/users\/flair\/534755.png\" rel=\"lightbox[1280]\" alt=\"profile for Nikola at Stack Overflow, Q&amp;A for professional and enthusiast programmers\" width=\"208\" height=\"58\" \/><\/a><br \/>\nI&#8217;m a big fan of <a href=\"http:\/\/stackoverflow.com\/\">Stack Overflow<\/a> and I tend to contribute regularly (am currently in the <a href=\"http:\/\/stackexchange.com\/leagues\/1\/alltime\/stackoverflow\/2008-07-31\/534755?sort=reputationchange#534755\">top 0.X%<\/a>).\u00a0In this category (<a href=\"http:\/\/www.nikola-breznjak.com\/blog\/category\/stack-overflow\/\">stackoverflow<\/a>)\u00a0of posts I will will be posting my top rated questions and answers. This, btw, is allowed as explained in the meta thread <a href=\"http:\/\/meta.stackoverflow.com\/questions\/266971\/can-i-post-so-questions-and-answers-in-a-personal-blog\/266973\">here<\/a>.<\/p>\n<p>My <a href=\"http:\/\/stackoverflow.com\/questions\/26465346\/group-by-hour-in-mysql-but-include-the-first-value-from-the-hour1-also\">question<\/a> was:<\/p>\n<p style=\"color: #222222;\">Say you have data like this:<\/p>\n<pre class=\"lang:default decode:true\">  time  value\r\n  10:00   5\r\n  10:15   12\r\n  10:30   15\r\n  10:45   27\r\n  11:00   29<\/pre>\n<p style=\"color: #222222;\">And a query like this:<\/p>\n<pre class=\"lang:default decode:true\">SELECT MAX(value)- MIN(value), HOUR(time)FROM mytable GROUP BY HOUR(time);<\/pre>\n<p style=\"color: #222222;\">You will get:<\/p>\n<pre class=\"lang:default decode:true\"> value  time\r\n   22   10<\/pre>\n<p style=\"color: #222222;\">But, I would need it to include the value at 11:00, thus the result being 24 (29-5), and not 22. Is there a way to do this in SQL or do I have no other choice than to do this manually in the code level (so, without the grouping, just fetching the data and manually substracting).<\/p>\n<p style=\"color: #222222;\">The answer, by <a style=\"color: #0c65a5;\" href=\"http:\/\/stackoverflow.com\/users\/2810986\/atp-jd\">ATP_JD<\/a>, was:<\/p>\n<blockquote>\n<div class=\"post-text\">\n<p style=\"color: #222222;\">Depending on how consistent your data is, you might be able to do this with a self join, like so:<\/p>\n<pre class=\"lang:default decode:true   \">SELECT HOUR(a.`time`)AS grouper,\r\nGREATEST(MAX(a.value),IFNULL(MIN(b.value),0))- MIN(a.value)AS diff\r\nFROM mytable a\r\nLEFTJOIN mytable b ONIF(HOUR(a.time)&lt;=23, HOUR(a.time)+1,0)= HOUR(b.time)GROUP BY grouper<\/pre>\n<p style=\"color: #222222;\">The\u00a0<code>LEFT JOIN<\/code>\u00a0on the same table allows you to get the next hour&#8217;s value for comparison purposes.<\/p>\n<p style=\"color: #222222;\"><a style=\"color: #0c65a5;\" href=\"http:\/\/sqlfiddle.com\/#!9\/fe72a\/16\" rel=\"nofollow\">http:\/\/sqlfiddle.com\/#!9\/fe72a\/16<\/a><\/p>\n<\/div>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m a big fan of Stack Overflow and I tend to contribute regularly (am currently in the top 0.X%).\u00a0In this category (stackoverflow)\u00a0of posts I will will be posting&hellip;<\/p>\n","protected":false},"author":1,"featured_media":609,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[35],"tags":[],"class_list":["post-1280","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-stack-overflow"],"_links":{"self":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/1280","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/comments?post=1280"}],"version-history":[{"count":3,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/1280\/revisions"}],"predecessor-version":[{"id":2021,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/1280\/revisions\/2021"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/media\/609"}],"wp:attachment":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/media?parent=1280"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/categories?post=1280"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/tags?post=1280"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}