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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T10:37:45+00:00 2026-06-04T10:37:45+00:00

This code works really well for an initial insert into a table. But now

  • 0

This code works really well for an initial insert into a table. But now i need to be able to do ongoing updates to append new rows to a table… Do I need to do a SET for each item I’m inserting. I’ve tried doing that, actually, and it still complains.

CREATE TABLE dbo.Calcs
(
    OutageDate DATE,
    StartTime TIME(7),
    EndTime TIME(7),
    Duration INT
);

INSERT dbo.Calcs SELECT '20101110', '16:00', '17:30', 90;


INSERT dbo.Calcs SELECT '20101111', '13:00', '14:02', 62;
INSERT dbo.Calcs SELECT '20101112', '17:00', '18:00', 60;
INSERT dbo.Calcs SELECT '20101113', '16:05', '16:25', 20;
INSERT dbo.Calcs SELECT '20101114', '16:59', '18:01', 62;
INSERT dbo.Calcs SELECT '20101115', '22:15', '01:30', 165;

CREATE TABLE dbo.TableIWantUpdated
(
    OutageDate DATE,
    StartHour TIME(7),
    StartMinutes TIME(7),
    StartTime TIME(7),
    EndHour int,
    EndMinutes int,
    EndTime TIME(7),
    Duration INT
);

;WITH n(n) AS 
(
  SELECT TOP 24 ROW_NUMBER() OVER (ORDER BY [object_id])
  FROM sys.objects
),
x AS
(
  SELECT 
    o.OutageDate, StartHour = (DATEPART(HOUR, StartTime) + n.n - 1) % 24,
    StartTime, EndTime, Duration,
    rn = ROW_NUMBER() OVER (PARTITION BY o.OutageDate, o.StartTime ORDER BY n.n)
  FROM n INNER JOIN dbo.Calcs AS o
  ON n.n <= CEILING(DATEDIFF(MINUTE, CONVERT(DATETIME, StartTime), 
    DATEADD(DAY, CASE WHEN EndTime < StartTime THEN 1 ELSE 0 END, 
    CONVERT(DATETIME, EndTime)))/60.0)
 ),
 mx AS (SELECT OutageDate, StartTime, minrn = MIN(rn), maxrn = MAX(rn) 
   FROM x GROUP BY OutageDate, StartTime)

 -- I want to use the below inner join from the CTE to update a table now
 UPDATE [dbo].[TableIWantUpdated]
 SELECT 
x.OutageDate, 
    x.StartHour, 
    StartMinutes = CASE 
      WHEN x.rn = mx.minrn THEN DATEPART(MINUTE, x.StartTime) ELSE 0 END,
    EndHour = x.StartHour + 1, 
    EndMinutes = CASE
      WHEN x.rn = mx.maxrn THEN DATEPART(MINUTE, x.EndTime) ELSE 0 END,
    x.StartTime, 
    x.EndTime,
    x.Duration
    --this is where I did the initial insert of data...but now I want it to be able to update
    --INTO [dbo].[TableIWantUpdated] FROM x INNER JOIN mx
 FROM x INNER JOIN mx 
 ON x.OutageDate = mx.OutageDate
 AND x.StartTime = mx.StartTime
 ORDER BY x.OutageDate, x.rn;
GO
  • 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-04T10:37:46+00:00Added an answer on June 4, 2026 at 10:37 am

    INSERT INTO should work, then. I’m also removing the ORDER BY at the bottom.

    ;WITH n(n) AS 
    (
      SELECT TOP 24 ROW_NUMBER() OVER (ORDER BY [object_id])
      FROM sys.objects
    ),
    x AS
    (
      SELECT 
        o.OutageDate, StartHour = (DATEPART(HOUR, StartTime) + n.n - 1) % 24,
        StartTime, EndTime, Duration,
        rn = ROW_NUMBER() OVER (PARTITION BY o.OutageDate, o.StartTime ORDER BY n.n)
      FROM n INNER JOIN dbo.Calcs AS o
      ON n.n <= CEILING(DATEDIFF(MINUTE, CONVERT(DATETIME, StartTime), 
        DATEADD(DAY, CASE WHEN EndTime < StartTime THEN 1 ELSE 0 END, 
        CONVERT(DATETIME, EndTime)))/60.0)
     ),
     mx AS (SELECT OutageDate, StartTime, minrn = MIN(rn), maxrn = MAX(rn) 
       FROM x GROUP BY OutageDate, StartTime)
    
     -- I want to use the below inner join from the CTE to update a table now
     INSERT INTO [dbo].[TableIWantUpdated]
     SELECT 
    x.OutageDate, 
        x.StartHour, 
        StartMinutes = CASE 
          WHEN x.rn = mx.minrn THEN DATEPART(MINUTE, x.StartTime) ELSE 0 END,
        EndHour = x.StartHour + 1, 
        EndMinutes = CASE
          WHEN x.rn = mx.maxrn THEN DATEPART(MINUTE, x.EndTime) ELSE 0 END,
        x.StartTime, 
        x.EndTime,
        x.Duration
        --this is where I did the initial insert of data...but now I want it to be able to update
        --INTO [dbo].[TableIWantUpdated] FROM x INNER JOIN mx
     FROM x INNER JOIN mx 
     ON x.OutageDate = mx.OutageDate
     AND x.StartTime = mx.StartTime
     --ORDER BY x.OutageDate, x.rn;
    GO
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I refer to this question . The answer offered works really well but it
Thanks a lot for recent help. It works really well, but now I am
This code works great for generating thumbnails, but when given a very large (100MB+)
This code works fine to find an available room within certain date, but it
This code works when I try it from a .py file, but fails in
This code works fine in C#: Expression.Lambda(LambdaBody); But none of the methods for building
This code works... $(document).ready(function(){ $('body').css('background','black'); }); but this code doesn't... $(document).ready(function(){ $('thumb_slider').css('background','black'); }); ^^^the
Usually Xcode autocomplete works really well for me but when tabbing through method signatures
This code works: monkey.h @interface monkey : NSObject { NSNumber *monkeyRanch; } @property (nonatomic,
This code works (when ControlType=dropDown then the background yellow ): <Window x:Class=TestCollapsed.Views.MainView xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml

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.