处理多行

时间:2011-11-30 07:00:28

标签: sql oracle oracle11g

我根据单位ID,行ID,CURR_DATE汇总'INTERIM_AUTO'表格中的数据,即

SELECT INTERIM_AUTO_ID,
       UNIT_ID,
       PROD_LINE_ID,
       CURR_DATE,
       BAL_AMOUNT,
       V_SOURCE_CODE,
       CURR_ID,
       CREATED_BY,
       UPDATED_DATETIME
  FROM INTERIM_AUTO
 WHERE CURR_DATE = to_date(V_DATE, 'MM/DD/YYYY')
 GROUP BY UNIT_ID,
          PROD_LINE_ID,
          CURR_DATE

然后,如果我们获得上述组合的多个条目,那么我必须对BAL_Amount进行求和,并考虑上述组合的单个条目与此计算的金额。我该如何实现这一目标?

1 个答案:

答案 0 :(得分:1)

听起来你想要计算 BAL_AMOUNT 列的总和。您可以使用SUM函数执行此操作。

如果您在 BAL_AMOUNT 列上使用SUM,则必须将所有其他列添加到GROUP BY

   SELECT INTERIM_AUTO_ID,
          UNIT_ID,
          PROD_LINE_ID,
          CURR_DATE,
          SUM(BAL_AMOUNT) as SUM_BAL_AMOUNT,
          V_SOURCE_CODE,
          CURR_ID,
          CREATED_BY,
          UPDATED_DATETIME
     FROM INTERIM_AUTO
    WHERE CURR_DATE = to_date(V_DATE, 'MM/DD/YYYY')
 GROUP BY UNIT_ID,
          PROD_LINE_ID,
          CURR_DATE,
          INTERIM_AUTO_ID,
          UNIT_ID,
          PROD_LINE_ID,
          V_SOURCE_CODE,
          CURR_ID,
          CREATED_BY,
          UPDATED_DATETIME;

或删除不需要的列:

   SELECT UNIT_ID,
          PROD_LINE_ID,
          CURR_DATE,
          SUM(BAL_AMOUNT) as SUM_BAL_AMOUNT
     FROM INTERIM_AUTO
    WHERE CURR_DATE = to_date(V_DATE, 'MM/DD/YYYY')
 GROUP BY UNIT_ID,
          PROD_LINE_ID,
          CURR_DATE;