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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:42:20+00:00 2026-06-18T03:42:20+00:00

I need to copy each row as many times as how many hours there

  • 0

I need to copy each row as many times as how many hours there are between StartTime and EndTime.

Example data:

SQLFIDDLEExample

TimeKey HourKey SensorKey   IdleTimeMinute  StartTime   EndTime
20121017    8   45  110 2012.10.17 08:31    2012.10.17 10:21
20121017    10  45  25  2012.10.17 10:26    2012.10.17 10:51
20121017    12  45  5   2012.10.17 12:21    2012.10.17 12:26
20121017    12  45  60  2012.10.17 12:41    2012.10.17 13:41
20121017    13  45  55  2012.10.17 13:51    2012.10.17 14:46
20121017    15  45  5   2012.10.17 15:11    2012.10.17 15:16
20121017    15  45  35  2012.10.17 15:46    2012.10.17 16:21
20121017    18  45  5   2012.10.17 18:51    2012.10.17 18:56

Explanation

For example, the first row spans the hours 8, 9, and 10. In each of the three output rows, IdleTimeMinute needs to be the minutes duration within that hour.

Expected Result:

TimeKey HourKey SensorKey   IdleTimeMinute  StartTime   EndTime
20121017    8   45  29  2012.10.17 08:31    2012.10.17 10:21
20121017    9   45  60  2012.10.17 08:31    2012.10.17 10:21
20121017    10  45  21  2012.10.17 08:31    2012.10.17 10:21
20121017    10  45  25  2012.10.17 10:26    2012.10.17 10:51
20121017    12  45  5   2012.10.17 12:21    2012.10.17 12:26
20121017    12  45  19  2012.10.17 12:41    2012.10.17 13:41
20121017    13  45  41  2012.10.17 12:41    2012.10.17 13:41
20121017    13  45  9   2012.10.17 13:51    2012.10.17 14:46
20121017    14  45  46  2012.10.17 13:51    2012.10.17 14:46
20121017    15  45  5   2012.10.17 15:11    2012.10.17 15:16
20121017    15  45  14  2012.10.17 15:46    2012.10.17 16:21
20121017    16  45  21  2012.10.17 15:46    2012.10.17 16:21
20121017    18  45  5   2012.10.17 18:51    2012.10.17 18:56

Smaller Example

Smaller example from first two lines:

TimeKey HourKey SensorKey   IdleTimeMinute  StartTime   EndTime
20121017    8   45  110 2012.10.17 08:31    2012.10.17 10:21
20121017    10  45  25  2012.10.17 10:26    2012.10.17 10:51

For first line we have time 8:31 - 10:21 so In 8 Hour 29 Minutes, In 9 Hour 60 Minutes, In 10 Hour 21 Minute.

Expected small result:

TimeKey HourKey SensorKey   IdleTimeMinute  StartTime   EndTime
20121017    8   45  29  2012.10.17 08:31    2012.10.17 10:21
20121017    9   45  60  2012.10.17 08:31    2012.10.17 10:21
20121017    10  45  21  2012.10.17 08:31    2012.10.17 10:21
20121017    10  45  25  2012.10.17 10:26    2012.10.17 10:51
  • 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-18T03:42:21+00:00Added an answer on June 18, 2026 at 3:42 am

    Here you go. This will handle even cases where the span of time passes midnight or goes over many days. It should perform very well.

    See the SqlFiddle for it.

    DECLARE @MaxHour int = IsNull((SELECT Max(DateDiff(hour, StartTime, EndTime)) + 1 FROM dbo.Table1), 0);
    
    WITH L0 AS (SELECT 1 N UNION ALL SELECT 1),
    L1 AS (SELECT 1 N FROM L0, L0 B),
    L2 AS (SELECT 1 N FROM L1, L1 B),
    L3 AS (SELECT 1 N FROM L2, L2 B),
    L4 AS (SELECT 1 N FROM L3, L3 B),
    L5 AS (SELECT 1 N FROM L4, L4 B),
    Nums AS (SELECT Num = Row_Number() OVER (ORDER BY (SELECT 1)) FROM L5)
    SELECT
       TimeKey = Convert(int, Convert(char(8), S.HourStartTime, 112)),
       HourKey = DatePart(hour, S.HourStartTime),
       T.SensorKey,
       IdleTimeMinute = DateDiff(minute, S.HourStartTime, E.HourEndTime),
       T.StartTime,
       T.EndTime,
       S.HourStartTime,
       E.HourEndTime
    FROM
       dbo.Table1 T
       CROSS APPLY (
          SELECT AnchorHour = DateAdd(hour, DateDiff(hour, 0, T.StartTime) + N.Num - 1, 0)
          FROM Nums N
          WHERE
             DateDiff(hour, T.StartTime, T.EndTime) + 1 >= N.Num
             AND N.Num <= @MaxHour
       ) D
       CROSS APPLY (
          SELECT HourStartTime = Max(StartTime)
          FROM (VALUES (D.AnchorHour), (T.StartTime)) S (StartTime)
       ) S
       CROSS APPLY (
          SELECT HourEndTime = Min(EndTime)
          FROM (VALUES (DateAdd(hour, 1, D.AnchorHour)), (T.EndTime)) E (EndTime)
       ) E
    ORDER BY
       TimeKey,
       StartTime,
       HourKey;
    

    I included the new timespan for the selected hour. If you want that timespan to end at :59 instead of :00 of the next hour, then in the third CROSS APPLY change DateAdd(hour, 1, D.AnchorHour) to DateAdd(minute, 59, D.AnchorHour) and add + 1 to the end of DateDiff(minute, S.HourStartTime, E.HourEndTime) in the SELECT clause.

    In my opinion:

    • Using a CTE to incrementally add one’s way up to the max difference is not optimal. The CTE I use bails out close to as soon as the max Num is reached, and can go up to 4294967296.
    • Solutions that don’t calculate the new TimeKey are fragile due to depending on an unstated assumption that midnight cannot be crossed.
    • Solutions that depend on you changing your table to have a monotonically-increasing ID with no gaps are unlikely to fit your real-world data.

    Note: the reason I declare the @MaxHour variable is that the on-the-fly numbers table CTE I’m using wants a constant. Well, that’s not exactly accurate, but the long and short of it is that without a constant it can choose an execution plan that doesn’t limit the rows in the place needed to get a speedy query. I chose DateDiff to compute this, but in fact I see now that this would probably be faster, and definitely would be faster if you have an index on IdleTimeMinute:

    DECLARE @MaxHour int = IsNull((SELECT (Max(IdleTimeMinute) + 118) / 60 FROM dbo.Table1), 0);
    

    Since the smallest time range that can span two different hours is 2 minutes (say 11:59 to 12:00) then we have to add 118 minutes to be sure to get at least 2 hours out of it.

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

Sidebar

Related Questions

I need to copy files and directories within a directory. Suppose there is only
I need to copy a full row in an SQL table but add -Copy
I need to copy or synchronize 2 libraries between 2 different servers. Here are
I need to copy data from one database to another using a VB.NET program.
On click of the Copy link in a row, I need to create another
I have a little problem. I need to copy the attributes of a row,
I need to copy several files to remote server. for database in `mysql -Bseshow
I need to copy two linked lists recursively and return a new list .
I need to copy an object that has a pretty deep hierarchy of member
I need to copy all *.exe files in some directory to other virtual drive

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.