将选择查询中的小数位添加到总和中

问题描述 投票:0回答:1

我已经构建了一个使用 SUM 函数的选择查询,我试图使输出显示结算金额的小数点后两位。在这个例子中,实际的 STTL_AMOUNT 是 29999,但我希望它输出 299.99,所以我需要在最后 2 个数字之前添加一个小数位。 STTL_AMOUNT 的格式为数字(22,4)

SELECT COUNT (STTL_AMOUNT) as "COUNT",
   SUM(STTL_AMOUNT) - SUM(CASE WHEN IS_REVERSAL =1 THEN STTL_AMOUNT ELSE 0 End ) as "Settlement Amount",
   HOST_DATE as "Settlement Date"
   FROM  OPR_OPERATION
   WHERE    MCC = '6011'
   AND      MSG_TYPE ='MSGTPRES'
   AND      HOST_DATE >= TRUNC( SYSDATE )
   GROUP BY HOST_DATE;

我得到以下输出:

"COUNT" "Settlement Amount" "Settlement Date"
1   29999   24-MAY-24

但我希望它输出如下:

"COUNT" "Settlement Amount" "Settlement Date"
1   299.99  24-MAY-24

我想知道是否应该使用 to_char 但如果我尝试这样做,似乎根本无法让查询工作。任何帮助将不胜感激。

oracle-sqldeveloper
1个回答
0
投票

只需除以100并四舍五入以删除不必要的0?

SELECT COUNT (STTL_AMOUNT) as "COUNT",
       ROUND(SUM(STTL_AMOUNT) - SUM(CASE WHEN IS_REVERSAL =1 THEN STTL_AMOUNT ELSE 0 End ) / 100,2) as "Settlement Amount",
       HOST_DATE as "Settlement Date"
       FROM  OPR_OPERATION
       WHERE    MCC = '6011'
       AND      MSG_TYPE ='MSGTPRES'
       AND      HOST_DATE >= TRUNC( SYSDATE )
       GROUP BY HOST_DATE;
© www.soinside.com 2019 - 2024. All rights reserved.