例如表信息如下:
ID 姓名 年龄 时间
1 张三 20 ……
1 张 18 ……
1 李 23 ……
2 王五 18 ……
2 李 25 ……
4 王 28 ……
4 张麻 24 ……
我只想要每个ID的的第一条信息,想了时间不短了,不想再浪费时间了,想得到的信息如下!
ID 姓名 年龄 时间
1 张三 20 ……
2 王五 18 ……
4 王麻 28 ……
select
*
from
(select id0=row_number()over(partition by id order by getdate()),* from users) t
where
id0=1
查询每个ID至少有二个不同名字的数据信息
select id,userName From users where id in( select id from (select id,userName from users group by id,userName ) temp group by id
having count(temp.id)>1) group by id,userName
这个可能过于复杂,希望大家给与指定修改!