吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 569|回复: 0
收起左侧

[讨论] postgre 类似 start with connect by prior

[复制链接]
wuqingvika 发表于 2023-1-18 10:39
本帖最后由 wuqingvika 于 2023-1-18 11:08 编辑

需求:要将oracle迁移到postgre  里面用到了
[SQL] 纯文本查看 复制代码
SELECT
        so.*,
        connect_by_root ( so.org_id ) root_id 
FROM
        ( SELECT * FROM sys_organization WHERE TYPE = 30 ) so START WITH sys_level = 2 CONNECT BY PRIOR so.org_id = so.sup_id


要转变成postgre的写法,递归求 当前是在根(=2层算起的)第几层  

[SQL] 纯文本查看 复制代码
with recursive fg_tree as (
  select so.*,
         so.org_id as root_id -- <<< this is the "root" 
  from sys_organization so
  where TYPE = 30 and sys_level=2  -- <<< this is the "start with" part
  union all
  select c.*,
         p.sup_id
  from sys_organization c 
    join fg_tree p on  c.type=30 and p.org_id = c.sup_id -- <<< this is the "connect by" part
) 

select *
from fg_tree;

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-25 01:24

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表