I have a table that looks like this:
pk client value date
1 001 564 2012/5/1
2 002 245 2012/6/1
3 003 445 2012/6/6
4 001 845 2012/7/1
5 002 567 2012/8/1
6 001 123 2012/9/1
I know this can be solved with the greatest n per group and a self join, but I’m having trouble figuring it out.
Basically this is what I want for my output
client min(value) max(value) date_for_min(value) date_for_max(value)
001 123 845 2012/9/1 2012/7/1
002 245 567 2012/6/1 2012/8/1
003 445 445 2012/6/6 2012/6/6
The tricky part is getting only one row for each client with the min / max values and then the other columns that go along with those min / max values. Any ideas?
In the case there are multiple rows for some min or max value (for same client), I have given you the earliest date that they appear: