select * from tablename A inner join (
select 姓名,min(开始日期) as 开始日期 ,max(结束日期) as 结束日期
from tablename group by 姓名 ) B
on A.姓名=B.姓名 and A.开始日期=B.开始日期 and A.结束日期=B.结束日期
where A.费用类别 ='出差补助'

解决方案 »

  1.   


    select * from tablename A inner join (
    select 姓名,min(开始日期) as 开始日期 ,max(结束日期) as 结束日期
    from tablename group by 姓名 ) B
    on A.姓名=B.姓名 and A.开始日期>=B.开始日期 and A.结束日期<=B.结束日期
    where A.费用类别 ='出差补助'
      

  2.   

    select * 
    from 表 a
    where a.费用类别="出差补助"  
    and not exists(select 1 from 表 where 费用类别 in('市内交通费','住宿费用','在途费用') and (开始日期 between a.开始日期 and a.结束日期 or 结束日期 between a.开始日期 and a.结束日期) )
      

  3.   

    Select * from tablenames awhere isnull((select count(id) from tablenames 
             where Begindate>= a.begindate and enddate<=a.enddate and typed<>'出差补助' ),0)=0
    and a.typed='出差补助'
      

  4.   

    --生成测试数据
    create table #t(姓名 varchar(10),费用类别 varchar(10),开始日期 datetime,结束日期 datetime)
    insert into #t select 'a',rtrim('出差补助')  ,'2005-05-01','2005-05-10'
    insert into #t select 'a',rtrim('市内交通费'),'2005-05-04','2005-05-10'
    insert into #t select 'a',rtrim('住宿费用')  ,'2005-05-01','2005-05-03'
    insert into #t select 'a',rtrim('在途费用')  ,'2005-05-03','2005-05-04'
    insert into #t select 'a',rtrim('出差补助')  ,'2005-05-11','2005-05-20'
    insert into #t select 'a',rtrim('市内交通费'),'2005-05-14','2005-05-20'
    insert into #t select 'b',rtrim('出差补助')  ,'2005-05-01','2005-05-10'
    insert into #t select 'b',rtrim('住宿费用')  ,'2005-05-01','2005-05-03'
    insert into #t select 'b',rtrim('在途费用')  ,'2005-05-03','2005-05-04'--执行查询操作
    select 
        a.* 
    from 
        #t a
    where
        a.费用类别 = '出差补助'
        and
        (not exists(select 1 from #t where  姓名=a.姓名 and 开始日期>=a.开始日期 and 结束日期<=a.结束日期 and 费用类别='市内交通费') 
         or
         not exists(select 1 from #t where  姓名=a.姓名 and 开始日期>=a.开始日期 and 结束日期<=a.结束日期 and 费用类别='住宿费用') 
         or
         not exists(select 1 from #t where  姓名=a.姓名 and 开始日期>=a.开始日期 and 结束日期<=a.结束日期 and 费用类别='在途费用') )
    --输出查询结果
    姓名      费用类别           开始日期               结束日期
    -----     --------           ----------             ----------
    b         出差补助           2005-05-01             2005-05-10
    a         出差补助           2005-05-11             2005-05-20
      

  5.   

    Select * from tablenames awhere NOT EXISTS((select count(id) from tablenames 
             where Begindate>= a.begindate and enddate<=a.enddate and typed<>'出差补助' ))
    and a.typed='出差补助'