select t2.org_id ,nvl(t3.org_name||t2.org_name,t2.org_name)
from table2 t2
left outer join table1 t1 on t1.org_child_id=t2.org_id
left outer join table2 t3 on t1.org_father_id=t3.org_id
select t2.id, t3.name ¦ ¦t1.name
from table2 t1,table1 t2, table2 t3
where t1.id=t2.org_father_id(+) and t2.org_child_id=t3.id(+)
这个sql语句是错的,查询不到符合楼主的条件;talbe2没有id字段,把table2的org_id改为id查询的结果也不符合。
顶一下liangzi_ ,思路好好好噢。。。
select T_JISSEKI_LF_T_JISSEKI_TOKUSAI.*, T_TSUCHISYO_LOT-T_TSUCHISYO.Priority,T_TSUCHISYO_LOT-T_TSUCHISYO.TSUCHISYO_NO
from (
select TJISSEKI.NYUKEN_YMD,TJISSEKI.LOT_NO,TJISSEKI.GOUHI_KBN,
TJISSEKI.HANTEI_YMD,TJISSEKI.NONYU_SU,TJISSEKI.SAMPLE_YMD,TJISSEKITOKUSAI.TOKUSAI_NO
from
(
select * from
(
select *
from T_JISSEKI
where HINMEI='*L51399-OJ' and MAKER_CD='HAC3' and KYOTENKANRI_CD='A202' and
(GOUHI_KBN=1 or GOUHI_KBN=2) and DATA_STATUS=3
order by NYUKEN_YMD desc,SORTORDER_NYUKEN desc
)T_JISSEKI where rownum < 51
)TJISSEKI
left join
(select NYUKEN_ID , NYUKEN_SEQ , max(substr(sys_connect_by_path( TOKUSAI_NO, ','), 2)) TOKUSAI_NO
from
(
select NYUKEN_ID , NYUKEN_SEQ, TOKUSAI_NO, colc, lead(colc) over(partition by NYUKEN_ID , NYUKEN_SEQ order by colc) cold
from ( select NYUKEN_ID , NYUKEN_SEQ, TOKUSAI_NO , row_number() over(order by NYUKEN_ID , NYUKEN_SEQ) colc
from T_JISSEKI_TOKUSAI )
)
start with cold is null
connect by prior colc=cold
group by NYUKEN_ID , NYUKEN_SEQ
) TJISSEKITOKUSAI
on TJISSEKI.NYUKEN_ID=TJISSEKITOKUSAI.NYUKEN_ID
and TJISSEKI. NYUKEN_SEQ=TJISSEKITOKUSAI.NYUKEN_SEQ) T_JISSEKI_LF_T_JISSEKI_TOKUSAI
left join
( select T_TSUCHISYO_LOT.NYUKEN_ID,T_TSUCHISYO_LOT.NYUKEN_SEQ,
T_TSUCHISYO_LOT.TSUCHISYO_NO,T_TSUCHISYO.TSUCHISYO_NO,
T_TSUCHISYO.Priority
from T_TSUCHISYO_LOT,T_TSUCHISYO
where T_TSUCHISYO_LOT.TSUCHISYO_NO=T_TSUCHISYO.TSUCHISYO_NO) T_TSUCHISYO_LOT-T_TSUCHISYO
on T_TSUCHISYO_LOT-T_TSUCHISYO.NYUKEN_ID=T_JISSEKI_LF_T_JISSEKI_TOKUSAI.NYUKEN_ID
and T_TSUCHISYO_LOT-T_TSUCHISYO.NYUKEN_SEQ=JISSEKI_LF_T_JISSEKI_TOKUSAI.NYUKEN_SEQ
select id,name
from
(select
b.org_id fid,
(case when a.org_child_id is null then a.id else a.org_child_id end) id,
(case when a.org_child_id is null then a.org_name else b.org_name||a.org_name end) name
from
(select case when a.org_father_id is null then b.org_id else a.org_father_id end id,a.org_child_id,b.org_name from table1 a,table2 b where a.org_child_id(+)=b.org_id) a,
(select distinct b.org_id,b.org_name from table1 a,table2 b where a.org_father_id=b.org_id) b
where a.id=b.org_id(+)
)
order by fid,id;