Need help with what I think is likely a simple query - for each distinct val, return only the record with the most recent date. - Mailing list pgsql-general

From Reid Thompson
Subject Need help with what I think is likely a simple query - for each distinct val, return only the record with the most recent date.
Date
Msg-id 4E6EA9B8.6050001@ateb.com
Whole thread Raw
Responses Re: Need help with what I think is likely a simple query - for each distinct val, return only the record with the most recent date.  (Toby Corkindale <toby.corkindale@strategicdata.com.au>)
Re: Need help with what I think is likely a simple query - for each distinct val, return only the record with the most recent date.  (jesuthefrog <jesusthefrog@gmail.com>)
List pgsql-general
Could someone point me in the right direction..
Thanks - reid

Given the example data,
how do I write a query that will give me the resultset:

1    2011-01-01
2    2011-01-06
3    2011-01-05
4    2011-01-09
5    2011-01-05
6    2011-01-08

I.E. for each distinct val, return the record with the most recent date.

ex data
val  date
1    2011-01-01
2    2011-01-02
3    2011-01-03
4    2011-01-04
5    2011-01-05
5    2011-01-01
4    2011-01-02
6    2011-01-03
4    2011-01-04
3    2011-01-05
2    2011-01-06
4    2011-01-07
6    2011-01-08
4    2011-01-09
5    2011-01-01
2    2011-01-02
4    2011-01-03
2    2011-01-04
1    2011-01-01
2    2011-01-02
3    2011-01-03
4    2011-01-04
3    2011-01-05
1    2011-01-01
2    2011-01-02
3    2011-01-03
4    2011-01-04
5    2011-01-01
-----------------------------------

$ cat sampledata|sort -k1,2
1    2011-01-01
1    2011-01-01
1    2011-01-01
2    2011-01-02
2    2011-01-02
2    2011-01-02
2    2011-01-02
2    2011-01-04
2    2011-01-06
3    2011-01-03
3    2011-01-03
3    2011-01-03
3    2011-01-05
3    2011-01-05
4    2011-01-02
4    2011-01-03
4    2011-01-04
4    2011-01-04
4    2011-01-04
4    2011-01-04
4    2011-01-07
4    2011-01-09
5    2011-01-01
5    2011-01-01
5    2011-01-01
5    2011-01-05
6    2011-01-03
6    2011-01-08



pgsql-general by date:

Previous
From: Reid Thompson
Date:
Subject: Re: Need help with what I think is likely a simple query - for each distinct val, return only one record with the most recent date.
Next
From: pasman pasmański
Date:
Subject: Re: Compatibility 9.1rc and 9.1.0