select '1' as 优先级, * from 表 where (开始年月≦处理年月≦结束年月 你自己写一下)
union  all
select '2' as 优先级, * from 表 where (处理年月≦开始年月 你自己写一下)
union all
select '3' as 优先级, * from 表 where (结束年月≦处理年月 你自己写一下)再把优先级最高的同名用户取出来就可以了.

解决方案 »

  1.   


    IF EXISTS(SELECT * FROM TABLENAME WHERE 用户名='A' and 开始年月<='2003/06' and 结束年月>='2003/06')
       PRINT '一致的数据'
       SELECT * FROM TABLENAME WHERE 用户名='A' and 开始年月<='2003/06' and 结束年月>='2003/06'
      
    ELSE
       PRINT '未来的数据'
       SELECT * FROM TABLENAME WHERE 用户名='A' and 开始年月>='2003/06'
      

  2.   

    declare @yy int
    set @yy=200306select *  into #temp from
    (
    select 3 as 优先级,* from a
    where @yy between 开始年月 and 结束年月
    union 
    select 1,* from a
    where @yy<=开始年月
    union
    select 2,* from a
    where 结束年月<=@yy
    ) aa
    order by 优先级select 用户名,开始年月,结束年月
    from #temp
    where 优先级=(select min(优先级) from #temp)
      

  3.   


    select 用户名 ,   开始年月 ,  结束年月
    (select 用户名 ,   开始年月 ,  结束年月,case when 开始年月≦处理年月≦结束年月
    then 1 when 结束年月≦处理年月  then 2  when 处理年月≦开始年月 then 3 end as sort
    from table )
    order by sort
      

  4.   

    不用临时表
    select 用户名,开始年月,结束年月 from
    (
    select 3 as 优先级,* from a
    where @yy between 开始年月 and 结束年月
    union 
    select 1,* from a
    where @yy<=开始年月
    union
    select 2,* from a
    where 结束年月<=@yy
    ) aa
    where 优先级=case when exists(select 1 from a where @yy<=开始年月) then 1
                      when exists(select 1 from a where 结束年月<=@yy) then 2
                      else 3 end