My table looks like:
A B C D
1 1 1 1
1 1 3 2
1 1 0 4
1 1 2 1
1 2 1 0
1 2 0 2
1 2 4 5
2 1 5 3
My goal is to, for each pair of A and B, output the value in D that corresponds to MIN(C), and the value in D that corresponds to MAX(C). The output should be
A B D at MIN(C) D at MAX(C)
1 1 4 2
1 2 2 5
2 1 3 3
I know that to extract MIN(C) and MAX(C) I simply do:
SELECT A, B, MIN(C) as "minC", MAX(C) as "maxC"
FROM Table
GROUP BY A, B
ORDER BY A, B
My question is: how do I bring column D along for the ride? If I include it in the SELECT and GROUP BY clauses, it will generate the MIN(C) and MAX(C) for each D, which is not what I want. Moreover, I don't even need to output MIN(C) and MAX(C). D is all that I'm after.
The basic outline provided in SQL Select only rows with Max Value on a Column does not seem to handle this case.
Thanks in advance!