4
votes

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
Could you provide some sample data via SQL script?Chris
@Chris - Thanks for looking, sample data link postedJonno
Which of these (or another) is the correct requirement?: 1) Given a number of free positions, X, find one or all boxes that match; or 2) just find all boxes with free continuous positions and count those positions. If in a certain box there is a group of 5 continuous (10 to 14) and another group of 10 continuous (51 to 60) free positions how many positions are there?Clodoaldo Neto

1 Answers

2
votes
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.