Let's say I have this table:
id colorName
1 red
2 blue
3 red
4 blue
How to select one representative of each color?
Result:
1 red
2 blue
Thanks.
Let's say I have this table:
id colorName
1 red
2 blue
3 red
4 blue
How to select one representative of each color?
Result:
1 red
2 blue
Thanks.
Not random representatives, but...
select color, min(id)
from mytable
group by color;
In MS SQL Server
and Oracle
:
SELECT id, colorName
FROM (
SELECT id, colorName,
ROW_NUMBER() OVER (PARTITION BY colorName ORDER BY id) AS rn
FROM colors
)
WHERE rn = 1