[SOLVED] SQL distinct on Date and ignore Time

Issue

I would love to distinct the table below based on Dates only and at the same time there is an average of value taken. Is there a way to do so?

SELECT DISTINCT DATE_FORMAT(Date, '%Y-%m-%d'),
      AVG(value) AS Timestamp 
FROM abc GROUP BY Date

Table abc

+--------------------+-------+
| Date               | value |
+--------------------+-------+
| 18-Nov-2013 12:34  |   5   |
| 19-Nov-2013 14:45  |   4   |
| 20-Nov-2013 15:11  |   3   |
| 21-Nov-2013 08:33  |   2   |
| 21-Nov-2013 11:11  |   7   |
| 21-Nov-2013 15:15  |   8   |
+--------------------+-------+

Solution

You need to use GROUP function

SELECT DATE_FORMAT(Date, '%Y-%m-%d') AS Timestamp, AVG(value)
FROM abc
GROUP BY DATE(Date)

Answered By – Hett

Answer Checked By – Gilberto Lyons (BugsFixing Admin)

Leave a Reply

Your email address will not be published.