SELECT DISTINCT
A.currency_code AS currency_code,
NVL (B.Acct_ID, A.Acct_ID) SECND_Acct_ID,
NVL (B.Type_currency_code, A.Type_currency_code) SECND_Type_currency_code,
NVL (B.Seg_Type, A.Seg_Type) SECND_Seg_Type,
NVL (B.B_Func_Code, A.B_Func_Code) SECND_B_Func_Code,
A.Acct_ID AS PRIM_Acct_ID,
A.Type_currency_code AS PRIM_Type_currency_code,
A.Seg_Type AS PRIM_Seg_Type,
A.B_Func_Code AS PRIM_B_FuncCode
FROM (SELECT Acct_ID,
Type_currency_code,
Seg_Type,
B_Func_Code,
B_ID,
B_NBR,
currency_code
FROM BAU
WHERE P_A_IND IN ('Y')) A
LEFT OUTER JOIN
(SELECT Acct_ID,
Type_currency_code,
Seg_Type,
B_Func_Code,
B_ID,
B_NBR,
currency_code
FROM BAU
WHERE P_A_IND IN ('N')) B
ON A.B_NBR = B.B_NBR
AND A.B_ID = B.B_ID
AND A.currency_code = B.currency_code
我想知道/看看是否有更好的方法来编写此查询。
答案 0 :(得分:1)
您可以通过将子查询中的条件分别移动到where
或on
子句来澄清查询:
SELECT ...
FROM BAU A
LEFT JOIN
BAU B
ON B.P_A_IND IN ('N')
AND A.B_NBR = B.B_NBR
AND A.B_ID = B.B_ID
AND A.currency_code = B.currency_code
WHERE A.P_A_IND IN ('Y')