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

  • Home
  • SEARCH
  • 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 6749795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:43:57+00:00 2026-05-26T12:43:57+00:00

I have 2 SQL Server tables with the following structure Turns-time cod_turn (PrimaryKey) time

  • 0

I have 2 SQL Server tables with the following structure

Turns-time

cod_turn (PrimaryKey) 
time     (datetime)

Taken turns

cod_taken_turn (Primary Key)
cod_turn
...

and several other fields which are irrelevant to the problem. I cant alter the table structures because the app was made by someone else.

given a numeric variable parameter, which we will assume to be “3” for this example, and a given time, I need to create a query which looking from that time on, it looks the first 3 consecutive records by time which are not marked as “taken”. For example:

For example, for these turns, starting by the time of “8:00” chosen by the user

 8:00 (not taken)
 9:00 (not taken)
10:00 (taken)
11:00 (not taken)
12:00 (not taken)
13:00 (not taken)
14:00 (taken)

The query it would have to list

11:00
12:00
13:00

I cant figure out how to make the query in pure sql, if possible.

  • 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-26T12:43:58+00:00Added an answer on May 26, 2026 at 12:43 pm
    WITH Base AS (
    SELECT *,
           CASE WHEN EXISTS(
                   SELECT * 
                   FROM Taken_turns taken 
                   WHERE taken.cod_turn = turns.cod_turn) THEN 1 ELSE 0 END AS taken
    FROM   [Turns-time] turns)
    , RecursiveCTE As (
    SELECT TOP 1 cod_turn, [time], taken AS run, 0 AS grp
    FROM  Base 
    WHERE [time] >= @start_time
    ORDER BY [time]
    UNION   ALL
    SELECT  R.cod_turn, R.[time], R.run, R.grp
    FROM    (
            SELECT  T.*,
                    CASE WHEN T.taken = 0 THEN 0 ELSE run+1 END AS run,
                    CASE WHEN T.taken = 0 THEN grp + 1 ELSE grp END AS grp,
                    rn = ROW_NUMBER() OVER (ORDER BY T.[time])
            FROM    Base T
            JOIN    RecursiveCTE R
                    ON  R.[time] < T.[time]
            ) R
    WHERE   R.rn = 1 AND run < @run_length
    ),  T AS(
    SELECT *, 
            MAX(grp) OVER () AS FinalGroup,
            COUNT(*) OVER (PARTITION BY grp) AS group_size
    FROM RecursiveCTE
    )
    SELECT cod_turn,time
    FROM T 
    WHERE grp=FinalGroup AND group_size=@run_length
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a SQL Server table with the following structure cod_turn (PrimaryKey) taken (bit)
I have two SQL Server tables with Primary Keys (PK) and a Foreign Key
I have two tables in a SQL Server 2008 environment with the following structure
I have the following tables in SQL Server 2005 ReceiptPoint: ID (PK), Name GasIndexLocation:
i have the following tables in sql server: photoalbumsTable: album_ID album_caption albumtagmap id album_id
I have to write a component that re-creates SQL Server tables (structure and data)
I have a SQL Server table with the following structure: CREATE TABLE [dbo].[Log]( [LogID]
I have the following table in SQL Server 2000: TABLE_NAME | COLUMN_NAME | TYPE_NAME
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

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.