If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Group by range of hours

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-26-10, 23:34
sandeep.ctg sandeep.ctg is offline
Registered User
 
Join Date: Mar 2010
Posts: 1
Cool Group by range of hours

Hello All,
In my database i have a table with two timestamp fields like startDate and endDate
Now i want to pass startDate and endDate in my query and according to the difference between those dates i want to group the data by hours.

example 1) If differece is 24 hours then i will get 00-23

example 2) If the difference is 48 hours then i want to group 2 hours of the same day so i can finally get only 24 rows
|
|
example 7) If difference is 168 hours ie 7 days then group by 7 hours



condition is group hours of the same day
Reply With Quote
  #2 (permalink)  
Old 03-27-10, 04:59
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
One method might be to convert the two times into seconds (unix_timestamp) Then subtract one from the other to give number of seconds between the two datetimes. Then divide by 3600 to give the number of hours. Put this through a case statement to decide what type of output ie group by 2 hours etc. Unsure if you need to group the data by this field as well. It might of been clearer if you provided some sample data and output. What is the project and why does it need the output this way?
__________________
Mike
Reply With Quote
  #3 (permalink)  
Old 03-27-10, 08:41
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
[offtopic] it's not might of been, mike, it's might of beed [/offtopic]

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #4 (permalink)  
Old 03-27-10, 12:16
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
Seeing as it's a small town in India then surely it should be might of Beed.
__________________
Mike
Reply With Quote
Reply

Tags
groupby range hours

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On