Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6568075
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:24:44+00:00 2026-05-25T14:24:44+00:00

Possible Duplicate: Running total by grouped records in table I am trying to put

  • 0

Possible Duplicate:
Running total by grouped records in table

I am trying to put together an SQL statement that returns the SUM of a value by month, but on a year to date basis. In other words, for the month of March, I am looking to get the sum of a value for the months of January, February, and March.

I can easily do a group by to get a total for each month by itself, and potentially calculate the year to date value I need in my application from this data by looping through the results set. However, I was hoping to have some of this work handled with my SQL statement.

Has anyone ever tackled this type of problem with an SQL statement, and if so, what is the trick that I am missing?

My current sql statement for monthly data is similar to the following:

Select month, year, sum(value) from mytable group by month, year

If I include a where clause on the month, and only group by the year, I can get the result for a single month that I am looking for:

select year, sum(value) from mytable where month <= selectedMonth group by year

However, this requires me to have a particular month pre-selected or to utilize 12 different SQL statements to generate one clean result set.

Any guidance that can be provided would be greatly appreciated!

Update: The data is stored on an IBM iSeries.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T14:24:45+00:00Added an answer on May 25, 2026 at 2:24 pm
    declare @Q as table 
    (
    mmonth INT,
    value int 
    )
    
    insert into @Q
    values
    (1,10),
    (1,12),
    (2,45),
    (3,23)
    
    select sum(January) as UpToJanuary, 
    sum(February)as UpToFebruary,
    sum(March) as UpToMarch from (
    select 
    case when mmonth<=1 then sum(value) end as [January] ,
    case when mmonth<=2 then sum(value) end as [February],
    case when mmonth<=3 then sum(value) end as [March]
    from @Q
    group by mmonth
    ) t
    

    Produces:

    UpToJanuary UpToFebruary    UpToMarch
    22          67              90
    

    You get the idea, right?

    NOTE: This could be done easier with PIVOT tables but I don’t know if you are using SQL Server or not.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Tools for Generating Mock Data? I am running SQL Server 2005 and
Possible Duplicate: How do I detect that an SDK app is running on a
Possible Duplicate: Running Internet Explorer 6, Internet Explorer 7, and Internet Explorer 8 on
Possible Duplicate: Running Internet Explorer 6, Internet Explorer 7, and Internet Explorer 8 on
Possible Duplicate: Running Internet Explorer 6, Internet Explorer 7, and Internet Explorer 8 on
Possible Duplicate: Running Internet Explorer 6, Internet Explorer 7, and Internet Explorer 8 on
Possible Duplicate: How can I hide the console window in a PyQt app running
Possible Duplicate: Why does C# execute Math.Sqrt() more slowly than VB.NET? I'm running into
Possible Duplicate: Is JavaScript’s math broken? This seems really stupid, but when running this,
Possible Duplicate: Determine if running on a rooted device On Launch of the application,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.