MySQL : “how many records per day?”

I just figured out a cool MySQL trick. I’ve been stupidly doing a very large loop through each month and possible day of the month to query my table for a run-rate per day… as in, “how many records were recorded for each day the data has been accepting data?”… No more large number of queries to get this data, just one:

SELECT DISTINCT(date(your_date_field_name)) AS thedate, count( * ) AS count
FROM your_table_name
GROUP BY thedate
ORDER BY thedate ASC

Results look like this:

2006-10-02	25
2006-10-03	12
2006-10-04	31
2006-10-05	131
2006-10-06	31
2006-10-07	130
2006-10-08	102

which is perfect.

Technorati Tags:


Bookmark and Share

Story pulse

WordPress database error: [You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE 1=1 AND post_id = 1443 GROUP BY hit_date' at line 5]
SELECT SUM(hit_count) AS hits, hit_date FROM WHERE 1=1 AND post_id = 1443 GROUP BY hit_date


One Comment

  1. Daniel:

    I dont think you need the DISTINCT in there. the Group By should do that for you.

Leave a comment