Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select distinct values from 1 column

I want to select distinct values from only one column (the BoekingPlaatsId column) with this query:

SELECT MAX(BoekingPlaatsId), BewonerId, Naam, VoorNaam FROM table GROUP BY BewonerId, Naam, VoorNaam 

How do I do that in SQL Server?

like image 676
Parm Avatar asked Dec 16 '09 23:12

Parm


People also ask

How do I select distinct values in one column?

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.

How can I get distinct values for a particular column in SQL?

To get unique or distinct values of a column in MySQL Table, use the following SQL Query. SELECT DISTINCT(column_name) FROM your_table_name; You can select distinct values for one or more columns. The column names has to be separated with comma.

How do I select multiple values in one column in SQL?

Note – Use of IN for matching multiple values i.e. TOYOTA and HONDA in the same column i.e. COMPANY. Syntax: SELECT * FROM TABLE_NAME WHERE COLUMN_NAME IN (MATCHING_VALUE1,MATCHING_VALUE2);

Does select distinct apply to all columns?

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


1 Answers

DISTINCT should work if you just want the user names:

SELECT DISTINCT BewonerId, Naam, Voornaam FROM TBL 

but if you need the minimum ID values, group by the names...

SELECT MIN(BoekingPlaatsId), MIN(BewonerId), Naam, Voornaam FROM TBL GROUP BY Naam, Voornaam 
like image 102
doza Avatar answered Sep 21 '22 12:09

doza