3

Say you have data like this:

  time  value
  10:00   5
  10:15   12
  10:30   15
  10:45   27
  11:00   29

And a query like this:

SELECT MAX(value) - MIN(value), HOUR(time) FROM mytable GROUP BY HOUR(time);

You will get:

  value  time
    22    10

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 subtracting).

6
  • You want to include both 10:00 and 11:00? Just remove the group by clause. Oct 20, 2014 at 12:11
  • And do you still need values at 10:00 to be included? Oct 20, 2014 at 12:12
  • @GordonLinoff: I can't remove the group by because I want the results per hour. So, I'll have result of a max-min for all the hours of the current day.
    – Nikola
    Oct 20, 2014 at 12:17
  • @PraveenPrasannan: Yes, though am interested in how does this change the potential answer from you? Can you state the difference if 10:00 is included vs that it isn't?
    – Nikola
    Oct 20, 2014 at 12:18
  • Just to verify, you want the data point at 11:00 to both be part of the 10-11 interval, and the 11-12 interval?
    – thebjorn
    Nov 15, 2014 at 17:13

3 Answers 3

3
+50

Depending on how consistent your data is, you might be able to do this with a self join, like so:

SELECT HOUR(a.`time`) AS grouper,
GREATEST(MAX(a.value),IFNULL(MIN(b.value),0)) - MIN(a.value) AS diff
FROM mytable a
LEFT JOIN mytable b ON IF(HOUR(a.time) <= 23, HOUR(a.time)+1, 0) = HOUR(b.time)
GROUP BY grouper

The LEFT JOIN on the same table allows you to get the next hour's value for comparison purposes.

http://sqlfiddle.com/#!9/fe72a/16

2
  • Hi, thanks for your answer - but the value at 12 should be 1 (34-33) instead of -33. Any idea on how to solve that?
    – Nikola
    Nov 16, 2014 at 12:59
  • Yes add the GREATEST() method on diff so that it uses the next hour, if it exists, otherwise the largest value from the current hour. See updated SQL above, and updated fiddle.
    – ATP_JD
    Nov 16, 2014 at 16:26
1

Try:

SELECT hr, 
       max( value ) - min( value )
FROM mytable m
JOIN (
  SELECT  HOUR(`time`) hr, 
          cast(Date_Format(min(`time`),'%Y-%m-%d %H:00:00') as time) mt
  FROM mytable 
  GROUP BY HOUR(`time`)
) q
ON m.`time` >= q.mt 
AND m.`time` <= q.mt + interval 1 hour 
GROUP BY hr

Demo: http://sqlfiddle.com/#!9/012c7/9

2
  • Thank you for your answer. As it seems your answer is perfect and I will accept it. However could you please give me a hint where am I going wrong when I incorporate this query to my bigger query which then looks like this: sqlfiddle.com/#!2/e6ac9/8
    – Nikola
    Nov 16, 2014 at 12:58
  • I realized in the previous examples I had the wrong dataset (inversed) so the real one is here sqlfiddle.com/#!2/3b0d4/1. And though, I'm still missing something to get the data, so any help would be appreciated
    – Nikola
    Nov 16, 2014 at 13:14
0

Since you want the value of 11:00 to be part of both HOUR=10 and HOUR=11, I would use this UNION ALL query:

SELECT `time`, MAX(value) - MIN(value)
FROM (
  SELECT
    HOUR(`time`) AS `time`, value
  FROM
    mytable

  UNION ALL

  SELECT
    CASE WHEN HOUR(`time`) > 0 THEN HOUR(`time`)-1 ELSE 23 END, value
  FROM
    mytable
  WHERE
    MINUTE(`time`)=0
) s
GROUP BY `time`

If you have a large amount of data, I would try to rethink your database structure since queries like this one can't be optimized much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.