The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Databases
> MySQL Help
|
Overlapping GROUP BY
Discuss Overlapping GROUP BY in the MySQL Help forum on Dev Shed. Overlapping GROUP BY MySQL Help forum discussing administration, SQL syntax, and other MySQL-related topics. MySQL is an open-source relational database management system (RDBMS).
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

October 10th, 2012, 11:53 PM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 2
Time spent in forums: 12 m 46 sec
Reputation Power: 0
|
|
|
Overlapping GROUP BY
I have a query that gets the number of events that happen on an object for each minute in a dynamically generated "time_intervals" table. So the results look something like this:
object | timestamp | events
NULL | 2012-10-01 00:00:00 | 0
NULL | 2012-10-01 00:01:00 | 0
object1 | 2012-10-01 00:02:00 | 5
object2 | 2012-10-01 00:02:00 | 7
object1 | 2012-10-01 00:03:00 | 2
NULL | 2012-10-01 00:04:00 | 0
...etc. What I want to do is take that and get the average value for the "events" column per object, but also include rows with no events/objects, since those represent minutes in which nothing happened and thus should contribute a zero to the average. Seems like I would have to write some complicated GROUP BY clause that includes all rows with object = NULL in each group, but I'm lost as to how to do that.
Any help would be appreciated.
Thanks!
|

October 11th, 2012, 01:50 AM
|
|
|
|
?
So given that data set, what would the result set look like ? (show the arithmetic too)
And what's the PRIMARY KEY on this table?
Last edited by cafelatte : October 11th, 2012 at 04:48 AM.
|

October 11th, 2012, 05:38 PM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 2
Time spent in forums: 12 m 46 sec
Reputation Power: 0
|
|
|
Sorry, should have been more clear. This is the result of the query I perform by joining the temporary time_intervals table with the events and objects table. I want to then take that query and group the results by objects.id, but also include all results with no object in each group.
So, the group for object1 would include all rows with object = NULL, as would the group for object2, object3, etc. This is so when calculating, say, the average number of events per minute, the zero-values are also calculated so the average is legitimate.
Hope this makes sense.
|

October 12th, 2012, 03:05 AM
|
|
|
Quote: | Originally Posted by jraede Hope this makes sense. |
Not to me, but there are some pretty smart cookies around here so it might to them.
Personally, I like clear, concise examples that can be easily replicated with 'copy and paste'.
|

October 12th, 2012, 07:22 AM
|
|
|
object | timestamp | events
NULL | 2012-10-01 00:00:00 | 0
NULL | 2012-10-01 00:01:00 | 0
object1 | 2012-10-01 00:02:00 | 5
object2 | 2012-10-01 00:02:00 | 7
object1 | 2012-10-01 00:03:00 | 2
NULL | 2012-10-01 00:04:00 | 0
This isn't tested but is it what you are after?
Code:
SELECT
object,
(SUM(events) / ts.minutes) avgEvents
FROM
time_intervals,
(select ((max(timestamp) - min(timestamp)) / 60) minutes from time_intervals) ts
GROUP BY
object
Last edited by TASB : October 12th, 2012 at 08:05 AM.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|