Momar
Momar

Reputation: 187

How to select distinct with ID on the result?

How to select distinct from table including ID column on the result?

Like for example: (this is error query)

SELECT ID,City,Street from (SELECT distinct City, Street from Location)

The table Location

CREATE TABLE Location(
ID int identity not null,
City varchar(max) not null,
Street varchar(max) not null
)

Then it will show the column ID, distinct column City, distinct column Street

Is there a possible query to have this result?

Upvotes: 1

Views: 2186

Answers (1)

juergen d
juergen d

Reputation: 204924

If you want for instance the lowest id for the unique data you desire you can do

select min(id), City, Street 
from Location
group by City, Street

Generally you have to tell the DB what id to take using an aggregate function like min() or max()

Upvotes: 2

Related Questions