需要SUM一个SQL语句

时间:2014-01-28 21:42:58

标签: sql select sum

我需要使用SUM来累计数据库中的所有工资。我无法让它上​​班。这是给我四行的陈述。除了“DIRECT SALARY COST”之外,所有其他列都相同。如何添加四个DIRECT SALARY COST(s),以便我可以拥有一条独特的线?

这是我的代码..

SELECT C.FOS_PROJ_ID_UNQU||'-'||C.FOS_PROJ_ID_STG||'-'||C.FOS_PROJ_ID_ELMT AS "STATE PROJECT ID",
  V.VEND_NM AS "VENDOR NAME",
  CC.WRKO_NB AS "WORK ORDER NUMBER",
  TO_CHAR(CCD.ATHZ_DT, 'MON-DD-YYYY') AS "AUTHORIZATION DATE",
    ----I need to SUM the following line ----
  TO_CHAR((CTD.DCT_SAL_COST_AMT),'$999,999,999.99') AS "DIRECT SALARY COST",
  TO_CHAR(CCC.OVHD_COST_AMT,'$999,999,999.99') AS "OVERHEAD COSTS",
  TO_CHAR(CTD.DCT_SAL_COST_AMT + CCC.OVHD_COST_AMT,'$999,999,999.99') AS "TOTAL"
FROM DOT1CARO.DT_VEND V, DOT1CARO.DT_CSPJ_CTRC CC, DOT1CARO.DT_CSPJ C, DOT1CARO.DT_CSPJ_CTRC_DTL CCD, DOT1CARO.DT_CSPJ_CTRC_COST CCC, DOT1CARO.DT_CTCST_TASK_DTL CTD
WHERE V.VEND_ID=CC.VEND_ID
AND CC.CSPJ_ID=C.CSPJ_ID
AND CC.CSPJ_CTRC_ID=CCD.CSPJ_CTRC_ID
AND CCD.CSPJ_CTRC_DTL_ID=CCC.CSPJ_CTRC_DTL_ID
AND CCC.CSPJ_CTCST_ID = CTD.CSPJ_CTCST_ID
AND CCD.ATHZ_DT = '14-NOV-2013'
AND V.VEND_NM LIKE 'Ka%
ORDER BY 2;

1 个答案:

答案 0 :(得分:0)

如果要在同一行中一起添加多个字段:

CTD.DCT_SAL_COST_AMT + CCC.OVHD_COST_AMT + .... AS Combined_Amounts

如果您希望跨行分别对其进行总计:

SELECT NonAggCol1, NonAggCol2, TO_CHAR((SUM(CTD.DCT_SAL_COST_AMT)),'$999,999,999.99') AS "DIRECT SALARY COST"
FROM Table1 a
JOIN Table2 b
  ON a.col1 = b.col1
JOIN Table3 c
  ON a.col1 = c.col1
GROUP BY NonAggCol1, NonAggCol2

如果要在行内和行之间添加它们,可以将上述两者结合起来。请务必在GROUP BY列表中包含每个非汇总字段。删除不推荐使用的连接语法是个好主意。