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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:22:15+00:00 2026-05-26T14:22:15+00:00

I have a fairly complex query (it takes about 30 seconds to execute) that

  • 0

I have a fairly complex query (it takes about 30 seconds to execute) that returns me the following dataset:

 Month     Buy     Sell
2010/10     1        2
2010/11     1        3
2010/12     2        5

And here’s the query:

select month, avg(buy) [buy], avg(sell) [sell] from products group by month order by month

Now I want to add two Accumulated Columns and the expected resultset is the following:

 Month   Ac. Buy   Ac. Sell
2010/10     1          2
2010/11     2          5
2010/12     4          10

I’m trying to use this query

select 
distinct x.month
,(select SUM(buy) from products where month <= x.month) [Ac Buy]
,(select SUM(sell) from products where month <= x.month) [Ac Sell]
from products X
order by x.month

But that takes way too long!

Is there any way to do this faster?

I’m using a MS SQL 2008 Server, but my compability level is set to 80 (like MSSQL 2000, and I can’t change that). So I feel like I’m driving a Ferrari using only the 1st gear. );

  • 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-26T14:22:16+00:00Added an answer on May 26, 2026 at 2:22 pm

    For 13 rows I would just materialise the intermediate results into a table variable then do a triangular join on that.

    DECLARE @Results TABLE
    (
    Month char(7) PRIMARY KEY,
    Buy int,
    Sell int
    )
    
    INSERT INTO @Results /*Your select query goes here*/
    SELECT '2010/10',1,2 UNION ALL
    SELECT '2010/11',1,3 UNION ALL
    SELECT '2010/12',2,5
    
    SELECT R1.Month,
           R1.Buy,
           R1.Sell,
           SUM (R2.Sell)AS AcSell,
           SUM (R2.Buy) AS AcBuy 
    FROM @Results R1
    JOIN @Results R2 ON R2.Month <= R1.Month
    GROUP BY R1.Month,
           R1.Buy,
           R1.Sell
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a fairly complex procedure that currently takes about 1:30 (min:sec) to complete.
I have a fairly complex query (that includes a table valued function to allow
I have a fairly complex query that will be referencing a single date as
I have a fairly complex query that looks something like this: create table Items(SomeOtherTableID
I have a fairly complex query in SQL that performs a count across two
I have a fairly complex SQL query that pulls different types of products from
I have a fat GUI that it getting fairly complex, and I would like
I have a web page that I use to update a fairly complex data
I have a fairly complex LINQ to Entities query I'd like to try compiling
I have a fairly complex query where I am filtering results with a LIKE

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.