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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:39:56+00:00 2026-05-25T16:39:56+00:00

Can I limit rows by sum of a column in a SQL Server database?

  • 0

Can I limit rows by sum of a column in a SQL Server database?

For example:

Type | Time (in minutes)
-------------------------
A    | 50
B    | 10
C    | 30
D    | 20
E    | 70
...

And I want to limit the selection by sum of time. For example maximum of 100 minutes. Table must look like this:

Type | Time (in minutes)
-------------------------
A    | 50
B    | 10
C    | 30

Any ideas? Thanks.

  • 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-25T16:39:57+00:00Added an answer on May 25, 2026 at 4:39 pm
    DECLARE @T TABLE
    (
    [Type] CHAR(1) PRIMARY KEY,
    [Time] INT
    )
    INSERT INTO @T
    SELECT 'A',50 UNION ALL
    SELECT 'B',10 UNION ALL
    SELECT 'C',30 UNION ALL
    SELECT 'D',20 UNION ALL
    SELECT 'E',70;
    
    
    WITH    RecursiveCTE
    AS      (
            SELECT TOP 1 [Type], [Time], CAST([Time] AS BIGINT) AS Total
            FROM @T
            ORDER BY [Type]
            UNION   ALL
            SELECT  R.[Type], R.[Time], R.Total
            FROM    (
                    SELECT  T.*,
                            T.[Time] + Total AS Total,
                            rn = ROW_NUMBER() OVER (ORDER BY T.[Type])
                    FROM    @T T
                    JOIN    RecursiveCTE R
                            ON  R.[Type] < T.[Type]
                    ) R
            WHERE   R.rn = 1 AND Total <= 100
            )
    SELECT  [Type], [Time], Total
    FROM    RecursiveCTE
    OPTION  (MAXRECURSION 0);
    

    Or if your table is small

    SELECT t1.[Type],
           t1.[Time],
           SUM(t2.[Time])
    FROM @T t1
     JOIN @T t2
    ON t2.[Type] <= t1.[Type]
    GROUP BY t1.[Type],t1.[Time]
    HAVING SUM(t2.[Time]) <=100
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to set the limit for my table's rows. How can I do
How can I limit my post-build events to running only for one type of
How can i limit a foreach() statement? Say i only want it to run
In the Django admin, the user can set filters which limit the rows displayed
I need to set a maximum limit of rows in my MySQL table. Documentation
I want to disable the selection of certain rows in my UIPickerView . Like
I have a number of rows from a database which I want to insert
I want to extract random rows from the app sqlite database in Android. I'm
I have a table var with some rows but only one column of type
I have a SQL Server 2005 database. I am logging data to a table.

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.