Ok I am representing a matrix with three columns in a table. For simplicity sake: Columns A,B and Value. A and B represent the X and Y “coordinates” of the matrix.
Could someone please help me work out the following:
For each column in the matrix (X), I wish to retrieve the smallest Value AND the Y coordinate field, which represented this smallest Value in column X.
This is for each column/X in the matrix.
I have been struggling because I couldnt include Y in a group by or aggregate function, so it wouldnt let me return it in the same query as calculating the minimum value (X was GROUP BY’d).
EDIT If there are two or more rows, the minimum Y value should be returned.
Just to be clear, my table is therefore:
Y | X | Value
0 | 0 | 0.38
1 | 0 | 0.72
2 | 0 | 0.13
3 | 0 | 0.45
0 | 1 | 0.52
1 | 1 | 0.23
2 | 1 | 0.84
3 | 1 | 0.15
0 | 2 | 0.52
1 | 2 | 0.04
2 | 2 | 0.04
3 | 2 | 0.15
With the above test data, it should return:
X | Y | Value
0 | 2 | 0.13
1 | 3 | 0.15
2 | 1 | 0.04
Please note on the Y=2, row 1 was the smallest Y, even though row 2 also had the minimum value.
You can test it at: SQL-Fiddle
(corrected to reflect the changed data in the question: test-2)
An index on
(x, value, y)will be useful for performance.