0

I have this MySQL statement which works fine:

SELECT claim_items.item_id, claim_items.quantity*items_rate.rate as per_item
FROM claim_items, items_rate 
WHERE claim_items.claim_id = 1 AND claim_items.item_id = items_rate.items_id

However what I want to do is to get the sum of the per_item field generated in the MySQL statement. Is it possible to do a nested statement to do that? I know I could create a view and then do it, but would prefer to do it in one statement.

Thanks for your help, much appreciated!

2 Answers 2

2
select t.id, SUM(t.per_item)
from 
(
SELECT claim_items.item_id as id, claim_items.quantity*items_rate.rate as per_item
FROM claim_items, items_rate 
WHERE claim_items.claim_id = 1 AND claim_items.item_id = items_rate.items_id
) t
group by t.id

Hope this should help.

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

1 Comment

Thanks that helped, I removed the ID though because otherwise it wont calculate the sum :)
1

If you just want the sum over all rows you can do it like that (using explicit joins for better readability):

SELECT SUM( claim_items.quantity * items_rate.rate )
FROM claim_items
JOIN items_rate ON ( items_rate.items_id = claim_items.item_id )
WHERE claim_items.claim_id = 1

1 Comment

Thanks that does indeed looks nicer :)

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.