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. Windows Server 2012 上安装 dotNET Framework v3.5

    Windows Server 2012不能直接运行dotNET Framework v3.5安装程序进行安装,系统提供通过服务器管理器的添加功能和角色向导进行安装. 安装的前几个步骤再这里略去,在默认 ...

  2. Windows下图文详解Mongodb安装及配置

    这两天接触了MongoDB数据库,发现和mysql数据库还是有很大差别的,同时使用前的配置看起来有些繁杂,踩过不少坑,其实只要一步一步搞清了,并不难. 接下来,我就整理下整个安装及配置过程. 安装的M ...

  3. centos7yum安装VirtualBox

    cd 进入目录:/etc/yum.repos.d 新建一个文件virtualbox.repo, 输入如下内容: [virtualbox] name=Oracle Linux / RHEL / Cent ...

  4. mysql 修改表字段默认值

    alter table 表名 alter column 字段名 drop default; (若本身存在默认值,则先删除) alter table 表名 alter column 字段名 set de ...

  5. 一条简单的 SQL 查询语句到底经历了什么?

    一.MySQL 基础架构   整体来说 MySQL 主要分为两个部分,一个部分是:Server 层,另一部分是:存储引擎层. 其中 Server 层包括有连接器.查询缓存.分析器.优化器.执行器等,存 ...

  6. 使用Log4Net将系统日志信息记录到记事本和数据库中

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/hxpjava1/article/details/32714855 一.使用Log4Net将日志记录到 ...

  7. vue中使用better-scroll滚动条插件

    应用场景: overflow: hidden会让超出的部分隐藏,并且无法拖拽,所以可使用插件让长列表限定的区域滚动拖拽. 参考:https://zhuanlan.zhihu.com/p/2740702 ...

  8. WPS专业版及序列号

    目录 1. 软件下载 2. 序列号(永久) 1. 软件下载 城通网盘:https://sn9.us/dir/13403389-35833830-3fc98f (仅Android版) 官网: https ...

  9. 【转】linux 查看哪些进程用了swap

    转自:http://blog.csdn.net/xiangliangyu/article/details/8213127 如果系统的物理内存用光了,则会用到swap.系统就会跑得很慢,但仍能运行;如果 ...

  10. 团队项目作业-Beta版本发布

    团队成员: 学号 姓名 201731062234 薛磊 201731062230 李林 201731062231 燕泓达 201731062232 陈东 201731062229 沈瑞琦 201731 ...