私は、停止を記述するテーブルで行を取り出し、別のテーブルに挿入して記述するために計算時間範囲で分割しますそれぞれの所与の範囲内の1時間ごとの時間帯。ストアドプロシージャから一時テーブル内のIDを経由して行を更新する必要があります
以下のコード私たちに次のような出力が得られます。
私たちは、この行(元データ)から行ってきました!私はストアドプロシージャ(これは所望の出力ISを実行した後、これに
OutageDate StartTime EndTime Duration
2010-11-10 16:00:00.0000000 17:30:00.0000000 90
私はちょうど)テーブルに保存する方法を知っておく必要があります。
OutageDate StartHour StartMinutes EndHour EndMinutes StartTime EndTime Duration
2010-11-10 16 0 17 0 16:00:00.0000000 17:30:00.0000000 90
2010-11-10 17 30 18 0 16:00:00.0000000 17:30:00.0000000 90
次私は私がしたい時間値を越え、私の行を分割後のテーブルに保存する方法を把握する必要があるコードです。これが起こることを持っている理由、それはちょうどこの形式でそれらによって私よりも誰かによって要求ごとのようにする必要があると指定した上で、私はコントロールを持っていない:
--First, let's look at the original data in the table...just one row will do
SELECT TOP (1) *
FROM actualTable
ORDER BY OutageDate ASC
--Begin sproc logic
declare @OutageDate date
declare @StartTime time(7)
declare @EndTime time(7)
declare @StartHour bigint
declare @EndHour int
declare @StartMinute int
declare @EndMinute int
declare @Duration int
declare @Temp_StartTime time
declare @Temp_EndTime time
declare @temp_StartHour int
declare @temp_EndHour int
declare @temp_StartMinute int
declare @temp_EndMinute int
SELECT TOP(1) @OutageDate = OutageDate, @StartTime = StartTime, @EndTime = EndTime, @Duration = Duration FROM actualTable
SET @[email protected]
SET @[email protected]
SET @temp_StartHour=DATEPART(HOUR, @StartTime)
SET @temp_EndHour=DATEPART(HOUR, @EndTime)
SET @temp_StartMinute=DATEPART(MI, @StartTime)
SET @temp_EndMinute=DATEPART(MI, @EndTime)
PRINT @temp_StartHour
PRINT @temp_EndHour
PRINT @temp_StartMinute
PRINT @StartTime
PRINT @EndTime
if(@temp_EndMinute>0)
BEGIN
SET @[email protected]_EndHour+1
END
--this declares the temp table
DECLARE @Temp_Table TABLE
(
OutageDate date,
StartHour int,
StartMinute int,
EndHour int,
EndMinute int,
StartTime time,
EndTime time,
Duration int
)
--Here's the loop that inserts the rows
While((@[email protected]_StartHour>1))
BEGIN
INSERT INTO @Temp_Table
SELECT
@OutageDate AS OutageDate,
(DATEPART(HOUR, @Temp_StartTime)) AS StartHour,
(DATEPART(MINUTE, @Temp_StartTime)) AS StartMinute,
@temp_StartHour+1 AS EndHour,
0 AS EndMinute,
@StartTime as StartTime,
@EndTime as EndTime,
@Duration AS Duration
--DATEADD returns a specified date with the specified number interval (signed integer) added to a specified datepart of that date.
SET @[email protected]_StartHour+1
SET @Temp_StartTime=DATEADD(HOUR,1,@Temp_StartTime)
--Let's make sure we account for the minutes in the first and last hours if any
if(DATEPART(MI, @Temp_StartTime)!=0)
BEGIN
SET @Temp_StartTime=DATEADD(MI,[email protected]_StartMinute,@Temp_StartTime)
END
END
--Ok, if we're at the last row insertion, we still need the minutes the outage finished at...those go into StartMinutes
WHile((@[email protected]_StartHour=1))
BEGIN
INSERT INTO @Temp_Table
SELECT
@OutageDate AS OutageDate,
(DATEPART(HOUR, @Temp_StartTime)) AS StartHour,
@temp_EndMinute AS StartMinute,
@temp_StartHour+1 AS EndHour,
0 AS EndMinute,
@StartTime as StartTime,
@EndTime as EndTime,
@Duration AS Duration
--DATEADD returns a specified date with the specified number interval (signed integer) added to a specified datepart of that date.
SET @[email protected]_StartHour+1
SET @Temp_StartTime=DATEADD(HOUR,1,@Temp_StartTime)
--Let's make sure we account for the minutes in the first and last hours if any
if(DATEPART(MI, @Temp_StartTime)!=0)
BEGIN
SET @Temp_StartTime=DATEADD(MI,[email protected]_StartMinute,@Temp_StartTime)
END
END
--Need to add logic that drops and recreates the table from the temp table so we don't have to employ a cursor
SELECT * FROM @Temp_Table
BEGIN
SELECT * INTO newTable FROM @Temp_Table
END
YOUがSMSでこのSTRAIGHTを実行する場合、 「は、その最も基本的で論理を得るでしょう:
declare @StartTime time
declare @EndTime time
declare @Temp_StartTime time
declare @temp_StartHour int
declare @temp_EndHour int
declare @temp_StartMinute int
declare @temp_EndMinute int
SET @StartTime='2:30:00'
SET @EndTime='4:01:00'
SET @[email protected]
SET @temp_StartHour=DATEPART(HOUR, @StartTime)
SET @temp_EndHour=DATEPART(HOUR, @EndTime)
SET @temp_StartMinute=DATEPART(MI, @StartTime)
SET @temp_EndMinute=DATEPART(MI, @EndTime)
if(@temp_EndMinute>0)
BEGIN
SET @[email protected]_EndHour+1
END
DECLARE @Temp_Table TABLE
(
StartHour int,
StartMinute int,
EndHour int,
EndMinute int,
StartTime time,
EndTime time
)
WHile((@[email protected]_StartHour>=1))
BEGIN
INSERT INTO @Temp_Table
SELECT (DATEPART(HOUR, @Temp_StartTime)) AS StartHour,(DATEPART(MINUTE, @Temp_StartTime)) AS StartMinute,
@temp_StartHour+1 AS EndHour,
0 AS EndMinute, @StartTime as StartTime, @EndTime as EndTime
SET @[email protected]_StartHour+1
SET @Temp_StartTime=DATEADD(HOUR,1,@Temp_StartTime)
if(DATEPART(MI, @Temp_StartTime)!=0)
BEGIN
SET @Temp_StartTime=DATEADD(MI,[email protected]_StartMinute,@Temp_StartTime)
END
END
SELECT * FROM @Temp_Table
リバースあなたのコードは非常に時間がかかることになるだろうエンジニアリング。私たちに動作しないクエリを与えるのではなく、 'actualTable'にいくつかのソース行を表示し、それらが出力先でどの行を生成するのでしょうか?私は[あなたの他の質問]で述べたように(http://stackoverflow.com/questions/10338436/how-to-convert-a-datepart-hour-thats-military-time-for-midnight-00-to-a-a-価値観)、私は本当にあなたが上で使用しているロジックがはるかに複雑であると信じています。しかし、私がソースデータを見ることができないのか、そこから抜け出そうとしているのかは分かりません。 –
Ok ...これを編集してみましょう。ありがとう。 – Lynn
開始時刻が22:15で終了時刻が00:30だった行をサンプルデータ(明らかに望ましい結果)に追加することができます。開始日と終了時刻が異なる日に発生したときにどのような出力が予想されるかを理解できます? –