MySQL Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsDatabasesMySQL Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 10th, 2012, 11:53 PM
jraede jraede is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 2 jraede User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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!

Reply With Quote
  #2  
Old October 11th, 2012, 01:50 AM
cafelatte cafelatte is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Mar 2008
Posts: 1,923 cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 5 Days 16 h 21 m 8 sec
Reputation Power: 377
?

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.

Reply With Quote
  #3  
Old October 11th, 2012, 05:38 PM
jraede jraede is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 2 jraede User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #4  
Old October 12th, 2012, 03:05 AM
cafelatte cafelatte is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Mar 2008
Posts: 1,923 cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 5 Days 16 h 21 m 8 sec
Reputation Power: 377
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'.

Reply With Quote
  #5  
Old October 12th, 2012, 07:22 AM
TASB TASB is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2009
Posts: 155 TASB User rank is Second Lieutenant (5000 - 10000 Reputation Level)TASB User rank is Second Lieutenant (5000 - 10000 Reputation Level)TASB User rank is Second Lieutenant (5000 - 10000 Reputation Level)TASB User rank is Second Lieutenant (5000 - 10000 Reputation Level)TASB User rank is Second Lieutenant (5000 - 10000 Reputation Level)TASB User rank is Second Lieutenant (5000 - 10000 Reputation Level)TASB User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 4 Days 8 h 9 m 7 sec
Reputation Power: 88
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMySQL Help > Overlapping GROUP BY

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap