views:

559

answers:

1

I want to log certain activities in MySql with a timecode using time(). Now I'm accumulating thousands of records, I want to output the data by sets of hours/days/months etc.

What would be the suggested method for grouping time codes in MySQL?

Example data:

  • 1248651289
  • 1248651299
  • 1248651386
  • 1248651588
  • 1248651647
  • 1248651700
  • 1248651707
  • 1248651737
  • 1248651808
  • 1248652269

Example code:

$sql = "SELECT COUNT(timecode) FROM timecodeTable";
//GROUP BY round(timecode/3600, 1) //group by hour??

Edit: There's two groupings that can be made so I should make that clearer: The 24 hours in the day can be grouped but I'm more interested in grouping over time so returning 365 results for each year the tracking is in place, so total's for each day passed, then being able to select a range of dates and see more details on hours/minutes accessed over those times selected.

This is why I've titled it as using PHP, as I'd expect this might be easier with a PHP loop to generate the hours/days etc?

+2  A: 

Peter

SELECT COUNT(*), HOUR(timecode)
FROM timecodeTable
GROUP BY HOUR(timecode);

Your result set, given the above data, would look as such:

+----------+----------------+
| COUNT(*) | HOUR(timecode) |
+----------+----------------+
|       10 |             18 | 
+----------+----------------+

Many more related functions can be found here.

Edit

After doing some tests of my own based on the output of your comment I determined that your database is in a state of epic fail. :) You're using INT's as TIMESTAMPs. This is never a good idea. There's no justifiable reason to use an INT in place of TIMESTAMP/DATETIME.

That said, you'd have to modify my above example as follows:

SELECT COUNT(*), HOUR(FROM_UNIXTIME(timecode))
FROM timecodeTable
GROUP BY HOUR(FROM_UNIXTIME(timecode));

Edit 2

You can use additional GROUP BY clauses to achieve this:

SELECT 
  COUNT(*),
  YEAR(timecode),
  DAYOFYEAR(timecode),
  HOUR(timecode)
FROM timecodeTable
GROUP BY YEAR(timecode), DAYOFYEAR(timecode), HOUR(timecode);

Note, I omitted the FROM_UNIXTIME() for brevity.

hobodave
There are a lot of potentially useful time functions on that page, they could possibly be combined to output what I'm after, looking at the INTERVAL options for example... Using my full dataset (150,000+ records spanning almost two years of time codes) it's giving me 105,000: NULL and 58000: 838 which means little to me so far :)
Peter
See my comment above. You have not adequately described "what you are after".
hobodave
Sorry, you're right - my explanation is confusing as I can't quite grasp what it is I'm trying to do :) My answer posted outputs exactly what I needed.
Peter
You're right, the "timecodes" are stored as int (*slap on wrist* tut tut). This example works perfectly for breaking up into a total of 24 rows (listing each hour), is there a variation that can output 17500+ rows of each hour on a timeline (ie 24hrs * 365 days *2 yrs = 17500 records)?
Peter
My time codes are stored as int because I find it easier to work with figures like "1248659428" (native to PHP) instead of "2009-07-27 11:25:01".
Peter
Yes, but now you have to do ugly stuff in SQL. HOUR(timecode) becomes either HOUR(FROM_UNIXTIME(timecode)) or ROUND(timecode/3600). Check out strtotime http://us2.php.net/strtotime
hobodave
Thanks for the continued edits, this is exactly what I'm looking for with the temporary FROM_UNIXTIME(). I'll convert the records. Works perfectly, cheers!
Peter