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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:43:28+00:00 2026-05-23T17:43:28+00:00

I have a positions table in SQL Server 2008R2 (definition below). In the system

  • 0

I have a positions table in SQL Server 2008R2 (definition below).

In the system boxes there are positions.

I have a requirement to find a box, which has X free positions remaining. However, the X positions must be continuous (left to right, top to bottom i.e. ascending PositionID).

It has been simple to construct a query that finds a box with X positions free. I now have the problem of determining if the positions are continuous.

Any suggestions on a TSQL based solution?

Table Definition

` CREATE TABLE [dbo].[Position](
        [PositionID] [int] IDENTITY(1,1) NOT NULL,
        [BoxID] [int] NOT NULL,
        [pRow] [int] NOT NULL,
        [pColumn] [int] NOT NULL,
        [pRowLetter] [char](1) NOT NULL,
        [pColumnLetter] [char](1) NOT NULL,
        [SampleID] [int] NULL,
        [ChangeReason] [nvarchar](4000) NOT NULL,
        [LastUserID] [int] NOT NULL,
        [TTSID] [bigint] NULL,
     CONSTRAINT [PK_Position] PRIMARY KEY CLUSTERED 
    (
        [PositionID] ASC
    )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
    ) ON [PRIMARY]`

Edit

http://pastebin.com/V8DLiucN – pastebin link with sample positions for 1 box (all positions empty in sample data)

Edit 2

A ‘free’ position is one with SampleID = null

  • 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-23T17:43:29+00:00Added an answer on May 23, 2026 at 5:43 pm
    DECLARE @AvailableSlots INT
    SET @AvailableSlots = 25
    
    ;WITH OrderedSet AS (
    SELECT
        BoxID,
        PositionID,
        Row_Number() OVER (PARTITION BY BoxID ORDER BY PositionID) AS rn
    FROM
        Position
    WHERE 
        SampleID IS NULL
    )
    SELECT
        BoxID,
        COUNT(*) AS AvailableSlots,
        MIN(PositionID) AS StartingPosition,
        MAX(PositionID) AS EndingPosition
    FROM
        OrderedSet
    GROUP BY
        PositionID - rn,
        BoxID
    HAVING
        COUNT(*) >= @AvailableSlots
    

    The trick is the PositionID - rn (row number) in the GROUP BY statement. This works to group together continuous sets… and from there it’s easy to just do a HAVING to limit the results to the BoxIDs that have the required amount of free slots.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using SQL Server 2008. I have a Student table in which there
I have the following very large table in SQL Server 2005: create table Blah
I have a SQL table called StudentMarks.which consist of StudentID,SubjectName,SubjectMark I want to write
I am using Microsoft SQL Server and I have a master-detail scenario where I
I have a silverlight 3 application, that fetches some simple data from a ms-sql-server
I'm generating T-SQL SELECT statements for tables for which I have no data type
I have an table which use an auto-increment field (ID) as primary key. The
I have a large table (100000+ rows) in which are 4 columns of same
I have 2 strings that I'd like to compare, and return the positions of
Let's say I have N pictures of an object, taken from N know positions.

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.