欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 运维知识 > 数据库 >内容正文

数据库

oracle子查询为游标结果集,这个SQL怎么破?select后的爬树子查询(connect by)变换

发布时间:2024/10/8 数据库 21 豆豆
生活随笔 收集整理的这篇文章主要介绍了 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)变换的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。