使用nvl编写的字母选择为什么?

slsn1g29  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(388)

我写了这篇精选:

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”。我不明白为什么我会收到这个结果。有人能帮我吗?

ivqmmu1c

ivqmmu1c1#

我建议使用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

相关问题