具有分组集的SQL不会返回相同的结果

时间:2017-06-12 01:39:20

标签: oracle11g grouping-sets

我有2个分组设置查询。在进行分组设置之前,我已经检查过结果是完全一样的。 L_TIM_53_ID和L_TIM_54_ID的值总是2458120.但是我得到了以下2个查询的不同结果。

查询A,

SELECT NVL(TABLE_A.PRD_LVL_MEMBER_ID,2147483647) AS L_PRD_1_ID
,NVL(TABLE_A.TIM_52_ID,2147483647) AS L_TIM_52_ID
,NVL(TABLE_A.TIM_53_ID,2147483647) AS L_TIM_53_ID
,NVL(TABLE_A.TIM_54_ID,2147483647) AS L_TIM_54_ID
,sum(ADJ_BOOK_HISTORY) AS M_ADJ_BOOK_HISTORY
FROM (SELECT TIM_LEVEL_ID,TIM_LVL_MEMBER_ID,TIM_51_ID,TIM_54_ID,TIM_53_ID,TIM_52_ID,DCS_LVL_MEMBER_ID,PRD_LVL_MEMBER_ID,CUS_LVL_MEMBER_ID,ORG_LVL_MEMBER_ID FROM TABLE_A,(SELECT TIM_LEVEL_ID,TIM_LVL_MEMBER_ID,TIM_51_ID,TIM_54_ID,TIM_53_ID,TIM_52_ID
FROM MSC_DIMENSION_TIM_DYD
WHERE ((MSC_DIMENSION_TIM_DYD.TIM_53_ID IN (2458120)) AND (MSC_DIMENSION_TIM_DYD.TIM_54_ID IN (2458120)))
AND TIM_LEVEL_ID = 51
AND AGGR_TIM_LEVEL_ID = 51
GROUP BY TIM_LEVEL_ID,TIM_LVL_MEMBER_ID,TIM_51_ID,TIM_54_ID,TIM_53_ID,TIM_52_ID) MISSING_DATES) TABLE_A
,TABLE_B
WHERE TABLE_A.TIM_LVL_MEMBER_ID = TABLE_B.TIM_LVL_MEMBER_ID(+)
AND TABLE_A.DCS_LVL_MEMBER_ID = TABLE_B.DCS_LVL_MEMBER_ID(+)
AND TABLE_A.PRD_LVL_MEMBER_ID = TABLE_B.PRD_LVL_MEMBER_ID(+)
AND TABLE_A.CUS_LVL_MEMBER_ID = TABLE_B.CUS_LVL_MEMBER_ID(+)
AND TABLE_A.ORG_LVL_MEMBER_ID = TABLE_B.ORG_LVL_MEMBER_ID(+)
GROUP BY GROUPING SETS (
(TABLE_A.TIM_54_ID)
,(TABLE_A.PRD_LVL_MEMBER_ID,TABLE_A.TIM_52_ID,TABLE_A.TIM_53_ID,TABLE_A.TIM_54_ID)
)

查询B,

SELECT NVL(TABLE_A.PRD_LVL_MEMBER_ID,2147483647) AS L_PRD_1_ID
,NVL(TABLE_A.TIM_52_ID,2147483647) AS L_TIM_52_ID
,NVL(TABLE_A.TIM_53_ID,2147483647) AS L_TIM_53_ID
,NVL(TABLE_A.TIM_54_ID,2147483647) AS L_TIM_54_ID
,sum(ADJ_BOOK_HISTORY) AS M_ADJ_BOOK_HISTORY
FROM (SELECT TIM_LEVEL_ID,TIM_LVL_MEMBER_ID,TIM_51_ID,TIM_54_ID,TIM_53_ID,TIM_52_ID,DCS_LVL_MEMBER_ID,PRD_LVL_MEMBER_ID,CUS_LVL_MEMBER_ID,ORG_LVL_MEMBER_ID FROM TABLE_A,(SELECT TIM_LEVEL_ID,TIM_LVL_MEMBER_ID,TIM_51_ID,TIM_54_ID,TIM_53_ID,TIM_52_ID
FROM MSC_DIMENSION_TIM_DYD
WHERE ((MSC_DIMENSION_TIM_DYD.TIM_53_ID IN (2458120)))
AND TIM_LEVEL_ID = 51
AND AGGR_TIM_LEVEL_ID = 51
GROUP BY TIM_LEVEL_ID,TIM_LVL_MEMBER_ID,TIM_51_ID,TIM_54_ID,TIM_53_ID,TIM_52_ID) MISSING_DATES) TABLE_A
,TABLE_B
WHERE TABLE_A.TIM_LVL_MEMBER_ID = TABLE_B.TIM_LVL_MEMBER_ID(+)
AND TABLE_A.DCS_LVL_MEMBER_ID = TABLE_B.DCS_LVL_MEMBER_ID(+)
AND TABLE_A.PRD_LVL_MEMBER_ID = TABLE_B.PRD_LVL_MEMBER_ID(+)
AND TABLE_A.CUS_LVL_MEMBER_ID = TABLE_B.CUS_LVL_MEMBER_ID(+)
AND TABLE_A.ORG_LVL_MEMBER_ID = TABLE_B.ORG_LVL_MEMBER_ID(+)
GROUP BY GROUPING SETS (
(TABLE_A.TIM_54_ID)
,(TABLE_A.PRD_LVL_MEMBER_ID,TABLE_A.TIM_52_ID,TABLE_A.TIM_53_ID,TABLE_A.TIM_54_ID)
)

查询A的结果,

12143       2458120     2458120     2458120     25.4794520578945205476403721461486967
12143       2458151     2458120     2458120     23.0136986329369863010945296803923712
12143       2458179     2458120     2458120     25.4794520578945205476403721461486967
2147483647  2147483647  2147483647  2147483647  73.9726027487260273963752739726897647

查询B的结果,

12143       2458120     2458120     2458120 25.47945205789452054764037214614869673869
12143       2458151     2458120     2458120 23.01369863293698630109452968039237124785
12143       2458179     2458120     2458120 25.47945205789452054764037214614869673869
2147483647  2147483647  2147483647  2458120 73.97260274872602739637527397268976472523

我们可以清楚地看到发布的组集是针对 TABLE_A.TIM_54_ID ,我不知道这两个SQL有什么区别。为什么他们会返回不同的结果?

0 个答案:

没有答案