Is there a way in Postgresql to write a query that groups rows based on a column with a limit without dropping extra rows.
Say I have a table with three columns id, color, score with the following rows
1 red 10.0 2 red 7.0 3 red 3.0 4 blue 5.0 5 green 4.0 6 blue 2.0 7 blue 1.0
I can get color-based grouping using window functions with the following query
SELECT * FROM ( SELECT id, color, score, rank() OVER (PARTITION BY color ORDER BY score DESC) FROM grouping_test ) AS foo WHERE rank <= 2;
with the result
id | color | score | rank ----+-------+-------+------ 4 | blue | 5.0 | 1 6 | blue | 2.0 | 2 5 | green | 4.0 | 1 1 | red | 10.0 | 1 2 | red | 7.0 | 2
which discards an element with a rank> 2. However, I need a result, for example
1 red 10.0 2 red 7.0 4 blue 5.0 6 blue 2.0 5 green 4.0 3 red 3.0 7 blue 1.0
No discarded lines.
Edit: To be more precise about the logic I need:
- Get me the row with the highest result.
- The next line with the same color and the highest possible rating.
- The item with the highest score for the remaining items
- The same as 2., but for a line from 3.
...
Continue until pairs with the same color can be found, and then order what's left descending.
The import statements for the test pattern can be found here . Thank you for your help.
source share