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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:49:23+00:00 2026-06-09T22:49:23+00:00

My question is somewhat similar to Group by every N records in T-SQL however

  • 0

My question is somewhat similar to

Group by every N records in T-SQL

however I want to group by a maximum dollar amount

So I have 20 records with a total amount of 50,000, I need to break them into groups so that each group has a maximum amount of 10,000.

I tried using a running value, giving the records a rank and them summing the values <= the current rank, then breaking them into SubGroups using Floor((RunningValue - amount) / 10000), but there are circumstances where it goes over the 10,000 maximum

Sample data, you can see here that SubGroup 1 is over 10,000

SubGroupNo  RowNo   amount  RunningValue
0           1       790.5   790.5
0           2       790.5   1581
0           3       790.5   2371.5
0           4       790.5   3162
0           5       744     3906
0           6       744     4650
0           7       1348.5  5998.5
0           8       1348.5  7347
0           9       1348.5  8695.5
1           10      1348.5  10044
1           11      1302    11346
1           12      1302    12648
1           13      1302    13950
1           14      1302    15252
1           15      1255.5  16507.5
1           16      1209    17716.5
1           17      1116    18832.5
1           18      1116    19948.5
2           19      1302    21250.5
2           20      1302    22552.5
2           21      1302    23854.5
2           22      1255.5  25110
2           23      1255.5  26365.5
2           24      976.5   27342
  • 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-09T22:49:24+00:00Added an answer on June 9, 2026 at 10:49 pm

    This is probably not the best approach, but it works. It assumes that your RowNo‘s are sequential. If not, I would add ROW_NUMBER() with this solution.

    Iteration-based approach

    SQL Fiddle

    DECLARE @runningTotal decimal(7,2), @sgn tinyint, @cursor int, @count int
    SET @runningTotal = '0.00'
    SET @sgn = 0
    SET @cursor = 1
    SELECT @count = COUNT(*) FROM tbl
    
    WHILE @cursor <= @count
    BEGIN
        SELECT @runningTotal = @runningTotal + amount FROM tbl WHERE RowNo = @cursor
        IF @runningTotal > 10000
            BEGIN
                SELECT @runningTotal = amount FROM dbo.tbl WHERE RowNo = @cursor
                SET @sgn = @sgn + 1
            END
        UPDATE tbl SET SubGroupNo = @sgn WHERE RowNo = @cursor
        SET @cursor = @cursor + 1
    END
    

    Edit 1: Cursor-based approach

    SET NOCOUNT ON;
    
    DECLARE @SubGroupNo tinyint, @RowNo smallint, 
        @amount decimal(8,2), @RunningTotal decimal(7,2)
    
    SET @SubGroupNo = 0
    SET @RunningTotal = '0.00'
    
    DECLARE row_cursor CURSOR FOR
        SELECT RowNo, amount FROM dbo.tbl
    
    OPEN row_cursor
    
    FETCH NEXT FROM row_cursor
    INTO @RowNo, @amount
    
    WHILE @@FETCH_STATUS = 0
    BEGIN
        SET @RunningTotal = @RunningTotal + @amount
    
        IF @RunningTotal > 10000
            BEGIN
                SET @RunningTotal = @amount
                SET @SubGroupNo = @SubGroupNo + 1
            END
        PRINT @RowNo 
        PRINT @RunningTotal
        PRINT @SubGroupNo
    
        UPDATE dbo.tbl SET SubGroupNo = @SubGroupNo WHERE RowNo = @RowNo
    
        FETCH NEXT FROM row_cursor
        INTO @RowNo, @amount
    END
    
    CLOSE row_cursor
    DEALLOCATE row_cursor
    

    Result

    | SUBGROUPNO | ROWNO | AMOUNT | RUNNINGVALUE |
    ----------------------------------------------
    |          0 |     1 |  790.5 |        790.5 |
    |          0 |     2 |  790.5 |         1581 |
    |          0 |     3 |  790.5 |       2371.5 |
    |          0 |     4 |  790.5 |         3162 |
    |          0 |     5 |    744 |         3906 |
    |          0 |     6 |    744 |         4650 |
    |          0 |     7 | 1348.5 |       5998.5 |
    |          0 |     8 | 1348.5 |         7347 |
    |          0 |     9 | 1348.5 |       8695.5 |
    |          1 |    10 | 1348.5 |        10044 |
    |          1 |    11 |   1302 |        11346 |
    |          1 |    12 |   1302 |        12648 |
    |          1 |    13 |   1302 |        13950 |
    |          1 |    14 |   1302 |        15252 |
    |          1 |    15 | 1255.5 |      16507.5 |
    |          1 |    16 |   1209 |      17716.5 |
    |          2 |    17 |   1116 |      18832.5 |
    |          2 |    18 |   1116 |      19948.5 |
    |          2 |    19 |   1302 |      21250.5 |
    |          2 |    20 |   1302 |      22552.5 |
    |          2 |    21 |   1302 |      23854.5 |
    |          2 |    22 | 1255.5 |        25110 |
    |          2 |    23 | 1255.5 |      26365.5 |
    |          2 |    24 |  976.5 |        27342 |

    Schema

    CREATE TABLE tbl (
      SubGroupNo tinyint,
      RowNo tinyint PRIMARY KEY,
      amount decimal(6,2),
      RunningValue decimal(7,2))
    
    INSERT INTO tbl (RowNo, amount, RunningValue)
    VALUES
    (1, '790.5', '790.5'),
    (2, '790.5', '1581'),
    (3, '790.5', '2371.5'),
    (4, '790.5', '3162'),
    (5, '744', '3906'),
    (6, '744', '4650'),
    (7, '1348.5', '5998.5'),
    (8, '1348.5', '7347'),
    (9, '1348.5', '8695.5'),
    (10, '1348.5', '10044'),
    (11, '1302', '11346'),
    (12, '1302', '12648'),
    (13, '1302', '13950'),
    (14, '1302', '15252'),
    (15, '1255.5', '16507.5'),
    (16, '1209', '17716.5'),
    (17, '1116', '18832.5'),
    (18, '1116', '19948.5'),
    (19, '1302', '21250.5'),
    (20, '1302', '22552.5'),
    (21, '1302', '23854.5'),
    (22, '1255.5', '25110'),
    (23, '1255.5', '26365.5'),
    (24, '976.5', '27342')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My question is somewhat similar to this one. We want to know if there's
My question somewhat overlaps with this and several other similar ones. Those have some
I referred to this somewhat similar question . However here the scenario is different:
My issue is somewhat similar to this SO question. However, since my implementation is
I have a question about Django, unixODBC, FreeTDS, Apache2, mod_wsgi, that is somewhat similar
Somewhat similar to this question , except we haven't decided that we're going with
I've a somewhat silly question, if i have a series of processes that are
This question is somewhat similar to How to combine two branches from two different
Rails 3.1, ActiveAdmin 0.3.4. My question is somewhat similar to this one but different
Someone else has already asked a somewhat similar question: Validate an Xml file against

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.