oracle:

select sys_connect_by_path(t2.ID, '/') as PATH, t1.id 
            from HTD_DEVICETYPE_RELATION t1
            inner join HTD_DEVICETYPE_RESOURCE t2 on t1.CHILD_RESOURCE_TYPE_ID = t2.ID
            start with t1.parent_resource_type_id = 100 and t1.id > 1 connect by nocycle
      prior t2.id = t1.parent_resource_type_id order by t1.id

postgresql:

with recursive r_table(path,cycle,t1id,t2id,parent_resource_type_id) as
(
     select array[t.t2id],false,t.t1id,t.t2id,parent_resource_type_id from
     
     (select t1.id as t1id,t2.id as t2id,t1.parent_resource_type_id as parent_resource_type_id
     from HTD_DEVICETYPE_RELATION as t1
     inner join HTD_DEVICETYPE_RESOURCE as t2 on
     t1.CHILD_RESOURCE_TYPE_ID = t2.ID where t1.parent_resource_type_id = 100 and t1.id > 1) as t
     
     union all
     select path || b.t2id,b.t2id = any(path),b.t1id,b.t2id,b.parent_resource_type_id
     from r_table
     inner join
     (select t1.id as t1id,t2.id as t2id,t1.parent_resource_type_id as parent_resource_type_id
     from HTD_DEVICETYPE_RELATION as t1
     inner join HTD_DEVICETYPE_RESOURCE as t2 on
     t1.CHILD_RESOURCE_TYPE_ID = t2.ID) as b
     
     on r_table.t2id = b.parent_resource_type_id and not cycle
)select '/' || Array_to_string(path,'/'),t1id from r_table where not cycle order by t1id;          ?column?         | t1id
--------------------------+------
 /31                      |    9
 /31/32                   |   10
 /102                     | 1519
 /103                     | 1520
 /104                     | 1521
 /105                     | 1522
 /102/115                 | 1523
 /102/160                 | 1524
 /102/103/115             | 1526
 /103/115                 | 1526
 /103/160                 | 1527
 /102/103/160             | 1527
 /102/103/113             | 1528
 /103/113                 | 1528
 /104/160                 | 1530
 /102/104/160             | 1530
 /102/105/115             | 1531
 /105/115                 | 1531
 /102/105/160             | 1532
 /105/160                 | 1532
 /102/105/113             | 1533
 /105/113                 | 1533
 /484                     | 1534
 /491                     | 1554
 /102/104/114             | 1556
 /104/114                 | 1556
 /185                     | 1596
 /154                     | 1614
 /493                     | 1634
 /504                     | 1635
--More--

实例中通过array数值保存访问过的id,b.t2id = any(path)检查是否已经访问过来避免产生死循环

在递归查询时,如出现如下错误

是由于数据库表字段类型numeric(20,0)不支持with递归查询,将数据库表字段改为bigint即可。

链接1

链接2

链接3

postgresql中实现oracle SYS_CONNECT_BY_PATH的更多相关文章

  1. 如何在postgresql中模拟oracle的dual表,来测试数据库最基本的连接功能?

    还好,网上弄到的,,没有dual的数据库,可以试图用select函数不带from数据表的方式来实现返回值. 一段测试代码: try: conn = psycopg2.connect(database= ...

  2. 在PostgreSQL中使用oracle_fdw访问Oracle

    本文讲述如何在PostgreSQL中使用oracle_fdw访问Oracle上的数据. 1. 安装oracle_fdw 可以参照:oracle_fdw in github 编译安装oracle_fdw ...

  3. Postgresql中临时表(temporary table)的特性和用法

    熟悉Oracle的人,相比对临时表(temporary table)并不陌生,很多场景对解决问题起到不错的作用,开源库Postgresql中,也有临时表的概念,虽然和Oracle中临时表名字相同,使用 ...

  4. Postgresql中的数据类型大全

    一.数值类型: 下面是PostgreSQL所支持的数值类型的列表和简单说明: 名字 存储空间 描述 范围 smallint 2 字节 小范围整数 -32768 到 +32767 integer 4 字 ...

  5. Postgresql中的large object

    1.初识postgresql large object 一位同事在对使用pg_dump备份出来的文件(使用plain格式)进行恢复时,觉得速度非常慢,让我分析一下是什么原因. 我拿到他的.bak文件, ...

  6. 用ArcMap在PostgreSQL中创建要素类需要执行”create enterprise geodatabase”吗

    问:用Acmap在PostgreSQL中创建要素类需要执行"create enterprise geodatabase"吗? 关于这个问题,是在为新员工做postgresql培训后 ...

  7. PostgreSQL中定时job执行(pgAgent)

    PostgreSQL中定时job执行 业务分析 近期项目需要定期清理数据库中的多余数据,即每月1号删除指定表中一年以上的数据. 初步分析这种定时job可以使用一下两种技术实现: Linux的cront ...

  8. 通过arcgis在PostgreSQL中创建企业级地理数据库

    部署环境: Win7 64位旗舰版 软件版本: PostgreSQL-9.1.3-2-windows-x64 Postgis-pg91x64-setup-2.0.6-1 Arcgis 10.1 SP1 ...

  9. 在64位SQL Server中创建Oracle的链接服务器

    当我们同时使用SQL Server和Oracle来存储数据时,经常会用到跨库查询.为了方便使用跨库查询,一个最好的办法就是通过创建链接服务器来实现.既可以在SQL Server中创建Oracle的链接 ...

随机推荐

  1. strtoul()要优于atoi()函数---C语言

    strtoul():将字符串转为长整型整数 atoi():将字符串转为整型整数 在32位STM32中,int是32位的,如果字符串是“3123456789”,大于0x7fff fff,用atoi()函 ...

  2. UUID工具类及使用

    1.工具类: package UUIdtest; import java.util.UUID; public class UUIDUtil { public static String getUUID ...

  3. Text Autosizer&&解决移动端网页文本字体怪异增大问题

    在做移动端页面时,有时你设置了字体大小,有的部分即使设置了行内样式也不生效,而有些显示正常,这个特性就是Text Autosizer在搞鬼. 以下是解决方案: ①给元素设置 -webkit-text- ...

  4. php strpos() 函数介绍与使用方法详解

    本文主要和大家介绍PHP中mb_strpos的使用技巧,通过使用语法以及实例给大家详细分析了用法,需要的朋友参考学习下.希望能帮助到大家.mb_strpos(PHP 4 >= 4.0.6, PH ...

  5. 声明对象的方式/构造函数/原型/this指向

      函数的发展历程(声明函数的方式):     1.通过Object构造函数或字面量的方式创建单个对象 var obj = new Object; obj.name="新华"; o ...

  6. ES6中class的实现原理

    一.在ES6以前实现类和继承 实现类的代码如下: function Person(name, age) { this.name = name; this.age = age; } Person.pro ...

  7. 继续死磕python

    一.数据运算 算术运算 比较运算 赋值运算 逻辑运算 成员运算 身份运算 位运算 其中左右移运算是逻辑左右移即缺失位补0,而算数右移缺失补符号位(注意逻辑运算都是补码运算即都取补码再运算,然后结果也是 ...

  8. Array.prototype

    Array.prototype  属性表示 Array 构造函数的原型,并允许您向所有Array对象添加新的属性和方法. /* 如果JavaScript本身不提供 first() 方法, 添加一个返回 ...

  9. 企业微信的corpsecret在哪里?

      问题: 查看“企业微信”的官方开发文档,在“获取access_token”部分提到,使用GET请求方法,访问 https://qyapi.weixin.qq.com/cgi-bin/gettoke ...

  10. pandas数据排序(series排序 & DataFrame排序)

    # pandas数据排序 # series的排序: # Series.sort_values(ascending = True,inplace = False) # 参数说明: # ascending ...