Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Getting incorrect Running Total

Author  Topic 

kellog1
Starting Member

35 Posts

Posted - 2010-08-16 : 16:44:36
Guys,
I am trying to calculate running totals and I am getting incorrect results. Below is my query...

select PublicationId,ProjectPublicationId, ControlFundingAmount,
(select sum(ControlFundingAmount)
from DimProjectPublication pp1
where pp1.ProjectPublicationId = pp.ProjectPublicationId
and pp1.PublicationId = pp.PublicationId)
from DimProjectPublication pp
where ProjectPublicationId IN (135750,135211)
order by ProjectPublicationId

Result:
PublicationId ProjectPublicationId ControlFundingAmount RT
43 135211 200000.00 200000.00
43 135750 150000.00 150000.00

Desired Result:
PublicationId ProjectPublicationId ControlFundingAmount RT
43 135211 200000.00 200000.00
43 135750 150000.00 350000.00

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2010-08-16 : 19:31:38
Without knowing what your tables actually look like, try this

where pp1.ProjectPublicationId = pp.ProjectPublicationId
and pp1.PublicationId >= pp.PublicationId)

Jim

Everyday I learn something that somebody else already knew
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-08-17 : 11:17:54
shouldnt it be?

where pp1.ProjectPublicationId = pp.ProjectPublicationId
and pp1.PublicationId <= pp.PublicationId)

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -