Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL select distinct with unique ids

Tags:

sql

sqlite

I'm sorry, my SQL is a bit rusty so this might be trivial, but I can't figure it out.

I have table data similar to this:

ID  LABEL
101 A
102 A
103 A
104 B
105 C
106 C

I'd like to select only distinct labels, but also have a column with ids. Ideally result could be:

ID  LABEL
101 A
104 B
105 C

I don't really care which ID gets selected for a label. Less ideally id could any unique integer, like this:

ID  LABEL
1   A
2   B
3   C

I'm using SQLite, if that matters.

like image 872
Juozas Kontvainis Avatar asked Dec 17 '22 07:12

Juozas Kontvainis


1 Answers

This query will do the trick and select you the min id for each label. You just need to put in the name of your table...

SELECT MIN(id), LABEL
FROM table 
GROUP BY LABEL
ORDER BY MIN(id)
like image 56
Kevin Holditch Avatar answered Jan 09 '23 11:01

Kevin Holditch