How Do You Group Data by Week in SQL Server? SQL Server provides a function called DATEPART() , which returns a specified part (year, quarter, month, week, hour, minute, etc.) of a specified date. ORDER BY DATEPART(week, RegistrationDate);
1 Answer. You can use DATE_FORMAT operator. If you are using this you can easily group the date, timestamp or datetime column using whatever format you want.
You can group month and year with the help of function DATE_FORMAT() in MySQL. The GROUP BY clause is also used.
How about this? Using the DATE
function:
SELECT DATE(FROM_UNIXTIME(MyTimestamp)) AS ForDate,
COUNT(*) AS NumPosts
FROM MyPostsTable
GROUP BY DATE(FROM_UNIXTIME(MyTimestamp))
ORDER BY ForDate
This will show you the number of posts on each date that the table has data for.
SELECT DATE(timestamp) AS ForDate,
COUNT(*) AS NumPosts
FROM user_messages
GROUP BY DATE(timestamp)
ORDER BY ForDate
This found for me. I have timestamp like "2013-03-27 15:46:08".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With