2

i need to write an SQL query which is similar to the following:

SELECT c.*, p.amount
FROM claims c, payments p
WHERE claims.debit < payments.amount AND c.customerId = p.customerId

Now the problem is, that this query only shows claims, which have a payment which belongs to this claims. But I want it to show every claim and if there's no belonging payment the amount should just be zero.

How can i achieve this?

Btw, I'm doing this in Firebird-SQL.

And sorry for the bad title, i have no idea how to describe it better.

Thank you for your help

2 Answers 2

4

You should do a LEFT JOIN, but also putting the "<" construction in the JOIN condition.

select c.*,
       coalesce(p.amount, 0) amount
  from claims c
  left outer join payments p
    on c.customerId = p.customerId and
       c.debit < p.amount 

If you do like @whirl-mind said, the WHERE will be evaluated against "NULL values" and it will filter out the records.

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

Comments

0

You should use a Left Outer Join.

SELECT c.*, p.amount
FROM claims c Left Outer Join  payments p On c.customerId = p.customerId
WHERE claims.debit < payments.amount 

Not sure about Firebird-SQL,you should be able to use an IsNull phrase or a Case Statement to fetch 0 if the payment is null.

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.