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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T08:18:30+00:00 2026-06-09T08:18:30+00:00

I have a table like this: create table time_sheet ( StatusCode char(1), start_time datetime,

  • 0

I have a table like this:

create table time_sheet
(
      StatusCode char(1),
      start_time datetime,
      end_time datetime
)

insert into time_sheet values
('W','2012-08-01 10:00:00','2012-08-01 12:00:00'),
('D','2012-08-01 12:00:00','2012-08-01 14:00:00'),
('N','2012-08-01 16:00:00','2012-08-01 18:00:00')

The output should be like this:

StatusCode start_time               end_time
B          2012-08-01 08:00:00.000  2012-08-01 10:00:00.000
W          2012-08-01 10:00:00.000  2012-08-01 12:00:00.000
D          2012-08-01 12:00:00.000  2012-08-01 14:00:00.000
B          2012-08-01 14:00:00.000  2012-08-01 16:00:00.000
N          2012-08-01 16:00:00.000  2012-08-01 18:00:00.000
B          2012-08-01 18:00:00.000  2012-08-01 20:00:00.000

The beging and end of the day are declared as below.

declare @begingOfDay datetime='2012-08-01 08:00:00.000'
declare @endOfDay    datetime='2012-08-01 20:00:00.000'

Basically I want to have missing time range records in the result set between begingOfDay and endOfDay with statusCode B . Please see that in the output there are 3 records added with StatusCode B

Could anybody help with this?

  • 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-09T08:18:31+00:00Added an answer on June 9, 2026 at 8:18 am

    Your sample data (note, Ts added to strings to enforce unambiguous date conversions):

    create table time_sheet
    (
          StatusCode char(1),
          start_time datetime,
          end_time datetime
    )
    
    insert into time_sheet values
    ('W','2012-08-01T10:00:00','2012-08-01T12:00:00'),
    ('D','2012-08-01T12:00:00','2012-08-01T14:00:00'),
    ('N','2012-08-01T16:00:00','2012-08-01T18:00:00')
    
    declare @begingOfDay datetime='2012-08-01T08:00:00.000'
    declare @endOfDay    datetime='2012-08-01T20:00:00.000'
    

    And the query:

    ;with AllDTs as (
        select @begingOfDay as TimePoint
        union
        select @endOfDay
        union
        select start_time from time_sheet
        union
        select end_time from time_sheet
    ), OrderedDTs as (
        select TimePoint,ROW_NUMBER() OVER (ORDER BY TimePoint) as rn
        from AllDTs
    ), Periods as (
        select o1.TimePoint as start_time,o2.TimePoint as end_time
        from
            OrderedDTs o1
                inner join
            OrderedDTs o2
                on
                    o1.rn = o2.rn-1
    )
    select
        COALESCE(ts.StatusCode,'B') as StatusCode,
        p.start_time,
        p.end_time
    from
        Periods p
            left join
        time_sheet ts
            on
                p.start_time = ts.start_time and
                p.end_time = ts.end_time
    

    Result:

    StatusCode start_time              end_time
    ---------- ----------------------- -----------------------
    B          2012-08-01 08:00:00.000 2012-08-01 10:00:00.000
    W          2012-08-01 10:00:00.000 2012-08-01 12:00:00.000
    D          2012-08-01 12:00:00.000 2012-08-01 14:00:00.000
    B          2012-08-01 14:00:00.000 2012-08-01 16:00:00.000
    N          2012-08-01 16:00:00.000 2012-08-01 18:00:00.000
    B          2012-08-01 18:00:00.000 2012-08-01 20:00:00.000
    

    Note, I’ve proceeded on the assumption that there are no overlapping time periods in the original table. The first CTE (AllDTs) just finds all unique datetime values that are of interest to us. OrderedDTs and Periods than arrange all of these datetime values into successive periods. The final query then takes each of these periods, and attempts to match them back to the original table, if possible. If not, then it’s obviously a B period.

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

Sidebar

Related Questions

I have a table like this: CREATE TABLE book_info ( book_id VARCHAR(32) not null,
I have a table created like this: CREATE TABLE rh857_omf.picture(MeasNr TINYINT UNSIGNED, ExperimentNr TINYINT
I have a table that looks like this: CREATE TABLE foobar ( id SERIAL
I have created an index on my table like this: CREATE INDEX index_typ_poplatky ON
I have a table that's created like this: CREATE TABLE bin_test (id INTEGER PRIMARY
Hi there I have an SQL table that looks like this: CREATE TABLE IF
I have something like this: create table account ( id int identity(1,1) primary key,
I have a schema that essentially looks like this: CREATE TABLE `data` ( `id`
I have an orders table with a schema like this. CREATE TABLE orders (
I have a testdata like this: DROP TABLE SELECT_PASS; CREATE TABLE SELECT_PASS(ID INT(20),TESTCASE VARCHAR(20),RESULT

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.