简体   繁体   中英

SQL Case Statement

I have a table named as "allocation" with the columns named as "all_code", "grs_amt" and "cut_amt". I want to create a SQL statement to be got results as Gross_Value, Cut_Value and Net_Value. If the "cut_amt" IsNull then assign 0 for the Cut_Value and then calculate Net_Value using that assigned 0 value or available value in the "cut_amt". I used the following query.

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) But unable to get the desired output. Can not understand what I am going wrong . Can anyone help me?

You have to repeat the expression:

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;

I prefer coalesce() under most circumstances because it is ANSI standard.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM