Assume I have the following data:
| f1 | f2 | f3 |
|----|----|----|
| 1 | 1 | 1 |
| 1 | 1 | 5 |
| 1 | 2 | 3 |
| 1 | 2 | 6 |
| 1 | 3 | 4 |
| 1 | 3 | 7 |
| 2 | 1 | 2 |
| 2 | 1 | 22 |
| 2 | 2 | 3 |
| 2 | 2 | 4 |
There are two f3 values maximum per each f1,f2 combination.
For this specific example, I want to get top-2 minimum values per f1 and range (f2 - max(f2) per f1).
Example output:
| f1 | RNG| f3 |
|----|----|----|
| 1 |1 -3| 1 |
| 1 |1 -3| 3 |
| 1 |2 -3| 3 |
| 1 |2 -3| 4 |
| 1 |3 -3| 4 |
| 1 |3 -3| 7 |
| 2 |1- 2| 2 |
| 2 |1- 2| 3 |
| 2 |2- 2| 3 |
| 2 |2- 2| 4 |
To create the field RNG is not required. I only added it to show that for f1=1, there are 3 ranges: 1-3, 2-3, 3-3 created by the distinct values of f2 for f1=1. For each such range I want to calculate the top-k minimum values per f1 and range.
SQL Fiddle here:
http://sqlfiddle.com/#!15/9ddbb/1
Building the Ranges may be done by:
SELECT DISTINCT s1.f1,s1.f2 AS range_from ,s2.f2 AS range_to
FROM dbTable s1,
(SELECT f1,MAX(f2) AS f2 FROM dbTable
GROUP BY f1) s2
WHERE s1.f1=s2.f1
ORDER BY s1.f1,s1.f2;
Is there any way to achieve that, without building intermediate DB tables?