Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/73.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_Oracle - Fatal编程技术网

具有父结构和子结构的ORACLE SQL查询

具有父结构和子结构的ORACLE SQL查询,sql,oracle,Sql,Oracle,我有两个表,分别是账户tbl和客户tbl,结构如下: 客户待定 Customer_ID Account_ID Parent_Account_ID 3780952 3780952 3780952 3780997 3780997 3780997 3781004 3781004 3780997 客户tbl(客户群体有不同的价值,但我只对个人感兴趣) 确定PS/NOPS、原则、支持的规则如下

我有两个表,分别是账户tbl和客户tbl,结构如下:

客户待定

Customer_ID    Account_ID    Parent_Account_ID    
3780952        3780952       3780952
3780997        3780997       3780997
3781004        3781004       3780997       
客户tbl(客户群体有不同的价值,但我只对个人感兴趣)

确定PS/NOPS、原则、支持的规则如下:

**PS/NonPs**
Customer_ID equal to Parent_Account and Parent_Account is unique (not exist more than 1)  then NonPs.
Customer_ID equal to Parent_Account and Parent_Account is non unique OR -   Customer_ID is not equal to Parent_Account then PS    
**Principle** 
IF NonPS then Principle is Null
IF PS - If Customer_ID equal to Parent_Account then Principle is Y else N
**Supp**
IF NonPS then Supp is Null
IF PS - If Customer_ID not equal to Parent_Account then supp is Y else N
最终输出应该是这样的

Customer_ID    Account_ID    Parent_Account_ID   PS/NonPS   Principle   Supp
3780952        3780952       3780952             NonPS       Null        Null
3780997        3780997       3780997             PS          Y           N
3781004        3781004       3780997             PS          N           Y   
我已经尝试了很多次,但仍然无法获得输出。有人可以帮助我吗?

和:

与及:


您可以这样做:

select a.Customer_ID, a.Account_ID, a.Parent_Account_ID,
b.PS as 'PS/NonPS',
case when (b.PS = 'NonPs') then NULL else
  (case when (a.Customer_ID = a.Parent_Account_ID) then 'Y' else 'N' end)
end as 'Principle',
case when (b.PS = 'NonPs') then NULL else
  (case when (a.Customer_ID = a.Parent_Account_ID) then 'N' else 'Y' end)
end as 'supp'

from Account a
inner join (
select a.Customer_ID, a.Account_ID, a.Parent_Account_ID,
case when (a.Customer_ID = a.Parent_Account_ID)
  and (select count(ax.Account_Id) from Account ax where ax.Parent_Account_ID = a.Parent_Account_ID) = 1
   then 'NonPS'
   else 'Ps'
 end as 'PS'
 from Account a) b on a.Customer_ID = b.Customer_ID and a.Account_ID = b.Account_ID and a.Parent_Account_ID = b.Parent_Account_ID

您可以这样做:

select a.Customer_ID, a.Account_ID, a.Parent_Account_ID,
b.PS as 'PS/NonPS',
case when (b.PS = 'NonPs') then NULL else
  (case when (a.Customer_ID = a.Parent_Account_ID) then 'Y' else 'N' end)
end as 'Principle',
case when (b.PS = 'NonPs') then NULL else
  (case when (a.Customer_ID = a.Parent_Account_ID) then 'N' else 'Y' end)
end as 'supp'

from Account a
inner join (
select a.Customer_ID, a.Account_ID, a.Parent_Account_ID,
case when (a.Customer_ID = a.Parent_Account_ID)
  and (select count(ax.Account_Id) from Account ax where ax.Parent_Account_ID = a.Parent_Account_ID) = 1
   then 'NonPS'
   else 'Ps'
 end as 'PS'
 from Account a) b on a.Customer_ID = b.Customer_ID and a.Account_ID = b.Account_ID and a.Parent_Account_ID = b.Parent_Account_ID
select a.Customer_ID, a.Account_ID, a.Parent_Account_ID,
b.PS as 'PS/NonPS',
case when (b.PS = 'NonPs') then NULL else
  (case when (a.Customer_ID = a.Parent_Account_ID) then 'Y' else 'N' end)
end as 'Principle',
case when (b.PS = 'NonPs') then NULL else
  (case when (a.Customer_ID = a.Parent_Account_ID) then 'N' else 'Y' end)
end as 'supp'

from Account a
inner join (
select a.Customer_ID, a.Account_ID, a.Parent_Account_ID,
case when (a.Customer_ID = a.Parent_Account_ID)
  and (select count(ax.Account_Id) from Account ax where ax.Parent_Account_ID = a.Parent_Account_ID) = 1
   then 'NonPS'
   else 'Ps'
 end as 'PS'
 from Account a) b on a.Customer_ID = b.Customer_ID and a.Account_ID = b.Account_ID and a.Parent_Account_ID = b.Parent_Account_ID