1

Here is my query:

SELECT (contract_value + amendment_value) AS total_value
FROM contracts
ORDER BY total_value DESC

When I run it, the result for each row is NULL. Why is that so?

Each row has either a contract_value or an amendment_value, but not both. Where there is no value, it is NULL. Could that be the problem? (I could not find anything in the documentation to suggest it). If so, how do I get around it?

1
  • 1
    COALESCE(contract_value, amendment_value) will return whichever is non-NULL Commented Oct 15, 2019 at 7:46

2 Answers 2

3

Use COALESCE:

SELECT
    COALESCE(contract_value, amendment_value) AS total_value
FROM contracts
ORDER BY total_value DESC;
Sign up to request clarification or add additional context in comments.

Comments

1

you can use coalesce() - all are showing null because any of your two value is null - with null value any calculation will return as null, so in this case, you've to replace null with 0

SELECT coalesce(contract_value,0) + coalesce(amendment_value,0) AS total_value
FROM contracts
ORDER BY total_value DESC

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.