求一条SQL语句! 各位路过的帮帮忙,不甚感谢!

seu31199113 2006-05-09 12:11:38
求一条SQL语句! 各位路过的帮帮忙,不甚感谢!

表结构:
编号1 编号2 时间
F01921 004 2006-05-03 00:00:00.000
F01921 004 2006-05-08 00:00:00.000
F01932 021 2006-05-03 00:00:00.000
F01932 021 2006-05-05 00:00:00.000
F01932 021 2006-05-07 00:00:00.000
F01932 005 2006-05-02 00:00:00.000
F01932 005 2006-05-03 00:00:00.000


如何用一句SQL取出下面三条记录:

F01921 004 2006-05-08 00:00:00.000
F01932 021 2006-05-07 00:00:00.000
F01932 005 2006-05-03 00:00:00.000

即如何取得编号1、编号2组合后,最近的时间的一条记录?
...全文
149 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
angel_bear 2006-05-09
  • 打赏
  • 举报
回复
楼上正解
子陌红尘 2006-05-09
  • 打赏
  • 举报
回复
--如果表中只有三个字段:
select 编号1,编号2,max(时间) as 时间 from 表 group by 编号1,编号2

--如果除了以上字段之外,还需要列出其他字段:
select a.* from 表 a where not exists(select 1 from 表 where 编号1=a.编号1 and 编号2=a.编号2 and 时间>a.时间)
OracleRoob 2006-05-09
  • 打赏
  • 举报
回复
select 编号1,编号2,max(时间) as 时间
from tablename
group by 编号1,编号2
xeqtr1982 2006-05-09
  • 打赏
  • 举报
回复
declare @t table(编号1 varchar(10),编号2 varchar(10),时间 datetime)
insert into @t select 'F01921','004','2006-05-03 00:00:00.000'
union all select 'F01921','004','2006-05-08 00:00:00.000'
union all select 'F01932','021','2006-05-03 00:00:00.000'
union all select 'F01932','021','2006-05-05 00:00:00.000'
union all select 'F01932','021','2006-05-07 00:00:00.000'
union all select 'F01932','005','2006-05-02 00:00:00.000'
union all select 'F01932','005','2006-05-03 00:00:00.000'

select * from @t a where not exists(select 1 from @t where 编号1=a.编号1 and 编号2=a.编号2 and 时间>a.时间)

xeqtr1982 2006-05-09
  • 打赏
  • 举报
回复
declare @t table(编号1 varchar(10),编号2 varchar(10),时间 datetime)
insert into @t select 'F01921','004','2006-05-03 00:00:00.000'
union all select 'F01921','004','2006-05-08 00:00:00.000'
union all select 'F01932','021','2006-05-03 00:00:00.000'
union all select 'F01932','021','2006-05-05 00:00:00.000'
union all select 'F01932','021','2006-05-07 00:00:00.000'
union all select 'F01932','005','2006-05-02 00:00:00.000'
union all select 'F01932','005','2006-05-03 00:00:00.000'

select * from @t a where not exists(select 1 from @t where 编号2=a.编号2 and 时间>a.时间)
冷箫轻笛 2006-05-09
  • 打赏
  • 举报
回复
select 编号1,编号2,max(时间)
from tablename
group by 编号1,编号2
chusaizhe 2006-05-09
  • 打赏
  • 举报
回复
select top 1 编号1,编号2,时间 from 表 group by 编号1,编号2 order by 时间 DESC
这样应该也可以!
noshape 2006-05-09
  • 打赏
  • 举报
回复
学习....

34,593

社区成员

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

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