MySQL Forums
Forum List  »  Newbie

Wondering if this makes any sense (Question on SUM())
Posted by: Seandon Mooy
Date: October 27, 2009 11:53AM

Hello,
I have recently consolidated quite a bit of PHP code into a nifty mySQL query. It appears to work correctly, but I get the feeling I am doing things in a particularly ugly way.
Thanks in advance for any help (and apologizes if this is in the wrong forum!).

My query:

SELECT
SUM(starttime BETWEEN 1249912800 AND 1249916400) as hour0,
SUM(starttime BETWEEN 1249916400 AND 1249920000) as hour1,
SUM(starttime BETWEEN 1249920000 AND 1249923600) as hour2,
SUM(starttime BETWEEN 1249923600 AND 1249927200) as hour3,
[...]
SUM(starttime BETWEEN 1249995600 AND 1249999200) as hour23,
COUNT(*) as totalhours
FROM records
LEFT JOIN groups ON groups.id = group_id
WHERE
starttime BETWEEN 1249884000 AND 1250056800

Basically, I'm grabbing a row count for each hour in a particular day (totaling rows that fall into each hour, as well as getting a total count with "totalhours").
It seems to be working, but like I said I'd like some critique :D My results are like "hour1: 5 rows", "hour2: 16 rows", etc.

Thanks!

Options: ReplyQuote


Subject
Written By
Posted
Wondering if this makes any sense (Question on SUM())
October 27, 2009 11:53AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.