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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T17:54:47+00:00 2026-06-08T17:54:47+00:00

SQL Server 2005+. I have a table with the following schema/data: Name Slots Date

  • 0

SQL Server 2005+.

I have a table with the following schema/data:

Name   Slots    Date
---------------------
Bob     1       1/1/2010 
Bob     2       1/8/2010 
Joe     4       1/2/2010 
Nat     1       1/4/2010 
Nat     3       1/3/2010 
Nat     8       1/9/2010 

What I need to do is find the row which for a particular name that results in the sum of slots of that name reaching or exceeding an arbitrary value – let’s say 3.

So in the above example, Bob has two rows. The second row, with a date of 1/8/2010, would be my match as the sum of 1 and 2 = 3.

Joe only has one row, and he would match as 4 is already greater than 3.

Nat has 3 rows, and in his case the matching row is the 2nd row with a date of 1/3/2010 since the sum of 1 and 3 is 4, and 4 is greater than 3.

So my results are:

Name   SlotsSum    Date
-------------------------
Bob     3       1/8/2010 
Joe     4       1/2/2010 
Nat     4       1/3/2010 

I know I can do this with a cursor, but I’m hoping for a set-based approach.

Thoughts?

  • 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-08T17:54:49+00:00Added an answer on June 8, 2026 at 5:54 pm

    Using two techniques, we can do what you want. CROSS APPLY to calculate a running total, and ROW_NUMBER so we can select the first row over your limit.

    declare @MyTable table (Name varchar(10), Slots int, Date date)
    insert @MyTable values
    ('Bob',     1 ,      '1/1/2010'),
    ('Bob',     2 ,      '1/8/2010'), 
    ('Joe',     4 ,      '1/2/2010 '),
    ('Nat',     1 ,      '1/4/2010 '),
    ('Nat',     3 ,      '1/3/2010 '),
    ('Nat',     8 ,      '1/9/2010 ')
    
    declare @Limit int = 3
    
    ;with cte as
    (
        select Name, Slots, Date, RunningTotal,
        ROW_NUMBER() over (partition by Name order by Name, Date) as rn
        from @MyTable rsTable
        cross apply 
        (
            select SUM(Slots) as RunningTotal
            from @MyTable rsApply
            where Date <= rsTable.Date and Name = rsTable.Name
        ) rsRunningTotal
        where RunningTotal >= @Limit
    
    )
    select Name, RunningTotal, Date
    from cte 
    where rn = 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using SQL Server 2005 I have a table with the following columns id name
I have a Sql Server 2005 table with following data: idHearing is the primary
I have the following table and data in SQL Server 2005: create table LogEntries
I have the following table variable in SQL Server 2005: DECLARE @results TABLE (id
I have a following scenario in my SQL Server 2005 database. zipcodes table has
I have the following tables in SQL Server 2005 ReceiptPoint: ID (PK), Name GasIndexLocation:
I have a table in SQL Server 2005 which has a date time field.
I'm using SQL-Server 2005 Standard. I have Users table with following columns: userID(int), userImage(varchar),
I have the following two table in SQL server 2005 DB 2- UserInfomration --->
In SQL Server 2005 I have a table with the following columns: Table Reservations:

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.