简体   繁体   中英

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?

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM