12.表结构如下:
表A: Employee_sn     StartDate        EndDate 
1             '2007-01-02'    '2007-01-03'
1             '2007-01-05'    '2007-01-06'
1             '2007-01-09'    '2007-01-13'
2             '2007-02-02'    '2007-02-03'
3             '2007-04-02'    '2007-04-03'

表B: Employee_sn   CreateDate
1            '2007-01-02'
1            '2007-01-05'   
1            '2007-01-03'
3            '2007-04-02'
1            '2007-01-04'
1            '2007-01-02'
2            '2007-02-02'

要得到的结果:
Employee_sn     StartDate       EndDate       Count
1             '2007-01-02'    '2007-01-03'     3
1             '2007-01-05'    '2007-01-06'     1
1             '2007-01-09'    '2007-01-13'     0
2             '2007-02-02'    '2007-02-03'     1
3             '2007-04-02'    '2007-04-03'     1
这个SQL文怎么写啊,从来都没写过这样的题啊。sql

解决方案 »

  1.   

    select *,(select count(*) from b where 
    a.Employee_sn=Employee_sn
    and  CreateDate between a.StartDate and a.EndDate 
    ) from a
      

  2.   

    select a.Employee_sn,a.startDate,a.EndDate,count(a.startDate)+count(b.createDate) as count
    from 表A a,表B b
    where a.Employee_sn=b.Employee_sn;
      

  3.   

    select a.Employee_sn,a.startDate,a.EndDate,count(a.startDate)+count(b.createDate) as count
    from 表A a,表B b
    where a.Employee_sn=b.Employee_sn
    group by a.Employee_sn,a.startDate,a.EndDate;