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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:44:48+00:00 2026-05-14T03:44:48+00:00

MSSQL 2008. I am trying to construct a SQL statement which returns the total

  • 0

MSSQL 2008. I am trying to construct a SQL statement which returns the total of column B for all rows where column A is between 2 known ranges. The range is a sliding window, and should be recomputed as it might be using a loop.

Here is an example of what I’m trying to do, much simplified from my actual problem. Suppose I have this data:

table: Test

Year        Sales
----------- -----------
2000        200
2001        200
2002        200
2003        200
2004        200
2005        200
2006        200
2007        200
2008        200
2009        200
2010        200
2011        200
2012        200
2013        200
2014        200
2015        200
2016        200
2017        200
2018        200
2019        200

I want to construct a query which returns 1 row for every decade in the above table, like this:

Desired Results:

DecadeEnd  TotalSales 
---------  ----------
2009        2000
2010        2000    

Where the first row is all the sales for the years 2000-2009, the second for years 2010-2019. The DecadeEnd is a sliding window that moves forward by a set ammount for each row in the result set. To illustrate, here is one way I can accomplish this using a loop:

declare @startYear int
set @startYear = (select top(1) [Year] from Test order by [Year] asc)
declare @endYear int
set @endYear = (select top(1) [Year] from Test order by [Year] desc)

select @startYear, @endYear

create table DecadeSummary (DecadeEnd int, TtlSales int)
declare @i int
-- first decade ends 9 years after the first data point
set @i = (@startYear + 9)   
while @i <= @endYear
begin
    declare @ttlSalesThisDecade int
    set @ttlSalesThisDecade = (select SUM(Sales) from Test where(Year <= @i and Year >= (@i-9)))
    insert into DecadeSummary values(@i, @ttlSalesThisDecade)

    set @i = (@i + 9)
end

select * from DecadeSummary

This returns the data I want:

DecadeEnd   TtlSales
----------- -----------
2009        2000
2018        2000

But it is very inefficient. How can I construct such a query?

  • 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-14T03:44:49+00:00Added an answer on May 14, 2026 at 3:44 am

    How about something like

    SELECT  (Year / 10) * 10,
            SUM(Sales)
    FROM    @Table
    GROUP BY (Year / 10) * 10
    

    Have a look at the example here

    DECLARE @Table TABLE(
        Year INT,
        Sales FLOAT
    )
    
    INSERT INTO @Table SELECT 2000,200 
    INSERT INTO @Table SELECT 2001,200 
    INSERT INTO @Table SELECT 2002,200 
    INSERT INTO @Table SELECT 2003,200 
    INSERT INTO @Table SELECT 2004,200 
    INSERT INTO @Table SELECT 2005,200 
    INSERT INTO @Table SELECT 2006,200 
    INSERT INTO @Table SELECT 2007,200 
    INSERT INTO @Table SELECT 2008,200 
    INSERT INTO @Table SELECT 2009,200 
    INSERT INTO @Table SELECT 2010,200 
    INSERT INTO @Table SELECT 2011,200 
    INSERT INTO @Table SELECT 2012,200 
    INSERT INTO @Table SELECT 2013,200 
    INSERT INTO @Table SELECT 2014,200 
    INSERT INTO @Table SELECT 2015,200 
    INSERT INTO @Table SELECT 2016,200 
    INSERT INTO @Table SELECT 2017,200 
    INSERT INTO @Table SELECT 2018,200 
    INSERT INTO @Table SELECT 2019,200 
    
    SELECT  (Year / 10) * 10,
            SUM(Sales)
    FROM    @Table
    GROUP BY (Year / 10) * 10
    

    OUTPUT

    Decade      SumOfSales
    ----------- ----------------------
    2000        2000
    2010        2000
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 427k
  • Answers 427k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This works if your structures only contain pointers to the… May 15, 2026 at 12:51 pm
  • Editorial Team
    Editorial Team added an answer If they want you to use log4net, they'd be happy… May 15, 2026 at 12:51 pm
  • Editorial Team
    Editorial Team added an answer I would say that combining the logging and web application… May 15, 2026 at 12:51 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.