Summing an amount, across two tables.

I have two tables, the first has original amount of a fund, the second is related and has increments to that fund amount.  I want to sum the original amount and all the increments.  How can I do this?

These two queries return the original amount of a fund, and the sum of the increments.  

select amount from tds_fund where id = 4
select sum(amount) from tds_fund_amount_added where fk_fund = 4
LVL 1
HLRosenbergerAsked:
Who is Participating?
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.

Vitor MontalvãoMSSQL Senior EngineerCommented:
WITH CTE_SUM
AS (
	SELECT fk_fund, sum(amount) Total
	FROM tds_fund_amount_added 
	GROUP BY fk_fund 
	)
SELECT f.id, f.amount+s.Total SumInc
FROM tds_fund f
	INNER JOIN CTE_SUM s ON f.id = s.fk_fund

Open in new window

0
Vitor MontalvãoMSSQL Senior EngineerCommented:
If you need you can add the filter in the main SELECT:
WITH CTE_SUM
AS (
	SELECT fk_fund, sum(amount) Total
	FROM tds_fund_amount_added 
	GROUP BY fk_fund 
	)
SELECT f.id, f.amount+s.Total SumInc
FROM tds_fund f
	INNER JOIN CTE_SUM s ON f.id = s.fk_fund
WHERE f.id = 4

Open in new window

0
Brian CroweDatabase AdministratorCommented:
SELECT F.id, F.amount AS OriginalAmount, SUM(amount) AS AmountAdded
FROM tds_fund AS F
LEFT OUTER JOIN tds_fund_amount_added AS FAA
      ON F.id = FAA.fk_fund
GROUP BY F.id, F.amount
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

HLRosenbergerAuthor Commented:
if I add the where clause, I get a syntax error.
0
Vitor MontalvãoMSSQL Senior EngineerCommented:
if I add the where clause, I get a syntax error.
Please share how are you doing it.
0
HLRosenbergerAuthor Commented:
in a studio manager window;  I get this error:

Msg 319, Level 15, State 1, Line 4
Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon.


use tdsdev

WITH CTE_SUM
AS (
      SELECT fk_fund, sum(amount) Total
      FROM tds_fund_amount_added
      GROUP BY fk_fund
      )
SELECT f.id, f.amount+s.Total SumInc
FROM tds_fund f
      INNER JOIN CTE_SUM s ON f.id = s.fk_fund
      
WHERE f.id = 4
0
Vitor MontalvãoMSSQL Senior EngineerCommented:
Ok, you'll need to add a semi-colon before WITH statement since isn't the first command (you have an USE):
;WITH CTE_SUM
AS (
	SELECT fk_fund, sum(amount) Total
	FROM tds_fund_amount_added 
	GROUP BY fk_fund 
	)
SELECT f.id, f.amount+s.Total SumInc
FROM tds_fund f
	INNER JOIN CTE_SUM s ON f.id = s.fk_fund
WHERE f.id = 4

Open in new window

0

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
HLRosenbergerAuthor Commented:
Ah, got it!.   Works great.  thanks.
0
HLRosenbergerAuthor Commented:
I need one more thing - it has to work when there are no records in the related table for a Fund.  In this case, I need just the amount in the Fund table.
0
Brian CroweDatabase AdministratorCommented:
Use a LEFT OUTER JOIN instead of an INNER JOIN
0
Vitor MontalvãoMSSQL Senior EngineerCommented:
Use a LEFT JOIN and ISNULL function to replace Null for Zero:
;WITH CTE_SUM
AS (
	SELECT fk_fund, sum(amount) Total
	FROM tds_fund_amount_added 
	GROUP BY fk_fund 
	)
SELECT f.id, f.amount+ISNULL(s.Total,0) SumInc
FROM tds_fund f
	LEFT JOIN CTE_SUM s ON f.id = s.fk_fund

Open in new window

0
HLRosenbergerAuthor Commented:
ah, yes.  DUH.  But I also need to do this:

CASE WHEN s.Total IS NULL THEN 0 ELSE s.Total END
0
HLRosenbergerAuthor Commented:
Or ISNULL(s.Total,0).

Thanks again!
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
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.