'MS Access select the Nth Highest value in a table
So I have the following database format:
+------++------+
| ID | Value |
+------++------+
| 1 | 5 |
| 1 | 2 |
| 1 | 8 |
| 1 | 1 |
| 2 | 2 |
| 2 | 3 |
| 2 | 6 |
| 2 | 10 |
| 3 | 1 |
| 3 | 2 |
| 3 | 5 |
| 3 | 3 |
| 3 | 5 |
+------++------+
And I am trying to output the top 3 values of each ID. Like:
+------++------+
| ID | Value |
+------++------+
| 1 | 8 |
| 1 | 5 |
| 1 | 3 |
| 2 | 10 |
| 2 | 6 |
| 2 | 3 |
| 3 | 5 |
| 3 | 5 |
| 3 | 3 |
+------++------+
Is this possible to do in MS Access using SQL?
Solution 1:[1]
You need correlation subquery :
select t.*
from [table] t
where value in (select top 3 t1.value
from [table] t1
where t1.id = t.id
order by t1.value desc
) order by ID asc, value desc;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Santosh |
