User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the MS SQL section within the Web Development category of DaniWeb, a massive community of 402,662 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,271 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our MS SQL advertiser: Programming Forums

Query Total Asset

Join Date: Mar 2007
Posts: 28
Reputation: cfAllie is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
cfAllie cfAllie is offline Offline
Light Poster

Re: Query Total Asset

  #3  
May 12th, 2007
Originally Posted by MidiMagic View Post
You are not summing in the previous month.


As Midimagic said, you need to sum the previous month(s) too. One way to do this is using a join

SELECT    curr.AssetYear, curr.AssetMonth,
    SUM(curr.Total + ISNULL(prev.Total, 0)) AS Asset_Total
FROM    (
       SELECT  Year(Date) AS AssetYear, 
           Month(Date) AS AssetMonth, 
           Sum(Value) AS Total
          FROM    Asset
       WHERE   date between @beginDate And @endDate
       GROUP BY Year(Date), Month(Date)
    ) 
    AS curr LEFT JOIN
    (
       SELECT  Year(Date) AS AssetYear, 
           Month(Date) AS AssetMonth, 
           Sum(Value) AS Total
          FROM    Asset
       WHERE   date between @beginDate And @endDate
       GROUP BY Year(Date), Month(Date)
    ) 
    AS prev ON curr.AssetYear = prev.AssetYear 
        AND curr.AssetMonth > prev.AssetMonth
GROUP BY curr.AssetYear, curr.AssetMonth
Reply With Quote  
All times are GMT -4. The time now is 3:52 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC