Jump to content

Grouping by month or week


lxndr

Recommended Posts

I have a table that contains amongst other things the following columns:

 

comment:  text

date:  int(10)

 

The date is a unix timestamp and what I'd like to be able to do is run a MySQL query to show the number of comments made each week or each month so that the output might be something like:

 

January 2009:    345

February 2009:  384

 

etc. etc.

 

I'm sure there's a way to do it with a single SQL query but have so far failed to get it working.  Any help much appreciated.

 

___

 

Link to comment
https://forums.phpfreaks.com/topic/187607-grouping-by-month-or-week/
Share on other sites

Hi

 

Try something like this

 

SELECT FROM_UNIXTIME(datefield, '%Y %M'), COUNT(comment)
FROM sometable
GROUP BY UNIXTIME(datefield, '%Y %M')

 

However be aware that you will need to play around to order it correctly (probably best done by bringing back the date column twice, once in a numeric format to order it by).

 

All the best

 

Keith

Hi

 

Try something like this

 

SELECT FROM_UNIXTIME(datefield, '%Y %M'), COUNT(comment)
FROM sometable
GROUP BY UNIXTIME(datefield, '%Y %M')

 

However be aware that you will need to play around to order it correctly (probably best done by bringing back the date column twice, once in a numeric format to order it by).

 

All the best

 

Keith

 

Hi Keith,

 

Many thanks for the suggestion but when I try that query I get an error message saying function UNIXTIME does not exist?

 

Best wishes,

 

Ian

 

__

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.