SQL Query获取列表中每个元素的前1个值

时间:2011-05-22 19:58:15

标签: sql-server-2005

enter image description here

我需要帮助编写一个返回所有列的查询

select id, city, road1, road2, lat, long 
from tbltest 
group by id, city, lat, long. 

最终结果应该是这样的enter image description here

谢谢, 爬完

2 个答案:

答案 0 :(得分:2)

虽然很奇怪,但是这里的表有重复的id列,这个查询会显示你需要的结果:

select id, city, road1, road2, lat, long
from
(
    select *, row_number() over(partition by id, city, lat, long order by road1, road2) RowNumber
    from tbltest 
) tt
where RowNumber = 1

答案 1 :(得分:0)

Using Common Table Expressions

;with tbl as
(
    select *, row_number() over(partition by id, city order by road1, road2) RowNumber
    from tbltest 
)

select * from tbl
where RowNumber = 1