Search code examples
sqliteselectdistinct

How to SELECT DISTINCT of one column and get the others?


I have a table like this (but with more columns):

Code Quantity
00001 1
00002 1
00002 1
00002 2
00003 2
00003 1

And I want to get the same result as with SELECT DISTINCT Code FROM table (00001,00002,00003) but with all of the other table columns.

If I perform SELECT DISTINCT Code, Quantity from table I get:

Code Quantity
00001 1
00002 1
00002 2
00003 1
00003 2

But I would like to get:

Code Quantity
00001 1
00002 1
00003 1

Solution

  • Assuming you are using MySQL (as the question is tagged), the following will return an arbitrary value for the other columns:

    select *
    from t
    group by code;
    

    However, the particular values being selected come from indeterminate rows.