SELECT sum((DECODE(transaction_code,'INV',quantity,( (-1)* quantity)))) total
FROM INVENTORY_TRANSACTION_HIST_TAB
WHERE order_no = 9999;
返回:1160721
declare
cursor c is
SELECT sum((DECODE(transaction_code,'INV',quantity,( (-1)* quantity)))) total
FROM INVENTORY_TRANSACTION_HIST_TAB
WHERE order_no = 9999
begin
for rec_ in c loop
dbms_output.put_line(rec_.total_consumed);
end loop;
end;
返回:1160720,999999999999995
有什么想法吗?如何解决这个问题?
答案 0 :(得分:2)
在SQLplus中执行此操作时,您将获得相同的结果:
SET NUMWIDTH 50;
SELECT sum((DECODE(transaction_code,'INV',quantity,( (-1)* quantity)))) total
FROM INVENTORY_TRANSACTION_HIST_TAB
WHERE order_no = 9999;
原因是在SQLplus上默认 nubmer格式为SET NUMWIDTH 10
,因此默认情况下结果舍入为10位。
在PL / SQL块中,您不会对任何内容进行舍入。前段时间我遇到了同样的问题,我从Tom Kyte得到答案。
答案 1 :(得分:0)
据推测,您希望将其四舍五入为适当的值。
也许最简单的方法是将其转换为小数:
SELECT cast(sum((DECODE(transaction_code,'INV',quantity,( (-1)* quantity)))) as decimal(15, 2) as total
FROM INVENTORY_TRANSACTION_HIST_TAB
WHERE order_no = 9999;