postgresql中实现oracle SYS_CONNECT_BY_PATH
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即可。
postgresql中实现oracle SYS_CONNECT_BY_PATH的更多相关文章
- 如何在postgresql中模拟oracle的dual表,来测试数据库最基本的连接功能?
还好,网上弄到的,,没有dual的数据库,可以试图用select函数不带from数据表的方式来实现返回值. 一段测试代码: try: conn = psycopg2.connect(database= ...
- 在PostgreSQL中使用oracle_fdw访问Oracle
本文讲述如何在PostgreSQL中使用oracle_fdw访问Oracle上的数据. 1. 安装oracle_fdw 可以参照:oracle_fdw in github 编译安装oracle_fdw ...
- Postgresql中临时表(temporary table)的特性和用法
熟悉Oracle的人,相比对临时表(temporary table)并不陌生,很多场景对解决问题起到不错的作用,开源库Postgresql中,也有临时表的概念,虽然和Oracle中临时表名字相同,使用 ...
- Postgresql中的数据类型大全
一.数值类型: 下面是PostgreSQL所支持的数值类型的列表和简单说明: 名字 存储空间 描述 范围 smallint 2 字节 小范围整数 -32768 到 +32767 integer 4 字 ...
- Postgresql中的large object
1.初识postgresql large object 一位同事在对使用pg_dump备份出来的文件(使用plain格式)进行恢复时,觉得速度非常慢,让我分析一下是什么原因. 我拿到他的.bak文件, ...
- 用ArcMap在PostgreSQL中创建要素类需要执行”create enterprise geodatabase”吗
问:用Acmap在PostgreSQL中创建要素类需要执行"create enterprise geodatabase"吗? 关于这个问题,是在为新员工做postgresql培训后 ...
- PostgreSQL中定时job执行(pgAgent)
PostgreSQL中定时job执行 业务分析 近期项目需要定期清理数据库中的多余数据,即每月1号删除指定表中一年以上的数据. 初步分析这种定时job可以使用一下两种技术实现: Linux的cront ...
- 通过arcgis在PostgreSQL中创建企业级地理数据库
部署环境: Win7 64位旗舰版 软件版本: PostgreSQL-9.1.3-2-windows-x64 Postgis-pg91x64-setup-2.0.6-1 Arcgis 10.1 SP1 ...
- 在64位SQL Server中创建Oracle的链接服务器
当我们同时使用SQL Server和Oracle来存储数据时,经常会用到跨库查询.为了方便使用跨库查询,一个最好的办法就是通过创建链接服务器来实现.既可以在SQL Server中创建Oracle的链接 ...
随机推荐
- 管道(Pipe)----计算机进程间通信
参至他人博客:https://blog.csdn.net/u011583316/article/details/83419805
- linux的管道 |和grep命令以及一些其他命令(diff,echo,cat,date,time,wc,which,whereis,gzip,zcat,unzip,sort)
linux提供管道符号“|”,作用是命令1的输出内容作为命令2的输入内容.通常与grep命令一起使用. 格式:命令1 |命令2 grep命令:全称为global regular expression ...
- Scala学习笔记(6)对象
1.单例对象.Scala没有静态方法或字段,可以使用object这个语法结构来达到同样的目的.对象定义了单个实例,包含了你想要的特性. object Accounts{ def newUniqueNu ...
- Houdini:也许是你未曾听过的最振奋人心的 CSS 进化
原文链接:Houdini: Maybe The Most Exciting Development In CSS You’ve Never Heard Of更多译文将陆续推出,欢迎点赞+收藏+关注我的 ...
- 如何设置一个App的缓存机制
在手机应用程序开发中,为了减少与服务端的交互次数,加快用户的响应速度,一般都会在iOS设备中加一个缓存的机制,前面一篇文章介绍了iOS设备的内存缓存,这篇文章将设计一个本地缓存的机制. 功能需求 这个 ...
- spring通过注解转换日期
Spring中有@DataTimeFormat和@JsonFormat @JsonFormat不要忘了加GMT+8 @DateTimeFormat要注意前台传过的日期格式是yyyy-MM-dd的形式, ...
- 牛客假日团队赛2 C 修围栏 ( 哈夫曼树,贪心)
链接:https://ac.nowcoder.com/acm/contest/924/C 来源:牛客网 修围栏 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言6 ...
- At grand 022 GCD序列构造 dp/floyd二进制变换最少费用
A diverse words指的是每一个字母在单词中出现的次数不超过1的单词 题目要求你求出字典序比当前给定单词大的字典序最小单词 1.如果给定的单词长度小于26 就遍历一次在单词尾部加上字典序最小 ...
- SQL查询优化的步骤
一.定位慢查询 SQL优化的一般步骤:先查询mysql数据库运行状况,然后定位慢查询,再分析sql的执行过程,然后进行优化 1.使用show status查询数据库的运行状况 //显示数据库运行状态 ...
- 前端每日实战:126# 视频演示如何用纯 CSS 创作小球变矩形背景的按钮悬停效果
效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/yxbEzJ 可交互视频 此视频是可 ...