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

  • Home
  • SEARCH
  • 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 419617
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T18:47:43+00:00 2026-05-12T18:47:43+00:00

I have a database for which I need to aggregate records into another smaller

  • 0

I have a database for which I need to aggregate records into another smaller set. This result set should contain the difference between maximum and minumum of specific columns of the original records where they add up to certain SUM, a closed interval constant C.

The constant C determines how the original records are aggregated and no entry in the resulting set ever exceeds it. Naturally I am supposed to run this in natural primary key order..

To illustrate: table has:

  • [key]
  • [a]
  • [b]
  • [minColumn]
  • [maxColumn]
  • [N]

…all are int datatype.

I am after a result set that has entries where the MAX(maxColumn) – MIN(minColumn) for that group such that when their difference is summed up it is less or equal to constant C.

Apart from the MAX(maxColumn) and MIN(minColumn) value I also need the FIRST record column [a] and LAST record column [b] values before creating a new entry in this result set. Finally, the N column should be SUMmed for all original records in a group.

Is there an efficient way to do this without cursors?

—–[Trivial Sample]————————————————————

I am attempting to group-by a slightly complicated form of a running sum, constant C.

There is only one table, columns are all of int type and sample data

declare @t table (
  PK int primary key
    , int a, int b, int minColumn, int maxColumn, int N 
)

insert @t values (1,5,6,100,200,1000)
insert @t values (2,7,8,210,300,2000)
insert @t values (3,9,10,420,600,3000)
insert @t values (4,11,12,640,800,4000)

Thus for:

key, a,   b, minColumn, maxColumn,    N
---------------------------------------
1,   5,   6,       100,       200, 1000 
2,   7,   8,       210,       300, 2000 
3,   9,  10,       420,       600, 3000 
4,   11, 12,       640,       800, 4000 

I need the result set to look like, for a constant C of 210 :

firstA | lastB | MIN_minColumn | MAX_maxColumn | SUM_N
5       8                  100             300    3000 
9       10                 420             600    3000 
11      12                 640             800    4000 

[ Adding the bounty and sample as discussed below]

For C = 381, It should contain 2 rows:

firstA | lastB | MIN_minColumn | MAX_maxColumn | SUM_N
5            8             100             300    3000 
9           12             420             800    7000

Hope this demonstrates the problem better.. and for a constant C say 1000 you would get 1 record in the result:

firstA | lastB | MIN_minColumn | MAX_maxColumn | SUM_N
5           12             100             800   10000
  • 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-12T18:47:43+00:00Added an answer on May 12, 2026 at 6:47 pm
    DECLARE @c int
    SELECT @c = 210
    
    SELECT MIN(a) firstA,
           MAX(b) lastB, 
           MIN(minColumn) MIN_minColumn, 
           MAX(maxColumn) MAX_maxColumn, 
           SUM(N) SUM_N
    FROM @t t 
    JOIN (SELECT key, floor(sum/@c) as rank
            FROM (SELECT key, 
                         (SELECT SUM(t2.maxColumn - t2.minColumn) 
                            FROM @t t2 
                           WHERE t2.key <= t1.key 
                        GROUP BY t1.key) as sum
                   FROM @t t1) A
         ) B on B.key = t.key
    GROUP BY B.rank
    
    /*
    
    Table A: for each key, calculating SUM[maxColumn-minColumn] of all keys below it.
    Table B: for each key, using the sum in A, calculating a rank so that:
      sum = (rank + y)*@c where 0 <= y < 1. 
      ex: @c=210, rank(100) = 0, rank(200) = 0, rank(220) = 1, ...
    finally grouping by rank, you'll have what you want.
    
    */
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 230k
  • Answers 230k
  • 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 The trigger below should work - it is also possible… May 13, 2026 at 2:05 am
  • Editorial Team
    Editorial Team added an answer What you're asking is very close to saying you want… May 13, 2026 at 2:05 am
  • Editorial Team
    Editorial Team added an answer What command is failing with the Permission Denied? It may… May 13, 2026 at 2:05 am

Related Questions

I'm relatively new to NHibernate, but have been using it for the last few
So for some research work, I need to analyze a ton of raw movement
I'm writing a simple time tracking program to manage my own projects. I'm a
I have certain objects in my domain which are not aggregate roots/entities, yet I

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.