Link to home
Start Free TrialLog in
Avatar of rwheeler23
rwheeler23Flag for United States of America

asked on

SQL Script with case and calculation

I need a SQL script that will look at two columns, determine which column is not zero and then multiply that column by a third column.
Example: Select A, B, C from mytable. On each row either B will have a non-zero value or  C will, but never both. The end result is that I need to get the one non-zero value and mulitply it by column A. I currently have what is below but I cannot get the syntax right for it to allow the mulitplication. If there is a better way than the Case please let me know.


SELECT Itemnmbr,'PostingCost' =
     CASE
          WHEN ext_assemble_qty_posting_cost = 0.00 THEN ext_stock_qty_posting_cost
                  ELSE ext_assemble_qty_posting_cost
     END
FROM bm30300
Avatar of chapmandew
chapmandew
Flag of United States of America image


select *, valfield = (case when afield > 0 then afield else bfield end) * cfield
from tablename

try a script like this

SELECT result =
      CASE WHEN column1 <> 0 and column2 = 0 THEN column1
             WHEN column2 <> 0 and column1 = 0 THEN column2
     END * column3
FROM TableName

SELECT Itemnmbr,'PostingCost' =
     CASE
          WHEN b = 0 THEN a*c
          WHEN c= 0 then a*b  
     END
FROM bm30300
ASKER CERTIFIED SOLUTION
Avatar of Ernariash
Ernariash
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial