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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:48:19+00:00 2026-06-15T20:48:19+00:00

I could need some help with a SQL statement. So I have the table

  • 0

I could need some help with a SQL statement. So I have the table “cont” which looks like that:

cont_id     name       weight
----------- ---------- -----------
1           1          10
2           1          20
3           2          40
4           2          15
5           2          20
6           3          15
7           3          40
8           4          60
9           5          10
10          6          5

I then summed up the weight column and grouped it by the name:

name       wsum
---------- -----------
2          75
4          60
3          55
1          30
5          10
6          5

And the result should have a accumulated column and should look like that:

name       wsum        acc_wsum
---------- ----------- ------------
2          75          75
4          60          135
3          55          190
1          30          220
5          10          230
6          5           235

But I didn’t manage to get the last statement working..

edit: this Statement did it (thanks Gordon)

select t.*,
    (select sum(wsum) from (select name, SUM(weight) wsum
    from cont
    group by name)
    t2 where t2.wsum > t.wsum or (t2.wsum = t.wsum and t2.name <= t.name)) as acc_wsum
from (select name, SUM(weight) wsum
from cont
group by name) t
order by wsum desc
  • 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-15T20:48:20+00:00Added an answer on June 15, 2026 at 8:48 pm

    So, the best way to do this is using cumulative sum:

    select t.*,
           sum(wsum) over (order by wsum desc) as acc_wsum
    from (<your summarized query>) t
    

    The order by clause makes this cumulative.

    If you don’t have that capability (in SQL Server 2012 and Oracle), a correlated subquery is an easy way to do it, assuming the summed weights are distinct values:

    select t.*,
           (select sum(wsum) from (<your summarized query>) t2 where t2.wsum >= t.wsum) as acc_wsum
    from (<your summarized query>) t
    

    This should work in all dialects of SQL. To work with situations where the accumulated weights might have duplicates:

    select t.*,
           (select sum(wsum) from (<your summarized query>) t2 where t2.wsum > t.wsum or (t2.wsum = t.wsum and t2.name <= t.name) as acc_wsum
    from (<your summarized query>) t
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table of data that looks a bit like this: Name StartTime
I have a SQL question that I could use some help with. Still somewhat
I need some editor tools for any markup language that could provide simple table
All - I need some help designing a table for a Postgre SQL database.
I have a table in SQL Server of strings that i need to inner
I could really use some help with the following SQL Select statement scenario: I
I need some help sorting my score value's of my sql database <table border
I need your help with some sample code for a situation I could not
I need a simple web based tool/CMS/app which could be linked to some repository
I need to implement something. Something that could do some certain task in my

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.