Let's say I have these rows in a table:
ID | SomeColumn | SomeDateTime
---+------------+-------------
1 | X1 | 01-01-14 14:00:00
2 | X2 | 01-01-14 22:00:00
3 | Y1 | 01-03-14 12:00:00
4 | Y2 | 01-03-14 23:00:00
How could I make an SQL query to grab just the rows with distinct dates (choosing the latest times)? That is, the result of the query should be:
ID | SomeColumn | SomeDateTime
---+------------+-------------
2 | X2 | 01-01-14 22:00:00
4 | Y2 | 01-03-14 23:00:00
I've tried this but it doesn't work:
SELECT MAX(SomeDateTime),ID,SomeColumn,SomeDateTime
FROM MyTable GROUP BY (SomeDateTime::date)
The error I get is:
The column "MyTable.ID" needs to appear in the GROUP BY clause or be used in an aggregation function
I'm using PostgreSQL, but if you have answers for other DB engines, I'll upvote too.