sql查询说明

时间:2017-06-13 06:00:42

标签: sql oracle left-join

以下是用于从源表STG01_ACCOUNT_EBS_SRC_DUP到目标表STG02_ACCOUNT_F_DUP的数据选择的查询。在执行这些关节时,我正面临着可视化数据的困难时期。

- 我的理解是从P1P10创建了TEN表实例,并且从G1G11创建了ELEVEN列别名。

insert into STG02_ACCOUNT_F_DUP 
select distinct '10' HIER_ID, 'EXPIT' HIER_NAME, P1.PARENT_FLEX_VALUE G1, 
P1.FLEX_VALUE G2, P1.SUMMARY_FLAG_ORG, P1.SUMMARY_FLAG_CLS, P1.RANGE_ATTRIBUTE, P1.ENABLED_FLAG, 
P2.FLEX_VALUE G3, P2.SUMMARY_FLAG_ORG, P2.SUMMARY_FLAG_CLS, P2.RANGE_ATTRIBUTE, P2.ENABLED_FLAG, 
P3.FLEX_VALUE G4, P3.SUMMARY_FLAG_ORG, P3.SUMMARY_FLAG_CLS, P3.RANGE_ATTRIBUTE, P3.ENABLED_FLAG, 
P4.FLEX_VALUE G5, P4.SUMMARY_FLAG_ORG, P4.SUMMARY_FLAG_CLS, P4.RANGE_ATTRIBUTE, P4.ENABLED_FLAG, 
P5.FLEX_VALUE G6, P5.SUMMARY_FLAG_ORG, P5.SUMMARY_FLAG_CLS, P5.RANGE_ATTRIBUTE, P5.ENABLED_FLAG, 
P6.FLEX_VALUE G7, P6.SUMMARY_FLAG_ORG, P6.SUMMARY_FLAG_CLS, P6.RANGE_ATTRIBUTE, P6.ENABLED_FLAG, 
P7.FLEX_VALUE G8, P7.SUMMARY_FLAG_ORG, P7.SUMMARY_FLAG_CLS, P7.RANGE_ATTRIBUTE, P7.ENABLED_FLAG, 
P8.FLEX_VALUE G8, P8.SUMMARY_FLAG_ORG, P8.SUMMARY_FLAG_CLS, P8.RANGE_ATTRIBUTE, P8.ENABLED_FLAG, 
P9.FLEX_VALUE G10, P9.SUMMARY_FLAG_ORG, P9.SUMMARY_FLAG_CLS, P9.RANGE_ATTRIBUTE, P9.ENABLED_FLAG, 
P10.FLEX_VALUE G11, P10.SUMMARY_FLAG_ORG, P10.SUMMARY_FLAG_CLS, P10.RANGE_ATTRIBUTE, P10.ENABLED_FLAG, 
SYSDATE
from STG01_ACCOUNT_EBS_SRC_DUP P1, STG01_ACCOUNT_EBS_SRC_DUP P2, 
STG01_ACCOUNT_EBS_SRC_DUP P3, STG01_ACCOUNT_EBS_SRC_DUP P4, 
STG01_ACCOUNT_EBS_SRC_DUP P5, STG01_ACCOUNT_EBS_SRC_DUP P6, 
STG01_ACCOUNT_EBS_SRC_DUP P7, STG01_ACCOUNT_EBS_SRC_DUP P8, 
STG01_ACCOUNT_EBS_SRC_DUP P9, STG01_ACCOUNT_EBS_SRC_DUP P10
where P1.FLEX_VALUE = P2.PARENT_FLEX_VALUE (+)
and P2.FLEX_VALUE = P3.PARENT_FLEX_VALUE (+)
and P3.FLEX_VALUE = P4.PARENT_FLEX_VALUE (+)
and P4.FLEX_VALUE = P5.PARENT_FLEX_VALUE (+)
and P5.FLEX_VALUE = P6.PARENT_FLEX_VALUE (+)
and P6.FLEX_VALUE = P7.PARENT_FLEX_VALUE (+)
and P7.FLEX_VALUE = P8.PARENT_FLEX_VALUE (+)
and P8.FLEX_VALUE = P9.PARENT_FLEX_VALUE (+)
and P9.FLEX_VALUE = P10.PARENT_FLEX_VALUE (+)
and P1.PARENT_FLEX_VALUE = '80000'
order by P1.PARENT_FLEX_VALUE, P1.FLEX_VALUE, P2.FLEX_VALUE, P3.FLEX_VALUE, P4.FLEX_VALUE, 
P5.FLEX_VALUE, P6.FLEX_VALUE, P7.FLEX_VALUE, P8.FLEX_VALUE,
P9.FLEX_VALUE, P10.FLEX_VALUE;

您能否使用L EFT-JOIN来解释此块中发生的情况?

 where P1.FLEX_VALUE = P2.PARENT_FLEX_VALUE (+)
    and P2.FLEX_VALUE = P3.PARENT_FLEX_VALUE (+)
    and P3.FLEX_VALUE = P4.PARENT_FLEX_VALUE (+)
    and P4.FLEX_VALUE = P5.PARENT_FLEX_VALUE (+)
    and P5.FLEX_VALUE = P6.PARENT_FLEX_VALUE (+)
    and P6.FLEX_VALUE = P7.PARENT_FLEX_VALUE (+)
    and P7.FLEX_VALUE = P8.PARENT_FLEX_VALUE (+)
    and P8.FLEX_VALUE = P9.PARENT_FLEX_VALUE (+)
    and P9.FLEX_VALUE = P10.PARENT_FLEX_VALUE (+)
    and P1.PARENT_FLEX_VALUE = '80000'

谢谢!

1 个答案:

答案 0 :(得分:0)

你知道左连接是什么吗?

where P1.FLEX_VALUE = P2.PARENT_FLEX_VALUE (+)

表示:始终从P1返回记录,无论P2中是否存在匹配记录,如果P2中没有匹配记录,则返回NULL。请参阅此处:Oracle "(+)" Operator如果这不能解答您的问题,则需要进一步解释。

换句话说,这是在树上工作,如果树中没有父,那么仍然包括记录。

如果要删除(+),那么您的查询可能会返回非常小的