sql语句,查出每组中时间最近的一条记录

sigridl 2009-12-09 01:24:32
fenquID bkID ID vdatetime(时间)
1 1 1 2009-12-02 10:50:45
1 1 2 2009-12-02 11:50:45
1 2 1 2009-12-01 10:50:45

一个fenquID 包括好几个bkID,一个bkID包括好几个ID
...全文
795 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
athloner 2009-12-10
  • 打赏
  • 举报
回复
8楼正解!
qurihong 2009-12-09
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 sigridl 的回复:]
引用 8 楼 fredrickhu 的回复:
SQL code---还是这样?
----------------------------------------------------------------
-- Author  :fredrickhu(我是小F,向高手学习)
-- Date    :2009-12-09 13:26:45
-- Version:
--      Microsoft SQL Server 2005 - 9.0?-


我想要的就是这种,谢谢你!
我还想问一下,能不能同时查出每组中的记录条数?
[/Quote]

select *,(select count(*) from table_test where fenquID=a.fenquID) as record_num from table_test as a where vdatetime = ( select max(vdatetime) from table_test where fenquID=a.fenquID)
Rotel-刘志东 2009-12-09
  • 打赏
  • 举报
回复
select * from  table t
where vdatetime=(select max(vdatetime) from table where id=t.id)
qurihong 2009-12-09
  • 打赏
  • 举报
回复
1. select * from table_test where vdatetime in (select max(vdatetime) from table_test group by fenquID)

2. select * from table_test as a where vdatetime = ( select max(vdatetime) from table_test where fenquID=a.fenquID)
sigridl 2009-12-09
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 fredrickhu 的回复:]
SQL code---还是这样?
----------------------------------------------------------------
-- Author :fredrickhu(我是小F,向高手学习)
-- Date :2009-12-09 13:26:45
-- Version:
-- Microsoft SQL Server 2005 - 9.0?-
[/Quote]

我想要的就是这种,谢谢你!
我还想问一下,能不能同时查出每组中的记录条数?
bobge 2009-12-09
  • 打赏
  • 举报
回复
每组(fenqu)的最新记录
select t.*
from table as t left join (
select fenquID max(vdatetime)
group by fenquID
) as t2 on t.fenquID = t2.fenquID and t.vdatetime = t2.vdatetime
快乐_石头 2009-12-09
  • 打赏
  • 举报
回复
--小F-- 2009-12-09
  • 打赏
  • 举报
回复
---还是这样?
----------------------------------------------------------------
-- Author :fredrickhu(我是小F,向高手学习)
-- Date :2009-12-09 13:26:45
-- Version:
-- Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86)
-- Nov 24 2008 13:01:59
-- Copyright (c) 1988-2005 Microsoft Corporation
-- Developer Edition on Windows NT 5.2 (Build 3790: Service Pack 1)
--
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([fenquID] int,[bkID] int,[ID] int,[vdatetime] datetime)
insert [tb]
select 1,1,1,'2009-12-02 10:50:45' union all
select 1,1,2,'2009-12-02 11:50:45' union all
select 1,2,1,'2009-12-01 10:50:45'
--------------开始查询--------------------------
select
*
from
tb t
where
vdatetime=(select max(vdatetime) from tb where fenquID=t.fenquID and bkID=t.bkID)
----------------结果----------------------------
/* fenquID bkID ID vdatetime
----------- ----------- ----------- -----------------------
1 2 1 2009-12-01 10:50:45.000
1 1 2 2009-12-02 11:50:45.000

(2 行受影响)
*/
--小F-- 2009-12-09
  • 打赏
  • 举报
回复
----------------------------------------------------------------
-- Author :fredrickhu(我是小F,向高手学习)
-- Date :2009-12-09 13:26:45
-- Version:
-- Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86)
-- Nov 24 2008 13:01:59
-- Copyright (c) 1988-2005 Microsoft Corporation
-- Developer Edition on Windows NT 5.2 (Build 3790: Service Pack 1)
--
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([fenquID] int,[bkID] int,[ID] int,[vdatetime] datetime)
insert [tb]
select 1,1,1,'2009-12-02 10:50:45' union all
select 1,1,2,'2009-12-02 11:50:45' union all
select 1,2,1,'2009-12-01 10:50:45'
--------------开始查询--------------------------
select
*
from
tb t
where
vdatetime=(select max(vdatetime) from tb where bkID=t.bkID)
----------------结果----------------------------
/* fenquID bkID ID vdatetime
----------- ----------- ----------- -----------------------
1 2 1 2009-12-01 10:50:45.000
1 1 2 2009-12-02 11:50:45.000

(2 行受影响)
*/
sgtzzc 2009-12-09
  • 打赏
  • 举报
回复
SELECT * 
FROM TB T
WHERE NOT EXISTS(SELECT 1 FROM TB WHERE bkID=T.bkID AND vdatetime>T.vdatetime)
如果以bkID来分组
--小F-- 2009-12-09
  • 打赏
  • 举报
回复
----------------------------------------------------------------
-- Author :fredrickhu(我是小F,向高手学习)
-- Date :2009-12-09 13:26:45
-- Version:
-- Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86)
-- Nov 24 2008 13:01:59
-- Copyright (c) 1988-2005 Microsoft Corporation
-- Developer Edition on Windows NT 5.2 (Build 3790: Service Pack 1)
--
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([fenquID] int,[bkID] int,[ID] int,[vdatetime] datetime)
insert [tb]
select 1,1,1,'2009-12-02 10:50:45' union all
select 1,1,2,'2009-12-02 11:50:45' union all
select 1,2,1,'2009-12-01 10:50:45'
--------------开始查询--------------------------
select
*
from
tb t
where
vdatetime=(select max(vdatetime) from tb where id=t.id)
----------------结果----------------------------
/* fenquID bkID ID vdatetime
----------- ----------- ----------- -----------------------
1 1 2 2009-12-02 11:50:45.000
1 1 1 2009-12-02 10:50:45.000

(2 行受影响)
*/
快乐_石头 2009-12-09
  • 打赏
  • 举报
回复
select * from tb t 
where not exists(select 1 from tb where fenquID=t.fenquID and bkID=t.bkID
and vdatetime>t.vdatetime)
--小F-- 2009-12-09
  • 打赏
  • 举报
回复
select 
*
from
tb t
where
vdatetime=(select max(vdatetime) from tb where id=t.id)
  • 打赏
  • 举报
回复
select fenquID , bkID ,   ID   , vdatetime=max(vdatetime)
from tb
group by fenquID , bkID , ID
sgtzzc 2009-12-09
  • 打赏
  • 举报
回复
以哪个来分组?

34,695

社区成员

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

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