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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T13:45:18+00:00 2026-06-05T13:45:18+00:00

I have a table that lists the duration of different activities. It looks like

  • 0

I have a table that lists the duration of different activities. It looks like

id duration  
1  15  
2  30    
3  30  
4  45  
5  30  

…etc

I want to sum these activities like

for (lastActivity=1 to 5)
  SELECT id, SUM(duration) FROM durations
  WHERE id<=lastActivity

to produce an output like

id endtime  
1  15
2  45
3  75  
4  120  
5  150  

where each row sums the duration of the activities up to its position in the list.

It seems an easy task (and possibly is), but I can’t figure out how the sql should look like to produce such an output. I have tried using GROUP BY together with the OVER clause but perhaps there’s a simpler way of doing this.

  • 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-06-05T13:45:19+00:00Added an answer on June 5, 2026 at 1:45 pm
    SELECT t.id, 
           t.duration, 
           rt.runningTotal 
    FROM   mytable t 
           CROSS apply (SELECT Sum(duration) AS runningTotal 
                        FROM   emp 
                        WHERE  id <= t.id) AS rt 
    ORDER  BY t.id 
    

    The APPLY operator allows you to invoke a table-valued function for each row returned by an outer table expression of a query. The table-valued function acts as the right input and the outer table expression acts as the left input. The right input is evaluated for each row from the left input and the rows produced are combined for the final output. The list of columns produced by the APPLY operator is the set of columns in the left input followed by the list of columns returned by the right input.

    Note : To use APPLY, the database compatibility level must be at least 90. This was introduced in sql server 2005.

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

Sidebar

Related Questions

I have a table that lists number of comments from a particular site like
I have a table that lists people and all their contact info. I want
All, I have a table that looks like this: Date Pitcher WHIP -------- --------------
I have the following table that lists all awarded cups: Name: Cups Columns: Month,
So I have a list box that displays averages in a table like format
I have a table that lists users that are registered at my website. The
I have a table that lists a bunch of customers. The last cell is
I have a table that lists service calls for employees. i have a view
I have a table that lists products as well as displays a quantity text
I have a table that lists items and has a quantity textbox column that

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.