我写了这个SELECT:
SELECT
nvl(SUM(vl_item),0) AS SUM_VALOR_ITENS
,nvl(SUM(vl_discount),0) AS SUM_VALOR_DESCONTO
,nvl(SUM(qty_item) is null ,0,0) AS SUM_QTD_ITENS
,COUNT(DISTINCT cod_product_rms) AS QTD_ITENS_UNICOS
FROM db.my_table
where SAFRA = 202006
当我在DBeaver上运行时,结果是0(零),就像我预想的那样。 但是,当我使用Java在HQL文件中运行这个SQL时,结果分别是“0e-10”,“0e-18”和“0e-18”。 我不明白为什么会收到这个结果。 有人能帮我吗?
我建议使用coalesce,nvl不能用于第3列,因为nvl只接受2个参数(这一列接受3个),而coalesce可以接受任意多个参数。
SELECT
coalesce(SUM(vl_item),0) AS SUM_VALOR_ITENS
,coalesce(SUM(vl_discount),0) AS SUM_VALOR_DESCONTO
,coalesce(SUM(qty_item) is null ,0,0) AS SUM_QTD_ITENS
,COUNT(DISTINCT cod_product_rms) AS QTD_ITENS_UNICOS
FROM db.my_table
where SAFRA = 202006