Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用于获取oracle sql中同一表中可用行值的查询_Sql_Database_Oracle - Fatal编程技术网

用于获取oracle sql中同一表中可用行值的查询

用于获取oracle sql中同一表中可用行值的查询,sql,database,oracle,Sql,Database,Oracle,我需要使用join查询获取同一表中可用的行值 例如: Id (Primary Key) Name Address Parent Organization ID (Foreign Key) reference (Id) 1 Sup Org1 Address1 null 2 Sup Org2 Address2 null 3 Sup Org3 Address3

我需要使用join查询获取同一表中可用的行值

例如:

Id (Primary Key)   Name       Address   Parent Organization ID (Foreign Key) reference (Id)

1                  Sup Org1   Address1  null

2                  Sup Org2   Address2  null

3                  Sup Org3   Address3  null

4                  Sub Org5   Address4  1
输出:

Name                Address

Sup Org1            Address1  

Sup Org2            Address2

Sup Org3            Address3 

Sub Org5(Sup Org1)  Address4  
除超级组织值外,所有子组织都应附加 映射的超级组织(例如:子组织5(组织1))


有谁能帮我写一个查询,像上面的输出格式那样获取数据呢?你要找的过程叫做“自连接”。sql zoo提供了一些可能有助于提高技能的课程

没有提供小提琴,但我觉得这个很合适:

select sub.name||coalesce('('||sup.name||')',''),address
from table_name as sub
left join table_name as sup on sub.parent=sup.id

我没有测试,但这应该包括

Select 
   t1.Name || Decode(NVL(t2.Name , ''), '', '', '(' || t2.Name || ')'), -- here we convert null to empty string and don't wrap empty string into parenthesis
   Address
From Mytable t1 left join
     Mytable t2 on t1.id = t2.Parent
下面是SQL语句(下次遇到类似问题时应提供该语句)和SQL语句:

SELECT o2.Name, case when o1.Name is not null then '(' || o1.Name || ')' END, o2.Parent_Organization_ID, 01.Address
FROM Organization o1
RIGHT JOIN Organization o2 on o1.id = o2.Parent_Organization_ID
ORDER BY o2.NAME