I have a table with three columns: A,B,C.
The values are:
+---+-----+----+
| A | B | C |
+---+-----+----+
| 1 | -10 | 5 |
| 1 | 0 | 5 |
| 1 | 10 | 5 |
| 2 | 10 | 12 |
| 2 | 0 | 12 |
| 3 | -10 | 14 |
| 4 | 0 | 8 |
| 4 | 10 | 8 |
| 5 | 0 | 6 |
| 5 | 1 | 6 |
| 5 | -5 | 6 |
+---+-----+----+
If I first order the data by column A, then column B, then column C (although I did make all column C values the same per column A value) how would I select the “first row” per column A?
So, this should result in:
+---+-----+----+
| A | B | C |
+---+-----+----+
| 1 | -10 | 5 |
| 2 | 0 | 12 |
| 3 | -10 | 14 |
| 4 | 0 | 8 |
| 5 | -5 | 6 |
+---+-----+----+
or
Create a composite index on
(a, b, c)for the queries to work faster.Which one is more efficient depends on your data distribution.
If you have few distinct values of
abut lots of records within eacha, the second query would be better.You could improve it even more by creating an indexed view: