34,587
社区成员
发帖
与我相关
我的任务
分享
select *
from (select * from @t where datediff(d,聘任时间, '2005-05-06 ')> 0) a
where not exists(select 1
from ( select * from @t where datediff(d,聘任时间, '2005-05-06 ')> 0) b
where ( 人员ID = a.人员ID and datediff(d,聘任时间 , a.聘任时间) < 0) )
select * into # from @t where 聘任时间< '2005-05-06 23:59:59'
select *
from # a
where not exists(select 1
from #
where 人员ID = a.人员ID and 聘任时间 > a.聘任时间)
drop table #
declare @t table(ID int,人员ID int,聘任时间 datetime ,聘任名称 char(6))
insert @t select 1,2, '1998-8-9 ', '科员 '
insert @t select 2,3, '2001-5-6 ', '主任 '
insert @t select 3,2, '2001-8-9 ', '科长 '
insert @t select 4,2, '2004-5-1 ', '主任 '
insert @t select 5,4, '2006-8-9 ', '局长 '
insert @t select 6,5, '1995-07-01 ', '科员 '
insert @t select 7,5, '2000-07-01 ', '科长 '
insert @t select 8,5, '2005-07-01 ', '局长 '
select * into # from @t where datediff(d,聘任时间, '2005-05-06 ')> 0
select *
from # a
where not exists(select 1
from #
where ( 人员ID = a.人员ID and datediff(d,聘任时间 , a.聘任时间) < 0) )
drop table #
/*
ID 人员ID 聘任时间 聘任名称
----------- ----------- ------------------------------------------------------ ------
2 3 2001-05-06 00:00:00.000 主任
4 2 2004-05-01 00:00:00.000 主任
7 5 2000-07-01 00:00:00.000 科长
*/
(所影响的行数为 1 行)
(所影响的行数为 1 行)
(所影响的行数为 1 行)
(所影响的行数为 1 行)
(所影响的行数为 1 行)
ID 人员ID 聘任时间 聘任名称
----------- ----------- ------------------------------------------------------ ------
2 3 2001-05-06 00:00:00.000 主任
4 2 2004-05-01 00:00:00.000 主任
(所影响的行数为 2 行)
declare @t table(ID int,人员ID int,聘任时间 datetime ,聘任名称 char(6))
insert @t select 1,2,'1998-8-9','科员'
insert @t select 2,3,'2001-5-6','主任'
insert @t select 3,2,'2001-8-9','科长'
insert @t select 4,2,'2004-5-1','主任'
insert @t select 5,4,'2006-8-9','局长'
select *
from @t a
where not exists(select 1 from @t where 人员ID = a.人员ID and datediff(d,聘任时间 , a.聘任时间) < 0)
and datediff(d,聘任时间,'2005-05-06')>=0
/*
ID 人员ID 聘任时间 聘任名称
----------- ----------- ------------------------------------------------------ ------
2 3 2001-05-06 00:00:00.000 主任
4 2 2004-05-01 00:00:00.000 主任
*/