这个SQL语句,该怎么写?

KeepMoving 2011-03-25 01:22:01
例如表信息如下:
ID 姓名 年龄 时间
1 张三 20 ……
1 张 18 ……
1 李 23 ……
2 王五 18 ……
2 李 25 ……
4 王 28 ……
4 张麻 24 ……

我只想要每个ID的的第一条信息,想了时间不短了,不想再浪费时间了,想得到的信息如下!
ID 姓名 年龄 时间
1 张三 20 ……
2 王五 18 ……
4 王麻 28 ……
...全文
281 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
KeepMoving 2011-03-25
  • 打赏
  • 举报
回复
好了,明白了,多谢大家了!
liang145 2011-03-25
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 weiyanli20080 的回复:]

引用 5 楼 fredrickhu 的回复:
SQL code
select
*
from
(select id0=row_number()over(partition by id order by getdate(),* from tb)t
where
id0=1
这个能不能跟我解释下?不太明白!
[/Quote]

row_number()over(partition by id order by getdate(),可以理解为按id分组来编号~~
select id0=row_number()over(partition by id order by getdate(),* from tb, 这个查询结果大概是这样的:
id0 ID 姓名 年龄 时间
1 1 张三 20 ……
2 1 张 18 ……
3 1 李 23 ……
1 2 王五 18 ……
2 2 李 25 ……
1 4 王 28 ……
2 4 张麻 24 ……

再用where id0=1 把以上结果的id0=1的记录查出来~~打完~翻工~~ ^_^
KeepMoving 2011-03-25
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 fredrickhu 的回复:]
SQL code
select
*
from
(select id0=row_number()over(partition by id order by getdate(),* from tb)t
where
id0=1
[/Quote]这个能不能跟我解释下?不太明白!
liuhongJAVAEn 2011-03-25
  • 打赏
  • 举报
回复
上面的缺一个括号:
select
*
from
(select id0=row_number()over(partition by id order by getdate()),* from tb) t
where
id0=1
KeepMoving 2011-03-25
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 ssp2009 的回复:]
SQL code
select * from tb a where not exists(select 1 from tb where 姓名=a.姓名 and 时间<a.时间)
[/Quote]
我试了试,怎么什么也查不出来,也没错
倔強 2011-03-25
  • 打赏
  • 举报
回复
select * from tb a where not exists(select 1 from tb where 姓名=a.姓名 and 时间<a.时间)
gw6328 2011-03-25
  • 打赏
  • 举报
回复
select * from tb a where not exists(select 1 from tb b where b.id=a.id and b.time<a.time)

王麻 哈哈
oO寒枫Oo 2011-03-25
  • 打赏
  • 举报
回复

select * from tb a where not exists(select 1 from tb where ID=a.ID and 时间<a.时间)
--小F-- 2011-03-25
  • 打赏
  • 举报
回复
select
*
from
(select id0=row_number()over(partition by id order by getdate(),* from tb)t
where
id0=1
KeepMoving 2011-03-25
  • 打赏
  • 举报
回复
4 那个多写了个麻字,笔误!
快溜 2011-03-25
  • 打赏
  • 举报
回复
select * from tb a where not exists(select 1 from tb where 姓名=a.姓名 and 时间<a.时间)
nizhijie1984 2011-03-25
  • 打赏
  • 举报
回复
???
4 王麻 28 …… 28:24???

34,591

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧