I have a select statement but want to evaluate one column, if it contains value of 584. I am trying to create a new calculation column called total by multiplying 3 other columns, if not 584 then calculate based on different columns
select CASE `d2c_3_csg_batch_in`.`transactioncode` AS `transactioncode`,
`d2c_3_csg_batch_in`.`site` AS `site`,
`d2c_3_csg_employees`.`lastname` AS `lastname`,
`d2c_3_csg_employees`.`firstname` AS `firstname`,
`d2c_3_csg_employees`.`payrate` AS `payrate`,
`d2c_3_csg_batch_in`.`employeecode` AS `employeecode`,
`d2c_3_csg_batch_in`.`jobcode` AS `jobcode`,
`d2c_3_csg_employees`.`CompanyFrequency` AS `CompanyFrequency`,
`d2c_3_csg_batch_in`.`inputunits` AS `inputunits`,
`d2c_3_csg_transactioncodes`.`multiplier` AS `multiplier`,
`d2c_3_csg_transactioncodes`.`payspacewording` AS `payspacewording`,
WHEN `d2c_3_csg_batch_in`.`transactioncode` = '584' THEN `d2c_3_csg_batch_in`.`inputunits` AS `total`
WHEN `d2c_3_csg_batch_in`.`transactioncode` <> '584' THEN `d2c_3_csg_batch_in`.`inputunits` * `d2c_3_csg_employees`.`payrate` * `d2c_3_csg_transactioncodes`.`multiplier` AS `total`
END
from ((`d2c_3_csg_transactioncodes` join `d2c_3_csg_batch_in` on(`d2c_3_csg_transactioncodes`.`code` = `d2c_3_csg_batch_in`.`transactioncode`))
left join `d2c_3_csg_employees` on(`d2c_3_csg_batch_in`.`employeecode` = `d2c_3_csg_employees`.`employeenumber`))
where `d2c_3_csg_batch_in`.`flag` = 'ADD' and `d2c_3_csg_batch_in`.`prp` = 'Y'
group by `d2c_3_csg_batch_in`.`employeecode`,`d2c_3_csg_batch_in`.`transactioncode`
order by `d2c_3_csg_batch_in`.`employeecode` desc,`d2c_3_csg_batch_in`.`transactioncode`
Try using Sum if e.g
SUM(if (q1.bill = 584,q1.bill - q1.payment,0))AS bal,
SUM(if (q1.bill != 584,q1.payment - q1.bill ,0))AS advpay,
q1 is table alias. other fields are imaginary
Point of correction: When using case, you do it this way;
CASE WHEN `d2c_3_csg_batch_in`.`transactioncode` = '584'
THEN `d2c_3_csg_batch_in`.`inputunits`
WHEN `d2c_3_csg_batch_in`.`transactioncode` <> '584'
THEN `d2c_3_csg_batch_in`.`inputunits` * `d2c_3_csg_employees`.`payrate` * `d2c_3_csg_transactioncodes`.`multiplier`
END AS `total` -- an alias must be there!