name zzdate(进入日期) lzdate(离职日期)
张三 2002-09-08 NULL (null代表还在)
李四 2003-07-07 2004-07-07
王二 2004-02-07 Nullselect count(*) as 在职人数 from table where datepart(month,zzdate)=6 and lzdate=null
张三 2002-09-08 NULL (null代表还在)
李四 2003-07-07 2004-07-07
王二 2004-02-07 Nullselect count(*) as 在职人数 from table where datepart(month,zzdate)=6 and lzdate=null
and (convert(datetime,lzdate,120)>='2004-06-01' or lzdate is null)
where lzdate > '2004/6/30' or lzdate = null group by name
from T
where zzdate <= '2004-06-30'
and isnull(lzdate,'9999-12-31') > lzdate
from T
where zzdate <= '2004-06-30'
and isnull(lzdate,'9999-12-31') > '2004-06-30'
我想查询人员xxxx年x月份在职人员总数如何得之
不固定的年月怎么判断 日期
比如: 我要查2003年2月份在职的人数在比如可能我要查 2003年12月份在职的人数
and (lzdate < convert(datetime,'2004-06-01') or lzdate is null)
insert into @tb1
select '张三', '2002-09-08', NULL union all
select '李四', '2003-07-07', '2004-07-07' union all
select '王二', '2004-02-07', Null
--我想查询人员2004年6月份在职人员总数如何得之?
select * from @tb1 where 离职日期 is null--就一句就可以了吧?
insert into @tb1
select '张三', '2004-06-30', NULL union all
select '李四', '2003-07-07', '2004-07-07' union all
select '王二', '2004-02-07', Null
--我想查询人员2004年6月份在职人员总数如何得之?
select * from @tb1 where 离职日期 is null and datediff(day,进入日期,cast('2004-07-01' as datetime))>=1
/*
name 进入日期 离职日期
----- ----------------- -------------------
张三 2004-06-30 00:00:00.000 NULL
王二 2004-02-07 00:00:00.000 NULL(所影响的行数为 2 行)
declare @tb1 table ([name] varchar(20),进入日期 datetime,离职日期 datetime)
insert into @tb1
select '张三', '2004-07-01', NULL union all
select '李四', '2003-07-07', '2004-07-07' union all
select '王二', '2004-02-07', Null
--我想查询人员2004年6月份在职人员总数如何得之?
select * from @tb1 where 离职日期 is null and datediff(day,进入日期,cast('2004-07-01' as datetime))>=1/*
name 进入日期 离职日期
----- ------------ ------------
王二 2004-02-07 00:00:00.000 NULL(所影响的行数为 1 行)
*/
再比如2003年6月在职的 name zzdate(进入日期) lzdate(离职日期)
张三 2002-09-08 NULL (null代表还在)
李四 2003-07-07 2004-07-07
王二 2004-02-07 Null
赵四 2004-03-09 2004-07-09
武六 2004-06-09 Null
钱七 2003-06-09 2003-07-09应该是2
否则:
select count(*)
from T
where zzdate <= '2004-06-30'
and isnull(lzdate,'9999-12-31') > '2004-06-01'