如果  tba中
id     passnum 
1       67
2       NULL
3       80tbb中
id    gradenum 
1      60.5
2      60
2      NULL
3      90
想当 tba中passnum为NULL时 得到 Y 
passnum有值时 如果tbb的gradenum大于tba中passnum为Y否则为N
select case passnum when is null  then  'Y'  when passnum>gradenum then 'Y' else 'N' end 
from tba  a,tbb b
where a.id=b.id
这样写都不行,得怎么写?passnum 和gradenum都是decimal(18, 1)

解决方案 »

  1.   

    select case when a.passnum is not null and a.passnum<b.gradenum then 'Y' else 'N' end  
    from tba a,tbb b
    where a.id=b.id
      

  2.   

    select 
     case when a.passnum is not null and a.passnum<b.gradenum 
          then 'Y' else 'N' end   
    from tba a,tbb b
    where a.id=b.id
      

  3.   

    当gradenum 或者 passnum 为NULL
    时也为'Y'
      

  4.   

    select 
     case when a.passnum is not null or b.gradenum is null  or a.passnum<b.gradenum 
          then 'Y' else 'N' end   
    from tba a,tbb b
    where a.id=b.id
      

  5.   

    测试过了OK的select a.ID,(case when passnum  IS null then 'Y' 
                else (case when passnum>gradenum then 'Y' else 'N' end) end) result
    from tba a,tbb b
    where a.id=b.id
      

  6.   

    select case passnum when is null then 'Y' when isnull(gradenum,0)>passnum then 'Y' else 'N' end  
    from tba a,tbb b
    where a.id=b.id
      

  7.   

    create table #tba
    (myid int,passnum int)insert into #tba
    select 1,67 union all
    select 2,null union all
    select 3,80create table #tbb
    (myid int ,gradenum int)
    insert into #tbb
    select 1,60 union all
    select 2,60 union all
    select 2,null union all
    select 3,90select myid,case when  passnum IS null then 'Y' else 
    case when passnum<(select top 1 gradenum from #tbb where myid=#tba.myid )
    then 'y' else 'n' end
     END as rusult  from #tba
      

  8.   

    select 
     case when a.passnum is not null or b.gradenum is null  or a.passnum<b.gradenum  then 'Y' else 'N' end   
    from
     tba a,tbb b
    where
     a.id=b.id
      

  9.   

    select a.*,b.*,case when passnum is null or passnum>gradenum then 'Y' else 'N' end  
    from tba a,tbb b
    where a.id=b.id
      

  10.   

    select case when a.passnum is not null and a.passnum<b.gradenum then 'Y' else 'N' end   
    from tba a,tbb b
    where a.id=b.id