日期:2014-05-17  浏览次数:20802 次

有条件获取数据函数
有如下一张表:(代码如下),现在想得到的效果是:
如果我现在表中存在0101,则连Acode等于01的数据一同显示出来(表中允许存在01的数据但不一定有下级数据,也就是用它的Acode值作为Apcode),如果没有,则连Acode等于01的也不显示。

create table tmpa as 
select '01' as Acode ,'分组1' as Aname,'' as Apcode from dual;
union all
select '0101' as Acode ,'A1' as Aname,'01' as Apcode from dual;
union all
select '0102' as Acode ,'A2' as Aname,'01' as Apcode from dual;
union all
select '02' as Acode ,'分组2' as Aname,'' as Apcode from dual;
union all
select '0201' as Acode ,'B1' as Aname,'02' as Apcode from dual;
union all
select '0202' as Acode ,'B2' as Aname,'02' as Apcode from dual;
union all
select '0203' as Acode ,'B3' as Aname,'02' as Apcode from dual;
union all
select '03' as Acode ,'分组2' as Aname,'' as Apcode from dual;
union all
select '0301' as Acode ,'C1' as Aname,'03' as Apcode from dual;
union all
select '0302' as Acode ,'C2' as Aname,'03' as Apcode from dual;
union all
select '0303' as Acode ,'C3' as Aname,'03' as Apcode from dual;



------解决方案--------------------
SQL code

--本人想的不知道是你的意思不
select Acode,Aname,Apcode from tmpa
where Acode='0101' and instr(Acode,Apcode)>0

------解决方案--------------------
还是没看明白LZ的要求,你执行下这个查询
select acode,aname,apcode,level from tmpa start with acode ='0301'
connect by prior apcode=acode;

看看是不是你要的结果。