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 6212511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:31:43+00:00 2026-05-24T06:31:43+00:00

I want to SUM up the last 12 months values from a sql query.

  • 0

I want to SUM up the last 12 months values from a sql query.

The problem is to sum up the last 12 months, instead of taking just the whole year.

So when I SELECT March 2000 as default in my parameter,

I want the query to SUM up: from March 1999-March 2000.

This is what I got so far:

SELECT Name, SUM(sales) as totalsales, year_month
FROM Total_Sales
WHERE 
(year_month = @year_month)
GROUP BY Name, year_month

Thanks for any help!

–EDIT–

SELECT Name, SUM(sales) as totalsales, year_month
FROM Total_Sales
WHERE 
(year_month >= @From) AND 
(year_month <= @To)
GROUP BY Name, year_month

Added this after suggestions from “Steve Morgan”


Can we add a average for the last 12 months somehow without selecting 12 months in the parameters.
Letting the “@From” parameter decide on start value. Something like: AVG(@From -12 months)??

Thanks again for the help!

–EDIT 2–

The ROW_NUMBER() function is very handy to solve this problem.
My query looks like this now:

SELECT  Name, SUM(sales)
FROM    
(               
SELECT  rn = ROW_NUMBER() OVER (PARTITION BY Name ORDER BY year, year_month)
, Name
, sales = SUM(sales)
FROM     Total_Sales ts
WHERE
(year_month>= @From) AND 
(year_month<= @To)
GROUP BY
Name
, year
, year_month
    ) ts 
WHERE
rn <= 12    

GROUP BY
Name  
  • 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-24T06:31:44+00:00Added an answer on May 24, 2026 at 6:31 am

    This code is longer than Steve Morgans, but is designed to allow the optimiser the ability to use any existing INDEX on the year and year_month fields. This is only relevant if you have a relatively large table as it is designed to efficiently know which records can be skipped.

    (It also assumes MS SQL Server, but the logic is applicable to other RDBMSes.)

    DECLARE
      @DateParam AS DATETIME
    SELECT
      @DateParam  = '2011 June 01'
    
    ;WITH MyTable (Name, Year, Year_Month, Sales) AS
    (
                 SELECT 'Rod', 2010, 1, 10
       UNION ALL SELECT 'Rod', 2010, 2, 10
       UNION ALL SELECT 'Rod', 2010, 3, 10
       UNION ALL SELECT 'Rod', 2010, 4, 10 UNION ALL SELECT 'Jane', 2010, 4, 10
       UNION ALL SELECT 'Rod', 2010, 5, 10 UNION ALL SELECT 'Jane', 2010, 5, 10
       UNION ALL SELECT 'Rod', 2010, 6, 10 UNION ALL SELECT 'Jane', 2010, 6, 10
    ------------------------------------------------------------------------------
       UNION ALL SELECT 'Rod', 2010, 7, 10 UNION ALL SELECT 'Jane', 2010, 7, 10
       UNION ALL SELECT 'Rod', 2010, 8, 10 UNION ALL SELECT 'Jane', 2010, 8, 10
       UNION ALL SELECT 'Rod', 2010, 9, 10 UNION ALL SELECT 'Jane', 2010, 9, 10
       UNION ALL SELECT 'Rod', 2010,10, 10 UNION ALL SELECT 'Jane', 2010,10, 10
                                           UNION ALL SELECT 'Jane', 2010,11, 10
                                           UNION ALL SELECT 'Jane', 2010,12, 10
                                           UNION ALL SELECT 'Jane', 2011, 1, 10
                                           UNION ALL SELECT 'Jane', 2011, 2, 10
       UNION ALL SELECT 'Rod', 2011, 3, 10 UNION ALL SELECT 'Jane', 2011, 3, 10
       UNION ALL SELECT 'Rod', 2011, 4, 10 UNION ALL SELECT 'Jane', 2011, 4, 10
       UNION ALL SELECT 'Rod', 2011, 5, 10
       UNION ALL SELECT 'Rod', 2011, 6, 10
    ------------------------------------------------------------------------------
       UNION ALL SELECT 'Rod', 2011, 7, 10
    )
    
    SELECT
      Name,
      Year,
      Year_Month,
      SUM(sales) AS Total_Sales
    FROM
      MyTable
    WHERE
       (Year = DATEPART(YEAR, @DateParam)     AND Year_Month <= DATEPART(MONTH, @DateParam))
    OR (Year = DATEPART(YEAR, @DateParam) - 1 AND Year_Month >  DATEPART(MONTH, @DateParam))
    GROUP BY
      Name,
      Year,
      Year_Month
    

    Notes:
    1. This will give the results for the 12 months up to and including June 2011
    2. No data prior to July 2010 will be included
    3. The gap for Rod’s data won’t cause Note 3 to be breached
    4. Jane’s missing data in May 2011 and June 2011 won’t cause Note 3 to be breached
    5. The formulation of the WHERE clause will allow INDEXes to be used

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

Sidebar

Related Questions

I want to create the following T-SQL statement: SELECT SUM (sa.Amount) as 'SumAmount', SUM(sa.Cost)
I want to pick and SUM values since last wednesday until NOW() in mysql.
When I run this query: select First_Name, sum(a) a, sum(b) b, sum(c) c from
I have a problem in subtracting a two values. i just want to be
SELECT YEAR(aum.AUM_Timeperiod) as Year, DATEPART(q, aum.AUM_TimePeriod) AS Quarter, SUM(cast(aum.AUM_AssetValue AS money)) as total_AssetValue FROM
I want a spreadsheet function that will produce a sum of all values in
Hi I have written this query SELECT cl_brands.name AS Brand,DATE_FORMAT(cl_doctor_call.date_entered,'%b%y')AS MonthYear, SUM(FIND_IN_SET(CONCAT('^',cl_brands.id,'^'),cl_doctor_call_cstm.brand_discussed_c))AS No_Times_Brand_Discussed FROM
I am using SQL Server 2008. I want to write a query that gives
I have a query that find the last name of a person, sum of
Crazy question...however, I want the sum of all the rows in a table for

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.