oracle子查询为游标结果集,这个SQL怎么破?select后的爬树子查询(connect by)变换
t_cum_info表有字段cust_code,prtn_id, t_pnm_partner 表有prtn_id(商戶ID), parent_id(上線商戶ID), partn_type字段。
樣例數據
cust_code? ?? ? prtn_id
--------------? ?? ???-------------
101? ?? ?? ?? ?? ???1
102? ?? ?? ?? ?? ???2
103? ?? ?? ?? ?? ???3
104? ?? ?? ?? ?? ???4
105? ?? ?? ?? ?? ???5
106? ?? ?? ?? ?? ???6
107? ?? ?? ?? ?? ???7
108? ?? ?? ?? ?? ???8
prtn_id? ?? ???parent_id? ?? ?? ? partn_type
------------? ?? ?-------------? ?? ?? ? ---------------
1? ?? ?? ?? ?? ?? ?2? ?? ?? ?? ?? ?? ?? ? PT101
2? ?? ?? ?? ?? ?? ?3? ?? ?? ?? ?? ?? ?? ? PT101
3? ?? ?? ?? ?? ?? ?4? ?? ?? ?? ?? ?? ?? ? PT403
4? ?? ?? ?? ?? ?? ?0? ?? ?? ?? ?? ?? ?? ? PT503
5? ?? ?? ?? ?? ?? ?6? ?? ?? ?? ?? ?? ?? ? PT101
6? ?? ?? ?? ?? ?? ?7? ?? ?? ?? ?? ?? ?? ? PT101
7? ?? ?? ?? ?? ?? ?8? ?? ?? ?? ?? ?? ?? ? PT403
8? ?? ?? ?? ?? ?? ?0? ?? ?? ?? ?? ?? ?? ? PT503
--希望獲取到每個非PT403類型的商戶的cust_code和商戶對應的頂層商戶ID
cust_code? ?? ?? ?? ?prtn_id
--------------? ?? ?? ???--------------
101? ?? ?? ?? ?? ?? ?? ?? ?? ? 3
102? ?? ?? ?? ?? ?? ?? ?? ?? ? 3
105? ?? ?? ?? ?? ?? ?? ?? ?? ? 7
106? ?? ?? ?? ?? ?? ?? ?? ?? ? 7
--原查詢語句如下,但我希望不要把子查詢放在select后(要建物化視圖),而是放在where或from后
怎么破?感謝!
select i.cust_code,
(select max(p.prtn_id)
from t_pnm_partner p
where p.prtn_type = 'PT403'
start with p.prtn_id = i.prtn_id
connect BY nocycle prior p.parent_id = p.prtn_id) as father
from t_cum_info i
WHERE i.prtn_id not in
(select p.prtn_id
from t_pnm_partner p
where p.prtn_type = 'PT403');
總結
以上是生活随笔為你收集整理的oracle子查询为游标结果集,这个SQL怎么破?select后的爬树子查询(connect by)变换的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 宝马740废气阀更换多少钱?
- 下一篇: oracle 存储过程字符替换,Orac