Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLite - SELECT DISTINCT of one column and get the others

Tags:

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 that with SELECT DISTINCT Code FROM table (00001,00002,00003) but with all of the other table columns.

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

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

And I would like to get:

    Code    Quantity              -----   --------            00001      1                00002      1                        00003      1  

Thanks in advance!

like image 285
javiazo Avatar asked Jun 24 '13 13:06

javiazo


People also ask

How do I get unique values from a column in SQLite?

Introduction to SQLite SELECT DISTINCT clause In this syntax: First, the DISTINCT clause must appear immediately after the SELECT keyword. Second, you place a column or a list of columns after the DISTINCT keyword. If you use one column, SQLite uses values in that column to evaluate the duplicate.

How do I get distinct from one column in SQL?

Adding the DISTINCT keyword to a SELECT query causes it to return only unique values for the specified column list so that duplicate rows are removed from the result set.

Does SELECT distinct apply to all columns?

Yes, DISTINCT works on all combinations of column values for all columns in the SELECT clause.

How can I get distinct values from multiple columns in SQL?

Select with distinct on all columns of the first query. Select with distinct on multiple columns and order by clause. Count() function and select with distinct on multiple columns.


1 Answers

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.

like image 196
Gordon Linoff Avatar answered Oct 13 '22 14:10

Gordon Linoff