34,695
社区成员
发帖
与我相关
我的任务
分享
select * from table t
where vdatetime=(select max(vdatetime) from table where id=t.id)
---还是这样?
----------------------------------------------------------------
-- 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 行受影响)
*/
----------------------------------------------------------------
-- 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 行受影响)
*/
SELECT *
FROM TB T
WHERE NOT EXISTS(SELECT 1 FROM TB WHERE bkID=T.bkID AND vdatetime>T.vdatetime)
如果以bkID来分组----------------------------------------------------------------
-- 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 行受影响)
*/
select * from tb t
where not exists(select 1 from tb where fenquID=t.fenquID and bkID=t.bkID
and vdatetime>t.vdatetime)
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