2016-11-20 77 views
0

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

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)但无法获得所需的输出。不明白我出错了。谁能帮我?

+0

我使用了MySQL – Xtern

回答

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标准。