Hi all
I have a table with two records, and i want to manipulate one of the columns.
here are the records
Name DateCreated TotalTime(ms)
Chris 2013-01-31 07:32:20.657 9600000
Chris 2013-01-31 10:14:16.817 20479000
The column i want to manipulate is the totalTime(millisecond). i want each record to have a maximum time of less than an hour.
An hour = 3600000ms
expected results
Name DateCreated TotalTime(ms)
Chris 2013-01-31 07:32:20.657 3600000
Chris 2013-01-31 08:32:20.657 3600000
Chris 2013-01-31 09:32:20.657 2400000
------------------------------------------------
Chris 2013-01-31 10:14:16.817 3600000
Chris 2013-01-31 11:14:16.817 3600000
Chris 2013-01-31 12:14:16.817 3600000
Chris 2013-01-31 13:14:16.817 3600000
Chris 2013-01-31 14:14:16.817 3600000
Chris 2013-01-31 15:14:16.817 2479000
This is my code:
Declare @RowCount BigInt,
@Agent Varchar(50),
@Time datetime,
@TotalTime bigint,
@Version varchar(5),
@RowSize bigint,
@iNextAgentID Varchar(10),
@iLoopControl int,
@iCurrentAgentID Varchar(10),
@TotalTimeML int,
@iInnerLoopCtrl int
Select @RowCount = @@ROWCOUNT From AgentTimeDetailHRS
Select @RowCount RowCounts
select @iLoopControl = 0
select @iNextAgentID = min(Agent)
from AgentTimeDetailHRS
--Order by Time
if isnull(@iNextAgentID,'') = ''
begin
select 'No data in found in table!'
return
end
While @iLoopControl <= @RowCount
Begin
Select @Agent = Agent,
@Time = Time,
@TimeStamp = TimeStamp,
@TotalTime = TotalTime
From AgentTimeDetailHRS a
Where @iNextAgentID = a.Agent
Select @iLoopControl Loops
Select @TotalTimeML = Coalesce(@TotalTime, 0)
Select @TotalTimeML TotalTime
Select @Time Nako
--Inner loop to Iterate through total time > and hour
Select @iInnerLoopCtrl = 0
While @TotalTimeML > 3600000
Begin
Select @TotalTimeML = @TotalTimeML - 3600000
Select @iInnerLoopCtrl InnerLoop
Insert Into AgentTimeDetailHRSTEST
Select @Agent Agent,
DateAdd(HOUR, @iInnerLoopCtrl, @Time) Time,
3600000 TotalTime
select @iInnerLoopCtrl = @iInnerLoopCtrl + 1
Select @iNextTT = MIN(TotalTime)
from AgentTimeDetailHRS
End
Insert Into AgentTimeDetailHRSTEST
Select @Agent Agent,
DateAdd(HOUR, @iInnerLoopCtrl, @Time) Time,
@TotalTimeML TotalTime
Select @iCurrentAgentID = @iNextAgentID
select @iNextAgentID = null
select @iNextAgentID = min(Agent)
from AgentTimeDetailHRS
where Agent > @iCurrentAgentID
Select @iLoopControl = @iLoopControl + 1
End
The code works fine, but it only manipulate the second record, i need to manipulated all the records
Your help will be highly appreciated
Thank you!!
I have a table with two records, and i want to manipulate one of the columns.
here are the records
Name DateCreated TotalTime(ms)
Chris 2013-01-31 07:32:20.657 9600000
Chris 2013-01-31 10:14:16.817 20479000
The column i want to manipulate is the totalTime(millisecond). i want each record to have a maximum time of less than an hour.
An hour = 3600000ms
expected results
Name DateCreated TotalTime(ms)
Chris 2013-01-31 07:32:20.657 3600000
Chris 2013-01-31 08:32:20.657 3600000
Chris 2013-01-31 09:32:20.657 2400000
------------------------------------------------
Chris 2013-01-31 10:14:16.817 3600000
Chris 2013-01-31 11:14:16.817 3600000
Chris 2013-01-31 12:14:16.817 3600000
Chris 2013-01-31 13:14:16.817 3600000
Chris 2013-01-31 14:14:16.817 3600000
Chris 2013-01-31 15:14:16.817 2479000
This is my code:
Declare @RowCount BigInt,
@Agent Varchar(50),
@Time datetime,
@TotalTime bigint,
@Version varchar(5),
@RowSize bigint,
@iNextAgentID Varchar(10),
@iLoopControl int,
@iCurrentAgentID Varchar(10),
@TotalTimeML int,
@iInnerLoopCtrl int
Select @RowCount = @@ROWCOUNT From AgentTimeDetailHRS
Select @RowCount RowCounts
select @iLoopControl = 0
select @iNextAgentID = min(Agent)
from AgentTimeDetailHRS
--Order by Time
if isnull(@iNextAgentID,'') = ''
begin
select 'No data in found in table!'
return
end
While @iLoopControl <= @RowCount
Begin
Select @Agent = Agent,
@Time = Time,
@TimeStamp = TimeStamp,
@TotalTime = TotalTime
From AgentTimeDetailHRS a
Where @iNextAgentID = a.Agent
Select @iLoopControl Loops
Select @TotalTimeML = Coalesce(@TotalTime, 0)
Select @TotalTimeML TotalTime
Select @Time Nako
--Inner loop to Iterate through total time > and hour
Select @iInnerLoopCtrl = 0
While @TotalTimeML > 3600000
Begin
Select @TotalTimeML = @TotalTimeML - 3600000
Select @iInnerLoopCtrl InnerLoop
Insert Into AgentTimeDetailHRSTEST
Select @Agent Agent,
DateAdd(HOUR, @iInnerLoopCtrl, @Time) Time,
3600000 TotalTime
select @iInnerLoopCtrl = @iInnerLoopCtrl + 1
Select @iNextTT = MIN(TotalTime)
from AgentTimeDetailHRS
End
Insert Into AgentTimeDetailHRSTEST
Select @Agent Agent,
DateAdd(HOUR, @iInnerLoopCtrl, @Time) Time,
@TotalTimeML TotalTime
Select @iCurrentAgentID = @iNextAgentID
select @iNextAgentID = null
select @iNextAgentID = min(Agent)
from AgentTimeDetailHRS
where Agent > @iCurrentAgentID
Select @iLoopControl = @iLoopControl + 1
End
The code works fine, but it only manipulate the second record, i need to manipulated all the records
Your help will be highly appreciated
Thank you!!