如有一个表
工号 拉号 入厂日期 离职日期
01 A1 2010/10/01 2010/10/10
02 A1 2010/10/01 2010/10/05
03 A2 2010/10/02 2010/10/05
有一个WEB页面,输入日期范围2010/10/02—2010/10/06
怎样用SQL语句显示以下资料
日期 拉号 员工总数
2010/10/02 A1 2
2010/10/02 A2 1
2010/10/03 A1 2
2010/10/03 A2 1
2010/10/04 A1 2
2010/10/04 A2 1
2010/10/05 A1 2
2010/10/05 A2 1
2010/10/06 A1 1
2010/10/06 A2 0

解决方案 »

  1.   

    统计一天的简单
    select 拉号,count(1) 员工总数 from T where '2010/10/02' between 入厂日期 and 离职日期 group by 拉号
    统计一个日期范围的没好办法,得用循环了
    declare @d datetime
    set @d='2010/10/02'
    while @d<'2010/10/06'
    begin
    select @d 日期,拉号,count(1) 员工总数 from T where @d between 入厂日期 and 离职日期 group by 拉号
    select @d=dateadd(day,1,@d)
    end
      

  2.   


    create table #tb (工号 varchar(10),拉号  varchar(10),入厂日期 datetime,离厂日期 datetime)
    insert #tb 
    select '01','A1', '2010/10/01', '2010/10/10' union all
    select '02' ,'A1' ,'2010/10/01', '2010/10/05' union all
    select '03', 'A2', '2010/10/02', '2010/10/05'
    declare @startdate datetime
    declare @enddate datetime
    set @startdate = '2010-10-02'
    set @enddate = '2010-10-06'
    select dateadd(dd,number,@startdate),拉号,count(*) from #tb, master..spt_values 
    where type = 'p' and dateadd(dd,number,@startdate) <= @enddate group by 拉号,number/*结果
    2010-10-02 00:00:00.000 A1 2
    2010-10-02 00:00:00.000 A2 1
    2010-10-03 00:00:00.000 A1 2
    2010-10-03 00:00:00.000 A2 1
    2010-10-04 00:00:00.000 A1 2
    2010-10-04 00:00:00.000 A2 1
    2010-10-05 00:00:00.000 A1 2
    2010-10-05 00:00:00.000 A2 1
    2010-10-06 00:00:00.000 A1 2
    2010-10-06 00:00:00.000 A2 1*/
      

  3.   

    DECLARE @a TABLE(gh VARCHAR(20), lh VARCHAR(20),rcrc SMALLDATETIME,lzrc SMALLDATETIME)
    INSERT @a SELECT '01','A1','2010/10/01','2010/10/10'
    union all select '02','A1','2010/10/01','2010/10/05'
    union all select '03','A2','2010/10/02','2010/10/05'DECLARE @x1 SMALLDATETIME,@x2 SMALLDATETIME
    SELECT @x1='2010-10-02',@x2='2010-10-06'SELECT x,lh,SUM(CASE WHEN x BETWEEN rcrc AND lzrc THEN 1 ELSE 0 END) zs  
    FROM (SELECT dateadd(day,number,@x1) x FROM MASTER.dbo.spt_values 
      WHERE TYPE='P' AND number  BETWEEN 0 AND DATEDIFF(DAY,@x1,@x2))aa,@a bb 
    GROUP BY x,lh
    ORDER BY x
    /*
    x                                                      lh                   zs          
    ------------------------------------------------------ -------------------- ----------- 
    2010-10-02 00:00:00                                    A1                   2
    2010-10-02 00:00:00                                    A2                   1
    2010-10-03 00:00:00                                    A1                   2
    2010-10-03 00:00:00                                    A2                   1
    2010-10-04 00:00:00                                    A1                   2
    2010-10-04 00:00:00                                    A2                   1
    2010-10-05 00:00:00                                    A1                   2
    2010-10-05 00:00:00                                    A2                   1
    2010-10-06 00:00:00                                    A1                   1
    2010-10-06 00:00:00                                    A2                   0(所影响的行数为 10 行)
    */
      

  4.   

    语句有点问题,应该加上条件select dateadd(dd,number,@startdate),拉号,count(工号) from #tb, master..spt_values 
    where type = 'p' and (dateadd(dd,number,@startdate) <= @enddate) and 
    (dateadd(dd,number,@startdate) between 入厂日期 and 离厂日期)
       group by 拉号,number
      

  5.   

    /*if object_id('V') is not null drop table V
    create table V(id varchar(3),na varchar(3),startt datetime,endt datetime)insert V
    select '01', 'A1', '2010/10/01', '2010/10/10' union all
    select '02', 'A1', '2010/10/01', '2010/10/05' union all
    select '03', 'A2', '2010/10/02', '2010/10/05'*/select a.riqi,V.na,num=count(V.na) from 
    (select  
      riqi=dateadd(dd,number,'2010-10-2')
    from
      master..spt_values
    where
      type='p'
    and  
      dateadd(dd,number,'2010-10-2')<='2010-10-6')a,V
    where a.riqi between V.startt and V.endt
    group by na,a.riqi
    /*
    2010-10-02 00:00:00.000 A1 2
    2010-10-02 00:00:00.000 A2 1
    2010-10-03 00:00:00.000 A1 2
    2010-10-03 00:00:00.000 A2 1
    2010-10-04 00:00:00.000 A1 2
    2010-10-04 00:00:00.000 A2 1
    2010-10-05 00:00:00.000 A1 2
    2010-10-05 00:00:00.000 A2 1
    2010-10-06 00:00:00.000 A1 1
    */
      

  6.   

    select convert(char(10),dateadd(dd,b.number,'2010-10-02'),21) '日期',a.拉号,count(*) '员工总数'  from tb a,master..spt_values b
    where b.type='p' and dateadd(dd,b.number,'2010-10-02') <='2010-10-06'
    and dateadd(dd,b.number,'2010-10-02') between 入厂日期 and 离厂日期
    group by 拉号,number