Reputation: 5550
I have a MySQL database with the following columns:
id (INT) duration (FLOAT) start_date (DATETIME)
I would like to get modal statistics (frequency counts) for the duration and start_date columns.
Sorting the duration of an event by frequency was straightforward:
SELECT duration, COUNT(duration)
AS frequency
FROM trips
GROUP BY duration
ORDER BY frequency
DESC;
If I run this same query on the start_date column, I get results of the form:
(datetime.datetime(2012, 8, 20, 15, 22), 6L)
(I'm using python to access the database). However, receiving my queries binned by the minute isn't necessarily helpful -- month, day of the week and hour are much more useful queries. I've tried changing the query to:
SELECT MONTH(start_date), COUNT(MONTH(start_date))
AS frequency
FROM trips
GROUP BY start_date
ORDER BY frequency
DESC;
However, if I do this then I start getting errors about my SQL syntax (Error 1064). I haven't been able to figure out what the error is, unfortunately.
I've considered putting the year, month, day of the week, hour, minute, etc. in different columns, but I feel that it should be possible to select the subfield of the datetime field within a query.
thanks!
Upvotes: 1
Views: 705
Reputation: 171421
Count frequency of start_date months:
SELECT YEAR(start_date), MONTH(start_date), COUNT(*) AS frequency
FROM trips
GROUP BY YEAR(start_date), MONTH(start_date)
ORDER BY frequency DESC;
Count frequency of durations:
SELECT duration, COUNT(*) AS frequency
FROM trips
GROUP BY duration
ORDER BY frequency DESC;
Upvotes: 1