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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T20:14:14+00:00 2026-05-29T20:14:14+00:00

I have table the following data structure in SQL Server: ID Date Allocation 1,

  • 0

I have table the following data structure in SQL Server:

ID  Date        Allocation
 1, 2012-01-01, 0
 2, 2012-01-02, 2
 3, 2012-01-03, 0
 4, 2012-01-04, 0
 5, 2012-01-05, 0
 6, 2012-01-06, 5

etc.

What I need to do is get all consecutive day periods where Allocation = 0, and in the following form:

Start Date    End Date     DayCount
2012-01-01    2012-01-01   1
2012-01-03    2012-01-05   3

etc.

Is it possible to do this in SQL, and if so how?

  • 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-29T20:14:15+00:00Added an answer on May 29, 2026 at 8:14 pm

    In this answer, I’ll assume that the “id” field numbers the rows consecutively when sorted by increasing date, like it does in the example data. (Such a column can be created if it does not exist).

    This is an example of a technique described here and here.

    1) Join the table to itself on adjacent “id” values. This pairs adjacent rows. Select rows where the “allocation” field has changed. Store the result in a temporary table, also keeping a running index.

    SET @idx = 0;
    CREATE TEMPORARY TABLE boundaries
    SELECT
       (@idx := @idx + 1) AS idx,
       a1.date AS prev_end,
       a2.date AS next_start,
       a1.allocation as allocation
    FROM allocations a1
    JOIN allocations a2
    ON (a2.id = a1.id + 1)
    WHERE a1.allocation != a2.allocation;
    

    This gives you a table having “the end of the previous period”, “the start of the next period”, and “the value of ‘allocation’ in the previous period” in each row:

    +------+------------+------------+------------+
    | idx  | prev_end   | next_start | allocation |
    +------+------------+------------+------------+
    |    1 | 2012-01-01 | 2012-01-02 |          0 |
    |    2 | 2012-01-02 | 2012-01-03 |          2 |
    |    3 | 2012-01-05 | 2012-01-06 |          0 |
    +------+------------+------------+------------+
    

    2) We need the start and end of each period in the same row, so we need to combine adjacent rows again. Do this by creating a second temporary table like boundaries but having an idx field 1 greater:

    +------+------------+------------+
    | idx  | prev_end   | next_start |
    +------+------------+------------+
    |    2 | 2012-01-01 | 2012-01-02 |
    |    3 | 2012-01-02 | 2012-01-03 |
    |    4 | 2012-01-05 | 2012-01-06 |
    +------+------------+------------+
    

    Now join on the idx field and we get the answer:

    SELECT
      boundaries2.next_start AS start,
      boundaries.prev_end AS end,
      allocation
    FROM boundaries
    JOIN boundaries2
    USING(idx);
    
    +------------+------------+------------+
    | start      | end        | allocation |
    +------------+------------+------------+
    | 2012-01-02 | 2012-01-02 |          2 |
    | 2012-01-03 | 2012-01-05 |          0 |
    +------------+------------+------------+
    

    ** Note that this answer gets the “internal” periods correctly but misses the two “edge” periods where allocation = 0 at the beginning and allocation = 5 at the end. Those can be pulled in using UNION clauses but I wanted to present the core idea without that complication.

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

Sidebar

Related Questions

I have the following table and data in SQL Server 2005: create table LogEntries
I have a SQL Server table with the following structure: CREATE TABLE [dbo].[Log]( [LogID]
I have a table emp with following structure and data: name dept salary -----
I have a table with the following data: id | numbers | date ----------------------------------
I have a sql table which have the following data, Id City Country ---
I have an SQL table with basically the following structure: PK (int, primary key),
I have the following table structure with data as ReadingDate Unit 01-05-2011 10 01-06-2011
We need an in-memory data structure / DB server with following characteristics: stand-alone server
I have an XML structure like the following: <tables> <table name=tableName1> <row ID=34 col1=data
Let's say I have the following hypothetical data structure: create table country ( country_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.