我有一个多个SUM
的查询,它给出了比我预期更高的值。如果我将查询缩减到只有一个SUM
,一切正常,但只要我添加其他SUM
,我就会得到不正确的值。我尝试过在线查看并尝试了不同的方法,包括更改JOIN
和嵌套SELECT
,但仍然没有运气。所有帮助将不胜感激。
SELECT
A.WR_NO WORK_ORDER, A.WR_TYPE_CODE WR_TYPE, B.NAME PLANNER, A.SCHEDULING_POLYGON_CODE AREA, A.FINAL_CLOSE_DATE COMPLETION_DATE, C.ACTUAL_ITEM_AMT EST_ENG,
SUM(D.AFTER_COND_FCT_AMT) EST_CREW, SUM(E.AFTER_COND_FCT_AMT) ASB_CREW, SUM(F.AFTER_COND_FCT_AMT) EST_CONTR, SUM(G.AFTER_COND_FCT_AMT) ASB_CONTR
FROM
WORK_REQUEST A, CC.ALL_PEOPLE B, WR_CONTRIBUTION_VOUCHER C, WR_COST_ESTIMATE D, WR_COST_ESTIMATE E, WR_COST_ESTIMATE F, WR_COST_ESTIMATE G
WHERE
(A.FINAL_CLOSE_DATE BETWEEN TO_DATE ('10/01/2012','MM/DD/YYYY') AND TO_DATE ('10/04/2012','MM/DD/YYYY'))
AND A.ENTRY_PERSON_NO = B.PERSON_NO(+)
AND A.WR_NO = C.WR_NO(+)
AND A.WR_NO = D.WR_NO(+)
AND A.WR_NO = E.WR_NO(+)
AND A.WR_NO = F.WR_NO(+)
AND A.WR_NO = G.WR_NO(+)
AND (C.VOUCHER_CNTRB_TYPE_CODE(+) = 'ENGT')
AND (D.COST_TYPE_CODE(+) = 'LAB' AND D.DESIGN_ASBUILT_DA(+) = 'D' AND D.PROVIDER(+) = 'CO')
AND (E.COST_TYPE_CODE(+) = 'LAB' AND E.DESIGN_ASBUILT_DA(+) = 'A' AND E.PROVIDER(+) = 'CO')
AND (F.COST_TYPE_CODE(+) = 'LAB' AND F.DESIGN_ASBUILT_DA(+) = 'F' AND F.PROVIDER(+) = 'CN')
AND (G.COST_TYPE_CODE(+) = 'LAB' AND G.DESIGN_ASBUILT_DA(+) = 'G' AND G.PROVIDER(+) = 'CN')
GROUP BY
A.WR_NO, A.WR_TYPE_CODE, B.NAME, A.SCHEDULING_POLYGON_CODE, A.FINAL_CLOSE_DATE, C.ACTUAL_ITEM_AMT
ORDER BY 1
答案 0 :(得分:0)
您获得的值高于预期,因为连接是multiplyinjg行。如果不了解数据结构,外部人员很难准确地告诉您问题所在。
问题的解决方案是仅在计算所需的表上执行总计 。然后,您可以在该点之后加入其他表格/摘要。
顺便说一下,你真的应该使用正确的JOIN
语法。使用逗号暗示连接已经过时,并且使查询更难理解。
答案 1 :(得分:0)
选择
WO_ID WORK_ORDER,
WR_TYPE_CODE WR_TYPE,
NAME PLANNER,
SCHEDULING_POLYGON_CODE AREA,
FINAL_CLOSE_DATE COMPLETE_DATE,
SUM (EENG) AS "ENG_TIME",
SUM (ECREW) AS "EST_CREW",
SUM (ACREW) AS "ASB_CREW",
SUM (ECONT) AS "EST_CONTR",
SUM (ACONT) AS "ASB_CONTR"
这
(SELECT
A.WO_ID,
A.WR_TYPE_CODE,
B.NAME,
A. SCHEDULING_POLYGON_CODE,
A.FINAL_CLOSE_DATE,
C.ACTUAL_ITEM_AMT AS "EENG",
0 "ECREW",
0 "ACREW",
0 "ECONT",
0 "ACONT"
FROM
WORK_REQUEST A,
CC.ALL_PEOPLE B,
WR_CONTRIBUTION_VOUCHER C
WHERE
(A.FINAL_CLOSE_DATE BETWEEN TO_DATE ('10/01/2012','MM/DD/YYYY') AND TO_DATE ('10/31/2012','MM/DD/YYYY'))
AND A.ENTRY_PERSON_NO = B.PERSON_NO
AND A.WR_NO = C.WR_NO
AND (C.VOUCHER_CNTRB_TYPE_CODE = 'ENGT')
UNION
SELECT
A.WO_ID,
A.WR_TYPE_CODE,
B.NAME,
A. SCHEDULING_POLYGON_CODE,
A.FINAL_CLOSE_DATE,
0 "EENG",
D.AFTER_COND_FCT_AMT AS "ECREW",
0 "ACREW",
0 "ECONT",
0 "ACONT"
FROM
WORK_REQUEST A,
CC.ALL_PEOPLE B,
WR_COST_ESTIMATE D
WHERE
(A.FINAL_CLOSE_DATE BETWEEN TO_DATE ('10/01/2012','MM/DD/YYYY') AND TO_DATE ('10/31/2012','MM/DD/YYYY'))
AND A.ENTRY_PERSON_NO = B.PERSON_NO
AND A.WR_NO = D.WR_NO
AND (D.COST_TYPE_CODE = 'LAB' AND D.DESIGN_ASBUILT_DA = 'D' AND D.PROVIDER = 'CO')
UNION
SELECT
A.WO_ID,
A.WR_TYPE_CODE,
B.NAME,
A. SCHEDULING_POLYGON_CODE,
A.FINAL_CLOSE_DATE,
0 "EENG",
0 "ECREW",
E. AFTER_COND_FCT_AMT AS "ACREW",
0 "ECONT",
0 "ACONT"
FROM
WORK_REQUEST A,
CC.ALL_PEOPLE B,
WR_COST_ESTIMATE E
WHERE
(A.FINAL_CLOSE_DATE BETWEEN TO_DATE ('10/01/2012','MM/DD/YYYY') AND TO_DATE ('10/31/2012','MM/DD/YYYY'))
AND A.ENTRY_PERSON_NO = B.PERSON_NO
AND A.WR_NO = E.WR_NO
AND (E.COST_TYPE_CODE = 'LAB' AND E.DESIGN_ASBUILT_DA = 'A' AND E.PROVIDER = 'CO')
UNION
SELECT
A.WO_ID,
A.WR_TYPE_CODE,
B.NAME,
A. SCHEDULING_POLYGON_CODE,
A.FINAL_CLOSE_DATE,
0 "EENG",
0 "ECREW",
0 "ACREW",
F.AFTER_COND_FCT_AMT AS "ECONT",
0 "ACONT"
FROM
WORK_REQUEST A,
CC.ALL_PEOPLE B,
WR_COST_ESTIMATE F
WHERE
(A.FINAL_CLOSE_DATE BETWEEN TO_DATE ('10/01/2012','MM/DD/YYYY') AND TO_DATE ('10/31/2012','MM/DD/YYYY'))
AND A.ENTRY_PERSON_NO = B.PERSON_NO
AND A.WR_NO = F.WR_NO
AND (F.COST_TYPE_CODE = 'LAB' AND F.DESIGN_ASBUILT_DA = 'D' AND F.PROVIDER = 'CN')
UNION
SELECT
A.WO_ID,
A.WR_TYPE_CODE,
B.NAME,
A. SCHEDULING_POLYGON_CODE,
A.FINAL_CLOSE_DATE,
0 "EENG",
0 "ECREW",
0 "ACREW",
0 "ECONT",
G.AFTER_COND_FCT_AMT AS "ACONT"
FROM
WORK_REQUEST A,
CC.ALL_PEOPLE B,
WR_COST_ESTIMATE G
WHERE
(A.FINAL_CLOSE_DATE BETWEEN TO_DATE ('10/01/2012','MM/DD/YYYY') AND TO_DATE ('10/31/2012','MM/DD/YYYY'))
AND A.ENTRY_PERSON_NO = B.PERSON_NO
AND A.WR_NO = G.WR_NO
AND (G.COST_TYPE_CODE = 'LAB' AND G.DESIGN_ASBUILT_DA = 'A' AND G.PROVIDER = 'CN'))
GROUP BY
WO_ID, WR_TYPE_CODE, NAME, SCHEDULING_POLYGON_CODE, FINAL_CLOSE_DATE
ORDER BY 1 ASC