Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Query Multiple Columns Using Distinct on One Column Only

I am trying to write a SQL query that selects multiple columns from a table with the distinct operator on one column only.

The table is simple. The columns are:

tblFruit_ID, tblFruit_FruitType, tblFruit_FruitName int          NVarChar            Text 

I am trying to select all the tblFruit_FruitType with their corresponding tblFruit_ID.

I have tried:

Select Distinct(tblFruit_FruitType), tblFruit_ID FROM tblFruit 

-Returns all results, not just distinct

Select tblFruit_FruitType, tblFruit_ID FROM tblFruit Group By tblFruit_FruitType 

-Errors with Column tblFruit_ID is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

Select tblFruit_FruitType, tblFruit_ID FROM tblFruit Group By tblFruit_FruitType, tblFruit_ID 

-Returns all results, not just distinct

I also checked out these similar posts and could not get anything to work :(

mySQL select one column DISTINCT, with corresponding other columns

SQL Server Distinct Union for one column

Hopefully this is enough information for an answer.

Thank you for your time!

EDIT (Sample Data and Desired Results)

tblFruit_ID, tblFruit_FruitType, tblFruit_FruitName int          NVarChar            Text 1            Citrus              Orange 2            Citrus              Lime 3            Citrus              Lemon 4            Seed                Cherry 5            Seed                Banana 

Results:

1            Citrus 4            Seed 
like image 562
Steve Avatar asked Aug 13 '12 15:08

Steve


People also ask

Can we apply distinct on single 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. Since DISTINCT operates on all of the fields in SELECT's column list, it can't be applied to an individual field that are part of a larger group.

Can you distinct multiple columns in SQL?

Answer. Yes, the DISTINCT clause can be applied to any valid SELECT query. It is important to note that DISTINCT will filter out all rows that are not unique in terms of all selected columns. Feel free to test this out in the editor to see what happens!

How do I use distinct on all columns in SQL?

SELECT DISTINCT FIELD1, FIELD2, FIELD3 FROM TABLE1 works if the values of all three columns are unique in the table. If, for example, you have multiple identical values for first name, but the last name and other information in the selected columns is different, the record will be included in the result set.

Does distinct apply to all columns in select?

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


2 Answers

select * from tblFruit where tblFruit_ID in (Select max(tblFruit_ID) FROM tblFruit group by tblFruit_FruitType) 
like image 167
valex Avatar answered Oct 07 '22 03:10

valex


You must use an aggregate function on the columns against which you are not grouping. In this example, I arbitrarily picked the Min function. You are combining the rows with the same FruitType value. If I have two rows with the same FruitType value but different Fruit_Id values for example, what should the system do?

Select Min(tblFruit_id) As tblFruit_id     , tblFruit_FruitType From tblFruit Group By tblFruit_FruitType 

SQL Fiddle example

like image 21
Thomas Avatar answered Oct 07 '22 01:10

Thomas