SQL案例陈述

时间:2016-11-20 16:14:01

标签: mysql sql

我有一个名为“allocation”的表,其中的列名为“all_code”,“grs_amt”和“cut_amt”。我想创建一个SQL语句来获得Gross_Value,Cut_Value和Net_Value的结果。如果“cut_amt”IsNull则为Cut_Value指定0,然后使用“cut_amt”中指定的0值或可用值计算Net_Value。我使用了以下查询。

SELECT allocation.grs_amt AS Gross_Value,
  IfNull(allocation.cut_amt, 0) AS Cut_Value,
  allocation.grs_amt - allocation.cut_amt AS Net_Value FROM
  allocation 

02)但无法获得所需的输出。无法理解我的错误。任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

你必须重复一下这个词:

SELECT a.grs_amt AS Gross_Value,
       coalesce(a.cut_amt, 0) AS Cut_Value,
       (a.grs_amt - coalesce(a.cut_amt, 0)) AS Net_Value
FROM allocation a;

在大多数情况下,我更喜欢coalesce()因为它是ANSI标准。