我有一个名为“allocation”的表,其中的列名为“all_code”、“grs_amt”和“cut_amt”。我想创建一个 SQL 语句来获取 Gross_Value、Cut_Value 和 Net_Value 等结果。如果“cut_amt”为空,则为 Cut_Value 分配 0,然后使用分配的 0 值或“cut_amt”中的可用值计算 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) 但无法获得所需的输出。无法理解我出了什么问题。谁能帮我吗?
最佳答案
你必须重复这个表达式:
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 标准。
关于mysql - SQL 案例语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40706293/