0

我有一个带有多个SUMs 的查询,它给我的值比我预期的要高。如果我将查询缩减为一个SUM,一切正常,但只要我添加其他SUMs,我就会得到不正确的值。我尝试在网上查找并尝试了不同的方法,包括更改JOINs 和 nested SELECTs,但仍然没有运气。所有帮助将不胜感激。

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
4

2 回答 2

0

您获得的值比您预期的要高,因为连接是乘以行的。如果不了解数据结构,外部人员很难准确地告诉您问题出在哪里。

该问题的解决方案是只在计算它所需的表格上进行求和。然后,您可以在该点之后加入其他表/摘要。

顺便说一句,你真的应该使用正确的JOIN语法。使用逗号来表示连接已经过时了,并且使查询更难理解。

于 2012-12-04T22:34:17.517 回答
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')

联盟

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')

联盟

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')

联盟

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')

联盟

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'))

通过...分组

WO_ID, WR_TYPE_CODE, NAME, SCHEDULING_POLYGON_CODE, FINAL_CLOSE_DATE

按 1 ASC 订购

于 2012-12-06T14:56:43.997 回答