I should be able to do this but the brain appears to have melted down
Database is postgres
Table structure is simple, just four columns that matter:
Location, User, Activity, DateTime
I need to sort by location and output just the latest occurrence of each activity that has taken place. I don’t know in advance what the activities are
Something like
Location User Activity DateTime London Fred A 08-29-2012 London Fred B 08-27-2012 Paris John A 08-29-2012 Tokyo Fred A 08-17-2012 Tokyo Jane D 08-29-2012
Thanks Folks
Fastest way in PostgreSQL is probably with
DISTINCT ON:Short form with positional parameters:
This combines sorting and reducing to distinct rows in one operation.
More details, explanation and benchmark in this related answer.
useris a reserved word. Don’t actually use it as column name. I substituted withusr.If performance should be crucial an index like the following will make the difference: