select headerid,colum1,
 status=case when exists(select * from body where header.headerid=body.headerid and flag<>-1) then 'REJECT' else 'APPROVE' end
from header

解决方案 »

  1.   

    create view viewname as
    select 
        a.headerid,
        a.colum1,
        case b.num when 0 then 'APPROVE' else 'REJECT' end as status
    from 
        header a
    inner join
        (select headerid,count(*) as num from body where flag <> -1 group by headerid) b
    where
        a.headerid = b.headerid
      

  2.   

    create view viewname as
    select a.*,status='APPROVE' from header a
           where not exists (select 1 from body 
                  where headerid=a.headerid
                        and flag in (0,1))
    union allselect a.*,status='REJECT' from header a
           where  exists (select 1 from body 
                  where headerid=a.headerid
                        and flag in (0,1))
      

  3.   

    --哦,改一点
    create view viewname as
    select a.*,status='APPROVE' from header a
           where not exists (select 1 from body 
                  where headerid=a.headerid
                        and flag in (0,1))
    union allselect a.*,status='REJECT' from header a
           where  exists (select 1 from body 
                  where headerid=a.headerid
                        and flag in (0,1) and flag<>-1)
      

  4.   

    select a.headerid,a.colum1,b.status
    from header a
    join
    (select headerid, case when exists(select count(*) from body  group by flag having count(*)=1 and flag=-1) then 'APPROVE' else 'REJECT' end
    from body) b
    on a.headerid=b.headerid
      

  5.   

    select a.headerid,a.colum1,b.status
    from header a
    join
    (select headerid, status=(case when exists(select count(*) from body  group by flag having count(*)=1 and flag=-1) then 'APPROVE' else 'REJECT' end)
    from body) b
    on a.headerid=b.headerid
      

  6.   

    select headerid,colum1
    ,status=case 
    when exists(select * from body where headerid=h.headerid and flag<>-1)
    then 'REJECT' else 'APPROVE' end
    from header h
      

  7.   

    select a.headerid,a.colum1,b.status
    from header a,(
    select headerid,status=case when count(*)=sum(case when flag=-1 then 1 else 0 end) then 'APPROVE' else 'REJECT' end
    from body group by headerid
    )b where a.headerid=b.headerid
      

  8.   

    TO  zjcxc(邹建) :select headerid,colum1
    ,status=case 
    when exists(select * from body where headerid=h.headerid and flag<>-1)
    then 'REJECT' else 'APPROVE' end
    from header hselect a.headerid,a.colum1,b.status
    from header a,(
    select headerid,status=case when count(*)=sum(case when flag=-1 then 1 else 0 end) then 'APPROVE' else 'REJECT' end
    from body group by headerid
    )b where a.headerid=b.headerid,第一个有问题吗?我怎么觉的也是对的????