My table is:
id env date name PI #
---|-----|------------|--------|-----
1 | 10 | 04/03/2009 | john | 399
2 | 11 | 04/03/2009 | juliet| 244
5 | 12 | 04/03/2009 | borat | 345
3 | 10 | 03/03/2009 | john | 399
4 | 11 | 03/03/2009 | juliet | 244
6 | 12 | 03/03/2009 | borat | 500
7 | 13 | 24/12/2008 | borat | 650
8 | 13 | 01/01/2009 | borat | 650
This post is slightly modified from the question below.
How can I SELECT rows with MAX(Column value), DISTINCT by another column in SQL?
The difference is that I want to select each distinct environment and PI# holding the maximum value of date. For example when two rows have the same env & their PI #s are the same (rows 3 & 1 ,2 & 4, 7 & 8), I would like to return the row with the max date.
Below is the desired result.
id env date name PI #
---|----|------------|--------|----
1 | 10 | 04/03/2009 | john | 399
2 | 11 | 04/03/2009 | juliet | 244
5 | 12 | 04/03/2009 | borat | 345
6 | 12 | 03/03/2009 | borat | 500
8 | 13 | 01/01/2009 | borat | 650