分组取最新记录的SQL如何写?实在不好意思了,没分了。

gdi_gtk1 2007-08-11 12:07:20
sbid revtime interfacetype value
08312 2007-8-11 11:00:00 7 12.90
08312 2007-8-11 12:00:00 7 12.21
08312 2007-8-11 12:00:00 10 112.90
如上记录,需要按interfacetype分组,然后取出时间最新的每个interfacetype的记录,就上面的记录而言,结果应该是最后两条记录合法。该怎么写?
...全文
324 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
qys2000 2007-08-11
  • 打赏
  • 举报
回复
create table t

(sbid varchar(10), revtime datetime, interfacetype int, value numeric(12,2))
insert into t
select '08312', '2007-8-11 11:00:00', 7 , 12.90 union all
select '08312', '2007-8-11 12:00:00', 7 , 12.21 union all
select '08312', '2007-8-11 12:00:00', 10, 112.90

select * from t a
where not exists(select 1 from t where interfacetype=a.interfacetype and revtime >a.revtime)
在路上_- 2007-08-11
  • 打赏
  • 举报
回复
select *
from @Test a
where revtime =
(select max(revtime) from @Test where interfacetype =
a.interfacetype)
Limpire 2007-08-11
  • 打赏
  • 举报
回复
declare @test table (sbid varchar(10), revtime datetime, interfacetype int, value money)
insert @test
select '08312', '2007-8-11 11:00:00', '7', 12.90 union all
select '08312', '2007-8-11 12:00:00', '7', 12.21 union all
select '08312', '2007-8-11 12:00:00', '10', 112.90

select * from @Test a where revtime in (select top 1 revtime from @Test where interfacetype = a.interfacetype order by revtime desc)
-- or
select * from @Test a where revtime in (select max(revtime) from @Test where interfacetype = a.interfacetype)
fa_ge 2007-08-11
  • 打赏
  • 举报
回复
create table t

(sbid varchar(10), revtime datetime, interfacetype int, value numeric(12,2))
insert into t
select '08312', '2007-8-11 11:00:00', 7 , 12.90 union all
select '08312', '2007-8-11 12:00:00', 7 , 12.21 union all
select '08312', '2007-8-11 12:00:00', 10, 112.90

select * from t a
where not exists(select 1 from t where interfacetype=a.interfacetype and revtime >a.revtime)

sbid revtime interfacetype value
---------- ------------------------------------------------------ ------------- --------------
08312 2007-08-11 12:00:00.000 7 12.21
08312 2007-08-11 12:00:00.000 10 112.90

(2 row(s) affected)
Limpire 2007-08-11
  • 打赏
  • 举报
回复
declare @test table (sbid varchar(10), revtime datetime, interfacetype int, value money)
insert @test
select '08312', '2007-8-11 11:00:00', '7', 12.90 union all
select '08312', '2007-8-11 12:00:00', '7', 12.21 union all
select '08312', '2007-8-11 12:00:00', '10', 112.90

select * from @Test a where revtime in (select top 1 revtime from @Test where interfacetype = a.interfacetype order by revtime desc)
fa_ge 2007-08-11
  • 打赏
  • 举报
回复


select * from t a
where not exists(select 1 from t where interfacetype=a.interfacetype and revtime >a.revtime)
fa_ge 2007-08-11
  • 打赏
  • 举报
回复
sbid revtime interfacetype value
08312 2007-8-11 11:00:00 7 12.90
08312 2007-8-11 12:00:00 7 12.21
08312 2007-8-11 12:00:00 10 112.90
如上记录,需要按interfacetype分组,然后取出时间最新的每个interfacetype的记录,就上面的记录而言,结果应该是最后两条记录合法。该怎么写?

-----------
select * from t a
where not exists(select 1 from t where interfacetype=a.interfacetype and revtime <a.revtime)

34,587

社区成员

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

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