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

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

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
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

HLRosenbergerAuthor Commented:
if I add the where clause, I get a syntax error.
Vitor MontalvãoMSSQL Senior EngineerCommented:
if I add the where clause, I get a syntax error.
Please share how are you doing it.
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
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

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.
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.
Brian CroweDatabase AdministratorCommented:
Use a LEFT OUTER JOIN instead of an INNER JOIN
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

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

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

Thanks again!
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.