日期:2014-05-17  浏览次数:20508 次

一个简单的查询,忘了怎么弄
表的内容如下

我想按照ids的顺序,取出每个id的第一条数据。如图,那么就应该得到第1和第3条记录。

不知道sql语句怎么写。

------解决方案--------------------
select * from tb as t
where ids=(select min(ids) from tb where id=t.id)
------解决方案--------------------
SELECT * FROM tb a
WHERE NOT EXISTS
(
SELECT 1
FROM tb b
WHERE b.id = a.id
AND b.ids < a.ids
)

------解决方案--------------------
select *
from (select *,rn=row_number() over(partition by id order by ids) from tb)t
where rn=1
------解决方案--------------------
select * from table where ids in 
(select min(ids) from table group by id)
------解决方案--------------------

select t.* from
(
  select id, min(ids) from t group by t.id
) a inner join t on a.ids=t.ids

t是你那个表的表名