具有父结构和子结构的ORACLE SQL查询
我有两个表,分别是账户tbl和客户tbl,结构如下: 客户待定具有父结构和子结构的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、原则、支持的规则如下
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