我在Oracle(12c)中有一个表,我需要检索数据 该表名为ACCOUNT_ANALYSIS_FY16。此表包含3级业务层次结构。此层次结构中的位置由DUNS_LEVEL列指定,其值为DUNS(表示业务的最低级别),IMMEDIATE(表示级别2,也称为DUNS级别的父级)或ULTIMATE,(表示级别3或父级别) IMMEDIATE)。
Applicable columns are:
NN_NAME - Account Name
DUNS_LEVEL
DUNS
ULTIMATE_PARENT_DUNS
IMMEDIATE_PARENT_DUNS
要求...对于给定的帐户名称(由于位置不同,可能会出现多次相同的帐户名称),我需要查看有关该帐户的所有内容(也称为SELECT *),AS WELL AS给定帐户的直接父母和最终父母。
我在SQL中编写了这个,但它涉及5个SELECT语句。我知道必须有一种更优雅和简单的方法来做到这一点。请注意,最终结果是将其放在匿名PL / SQL块中,因此如果这是一个更好的解决方案,则可以编写PL / SQL块。
这是我所拥有的直接SQL ...
Select *from ACCOUNT_ANALYSIS_FY16 where NN_NAME = 'XYZ GROUP'
union
Select * from ACCOUNT_ANALYSIS_FY16 where DUNS in (Select IMMEDIATE_PARENT_DUNS from ACCOUNT_ANALYSIS_FY16 where NN_NAME = 'XYZ GROUP')
union
Select * from ACCOUNT_ANALYSIS_FY16 where DUNS in (Select ULTIMATE_PARENT_DUNS from ACCOUNT_ANALYSIS_FY16 where NN_NAME = 'XYZ GROUP')
由于
答案 0 :(得分:1)
尝试:
SELECT *
FROM ACCOUNT_ANALYSIS_FY16 t
WHERE NN_NAME = 'XYZ GROUP'
OR EXISTS (
SELECT NULL
from ACCOUNT_ANALYSIS_FY16 t1
WHERE t.DUNS IN ( t1.ULTIMATE_PARENT_DUNS, t1.IMMEDIATE_PARENT_DUNS )
AND t1.NN_NAME = 'XYZ GROUP'
)