0

I am trying to calculate the difference between two columns, but sometimes one of the columns has NULL value and SQL is ignoring that.

Right now I am using this: if("source indicator" = 'Total OPEX', ("Eur Amount" -"EUR Target") * -1, "Eur Amount" -"EUR Target") as "Diff."

Is there any other way to calculate this (please note that OPEX value) and get the expected results?

Thanks

2
  • Which db engine are you using? Commented May 19, 2017 at 10:38
  • if is non standard... Commented May 19, 2017 at 10:39

1 Answer 1

4

You can use case and coalesce():

(case when "source indicator" = 'Total OPEX'
      then coalesce("Eur Amount", 0) - coalesce("EUR Target", 0) * -1
      else coalesce("Eur Amount", 0) - coalesce("EUR Target", 0)
 end)

The advantage of case is that it is ANSI-standard SQL, supported by pretty much all databases. if() is database-dependent.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.