sql - SELECT Top 1 ID, DISTINCT Field -
i have table sample table follows:
id | city -------------- 1 | new york 2 | san francisco 3 | new york 4 | los angeles 5 | atlanta i select distinct city , top id each. e.g., conceptually following
select top 1 id, distinct city cities should give me:
id | city -------------- 1 | new york 2 | san francisco 4 | los angeles 5 | atlanta because new york appears twice, it's taken first id 1 in instance.
but error:
column 'cities.id' invalid in select list because not contained in either aggregate function or group clause.
try way:
select min(id), city cities group city min function used choose 1 of id 2 new york cities.
Comments
Post a Comment