oracle

create table st as(
select '324' id,'黄埔' name,'220' cc from dual
union all select '325','^UP','110' from dual
union all select '326','^UP','小计' from dual
union all select '327','萝岗','500' from dual
union all select '328','^UP','220' from dual
union all select '329','^UP','110' from dual
union all select '330','^UP','小计' from dual
);

select v1.id,(case when (select count(1) from st where v1.name=id)=0 then v1.name
    when (select count(1) from st where v1.name=id)>0
    then (select name from st where v1.name=id) end) name,v1.cc
from(select t.id,(case when t.name ='^UP'
          then (select max(s.id) from st s where s.id<t.id and s.name !='^UP' )
          when t.name !='^UP' then t.name end) as name,t.cc
      from st t) v1;

你可能感兴趣的:(oracle)