Help with a query that fetches a SUM

I am trying to get a query that will output the invoice ID, invoice title, and sum of the invoice allocation amounts for each invoice where the allocation is paid

table: invoices
+-------+--------------+------+-----+---------+----------------+
| Field | Type         | Null | Key | Default | Extra          |
+-------+--------------+------+-----+---------+----------------+
| id    | int(11)      | NO   | PRI | NULL    | auto_increment |
| title | varchar(255) | NO   |     | NULL    |                |
+-------+--------------+------+-----+---------+----------------+

+----+------------+
| id | title      |
+----+------------+
|  1 | John Smith |
|  2 | Mary Jones |
+----+------------+

Open in new window


table: invoice_amounts
+------------+---------------+------+-----+---------+-------+
| Field      | Type          | Null | Key | Default | Extra |
+------------+---------------+------+-----+---------+-------+
| invoice_id | int(11)       | NO   |     | NULL    |       |
| amount     | decimal(10,0) | NO   |     | NULL    |       |
| paid       | varchar(255)  | YES  |     | NULL    |       |
+------------+---------------+------+-----+---------+-------+

+------------+--------+------+
| invoice_id | amount | paid |
+------------+--------+------+
|          1 | 26     | paid |
|          2 | 59     | NULL |
|          2 | 69     | paid |
|          2 | 30     | paid |
|          1 | 26     | paid |
+------------+--------+------+

Open in new window


Can anybody help me out? This is sort-of beyond my SQL query skills.

Thank you.
John S.Web DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Raja Jegan RSQL Server DBA & Architect, EE Solution GuideCommented:
This should do..
select i.id, i.title, sum(amount) sum_amount
from invoices i
join invoices_amount ia on i.id = ia.invoice_id
where ia.paid = 'paid'
group by i.id, i.title

Open in new window

1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MohitPanditCommented:
Here you go,
SELECT
	invoice_id = I.id,
	title = i.title,
	ia.invoice_sum_amount
FROM invoices i
INNER JOIN
(
	SELECT
		invoice_id,
		invoice_sum_amount = SUM(amount)
	FROM invoice_amounts
	WHERE
	(
		paid = 'paid'
	)
	GROUP BY invoice_id
) ia
ON i.id = ia.invoice_id

Open in new window

1
John S.Web DeveloperAuthor Commented:
Thank you. These answers helped me a lot.
0
Raja Jegan RSQL Server DBA & Architect, EE Solution GuideCommented:
You are welcome!!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Databases

From novice to tech pro — start learning today.