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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:00:55+00:00 2026-05-13T23:00:55+00:00

I have a tricky problem that has had me scratching my head for a

  • 0

I have a tricky problem that has had me scratching my head for a little while. I have some data that represents the delivery of “widgets” over a variable number of days, broken down into half hourly slots.

For example (apologies for formatting – haven’t quite got to grips with it):

Date          Time    NoOfUnits  
01-Mar-2010   00:00   0  
01-Mar-2010   00:30   0  
01-Mar-2010   01:00   0  
.... (following half hour intervals have NoOfUnits = 0)
01-Mar-2010   23:00   10  
01-Mar-2010   23:30   10
02-Mar-2010   00:00   10
.... (following half hour intervals have NoOfUnits = 1)
02-Mar-2010   07:00   10
02-Mar-2010   07:30   0
.... (following half hour intervals have NoOfUnits = 0)
02-Mar-2010   23:30   0

I need to generate a query that will allow me to group this data into all the distinct blocks where I am delivering a unit. In the above example I need to identify only 1 block – 23:00 to 07:00, and the sum of units for that block (160). The required result would therefore be StartTime, EndTime, TotalNoOfUnits.

However, the complexity comes when we have different patterns of delivery – maybe we have a day where we deliver units for 24 hours.

I need to be able to query data in the format above and identify all the unique StartTime, EndTime and TotalNoOfUnits combinations where NoOfUnits <> 0.

Apologies again for formatting and a slightly rambling question. Please ask any questions you need for me to clarify things.

EDIT: Just to be clear, the data will always run from 00:00 to 23:30 for each day of delivery, and each half hour slot will always be present. It is only the number of days and unit per half hour slot that may vary for any given data set.

EDIT2: Below is a script that will populate a table with 2 days worth of schedule data. The schedule is the same for both days. The result I would expect to see, based on my requirements, would be 13:00, 00:00, 230. As you will see from the below my SQL skills are not great hence the head scratching!

declare @DayCount int
declare @HalfHourCount int
declare @HH int
declare @CurrentDate datetime
declare @BaseDate datetime
declare @NoOfUnits int

set @HalfHourCount = 48
set @DayCount = 4
set @BaseDate = '1 Jan 1900'

create table #Schedule
(
Date datetime
, Time datetime
, NoOfUnits int
)

while @DayCount > 0
begin
set @CurrentDate = dateadd(dd, @DayCount * -1, CONVERT(VARCHAR(10),GETDATE(),111))
set @HH = @HalfHourCount

while @HH > 0
begin
    if @HH > 24
        set @NoOfUnits = 10
    else
    begin
        if @DayCount = 4 and @HH < 10
            set @NoOfUnits = 10
        else
            set @NoOfUnits = 0
    end

    insert into #Schedule(Date, Time, NoOfUnits)
    values (@CurrentDate, dateadd(mi, @HH / 2.0 * 60, @BaseDate), @NoOfUnits)

    select @HH = @HH - 1
end

set @DayCount = @DayCount - 1
end

Expected result (although test data should start from 00:00 and go to 23:00 instead of 00:30 to 00:00):

StartTime               TotalUnits  EndDate
----------------------- ----------- -----------------------
1900-01-01 00:30:00.000 90          1900-01-01 04:30:00.000
1900-01-01 12:30:00.000 960         1900-01-02 00:00:00.000
  • 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-13T23:00:56+00:00Added an answer on May 13, 2026 at 11:00 pm

    This should work, based on the data you have provided. It can almost certainly be simplified:

    ;WITH dateCTE
    AS
    (
            SELECT * 
                   ,date + [time] dt
            FROM #Schedule
    )
    ,seqCTE
    AS
    (
            SELECT NoOfUnits
                   ,dt
                   ,ROW_NUMBER() OVER (ORDER BY dt) AS rn
            FROM dateCTE               
    )
    ,recCTE
    AS
    (
            SELECT NoOfUnits
                   ,dt
                   ,1 AS SEQUENCE
                   ,rn
            FROM seqCTE
            WHERE rn = 1
    
            UNION ALL
    
            SELECT s.NoOfUnits
                  ,s.dt
                  ,CASE WHEN (s.NoOfUnits      > 0 
                              AND  r.NoOfUnits > 0
                             )
                          OR (s.NoOfUnits      = 0 
                              AND  r.NoOfUnits = 0
                             )
                        THEN r.SEQUENCE
                        ELSE r.SEQUENCE + 1
                   END
                  ,s.rn
            FROM recCTE AS r
            JOIN seqCTE AS s
            ON   s.rn = r.rn + 1
    )
    ,summaryCTE
    AS
    (
            SELECT RIGHT(CONVERT(varchar(23),MIN(dt),120),8)  AS startTime
                   ,RIGHT(CONVERT(varchar(23),MAX(dt),120),8) AS endTime
                   ,SUM(NoOfUnits) AS NoOfUnits
            FROM recCTE
            GROUP BY SEQUENCE
            HAVING SUM(NoOfUnits) != 0
    )
    SELECT startTime
           ,endTime
           ,SUM(NoOfUnits)
    FROM summaryCTE  
    group by startTime
             ,endTime  
    OPTION (MAXRECURSION 0)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 354k
  • Answers 354k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You need to do this through VBScript and load in… May 14, 2026 at 7:52 am
  • Editorial Team
    Editorial Team added an answer EDIT: I tried to clarify I bit more ... 1.… May 14, 2026 at 7:52 am
  • Editorial Team
    Editorial Team added an answer Read each line, stick the contents of the line into… May 14, 2026 at 7:52 am

Related Questions

I found a blog entry which suggests that sometimes c# compiler may decide to
I have gone through a long tutorial on W3Schooles to learn CSS; I learnt
I have the following use case: There's a class called Template and with that
I am having trouble while using the YUI panel as a dialog. I have
Been a while since I've dealt with ASP.NET and this is the first time

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.