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

解决方案 »

  1.   

    Select count(name) from table where lzdate not in null
      

  2.   

    select * from yourtable where convert(datetime,zzdate,120)<='2004-06-01'
    and (convert(datetime,lzdate,120)>='2004-06-01' or lzdate is null)
      

  3.   

    select count(name) as 在职人数 from table 
    where lzdate > '2004/6/30' or lzdate = null group by name
      

  4.   

    select count(*)
    from T
    where zzdate <= '2004-06-30'
          and isnull(lzdate,'9999-12-31') > lzdate
      

  5.   

    select count(*)
    from T
    where zzdate <= '2004-06-30'
          and isnull(lzdate,'9999-12-31') > '2004-06-30'
      

  6.   

    多问一句 
      我想查询人员xxxx年x月份在职人员总数如何得之 
      不固定的年月怎么判断 日期
      

  7.   

    xxxx年x月 离职的 算什么?
      

  8.   

    只有 playyuer(退休干部 卧鼠藏虫) 符合我意
      

  9.   

    因为我的日期不是固定的,随时变化 是否就得考虑 日期 '2004-06-30' 比如2月份就没有30日,这样如何在sql中考虑体现,
      

  10.   

    希望 playyuer(退休干部 卧鼠藏虫) 告知举个例子
    比如: 我要查2003年2月份在职的人数在比如可能我要查 2003年12月份在职的人数
                 
      

  11.   

    select * from yourtable where zzdate<=convert(datetime,'2004-06-01') 
    and (lzdate < convert(datetime,'2004-06-01') or lzdate is null)
      

  12.   

    declare @tb1 table ([name] varchar(20),进入日期 datetime,离职日期 datetime)
    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--就一句就可以了吧?
      

  13.   

    看错了,不好意思,应该是:declare @tb1 table ([name] varchar(20),进入日期 datetime,离职日期 datetime)
    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 行)
      

  14.   

    --如果把张三的进入日期改成'2004-07-01'说明他是7月份进入的,不满足条件,所以只有1条记录:
    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 行)
    */
      

  15.   

    这里的结果没有一个是对的
     
    再比如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
      

  16.   

    那就没错!
    否则:
    select count(*)
    from T
    where zzdate <= '2004-06-30'
          and isnull(lzdate,'9999-12-31') > '2004-06-01'
      

  17.   

    谢谢大家,只是分值太少,最终得解 再次谢 playyuer(退休干部 卧鼠藏虫)