Author |
Topic |
WebKill
Starting Member
32 Posts |
Posted - 2014-04-25 : 02:12:47
|
I have two tables, one with records, one with log entries of records loaded. The log entry starts before the records load so the timestamp is before the timestamp of the records in the datatable. If there was only one logtable entry per day it wouldn't be so bad but there could be multiple.I have fixed the process so that going forward the timestamps will match up, but I wanted to fix past records by replacing the datatable datetime with the logtable one, but I am having trouble matching the specific logtable entry to the datatable items.DECLARE @DataTable TABLE(ActivityTime DATETIME,ID varchar(10))INSERT INTO @DataTable VALUES('2014-04-08 8:01:00.000', 'R1'),('2014-04-08 8:01:00.000', 'R2'),('2014-04-08 8:02:00.000', 'R3'),('2014-04-08 8:02:00.000', 'R4'),('2014-04-08 8:03:00.000', 'R5'),('2014-04-08 8:03:00.000', 'R6'),('2014-04-08 8:11:00.000', 'R7'),('2014-04-08 8:11:00.000', 'R8'),('2014-04-08 8:12:00.000', 'R9'),('2014-04-08 8:12:00.000', 'R10'),('2014-04-08 8:13:00.000', 'R11'),('2014-04-08 8:13:00.000', 'R12')DECLARE @LogTable TABLE(LogTime DATETIME,recordcount int)INSERT INTO @LogTable VALUES('2014-04-08 8:00:00.000', 6),('2014-04-08 8:10:00.000', 6)In this single day example, I would want DataTable records R1 - R6 to have the activitytime updated to '2014-04-08 8:00:00.000', and R7 - R12 to be updated to '2014-04-08 8:10:00.000' |
|
stepson
Aged Yak Warrior
545 Posts |
Posted - 2014-04-25 : 04:07:34
|
[code]DECLARE @DataTable TABLE(ActivityTime DATETIME,ID varchar(10))INSERT INTO @DataTable VALUES('2014-04-08 8:01:00.000', 'R1'),('2014-04-08 8:01:00.000', 'R2'),('2014-04-08 8:02:00.000', 'R3'),('2014-04-08 8:02:00.000', 'R4'),('2014-04-08 8:03:00.000', 'R5'),('2014-04-08 8:03:00.000', 'R6'),('2014-04-08 8:11:00.000', 'R7'),('2014-04-08 8:11:00.000', 'R8'),('2014-04-08 8:12:00.000', 'R9'),('2014-04-08 8:12:00.000', 'R10'),('2014-04-08 8:13:00.000', 'R11'),('2014-04-08 8:13:00.000', 'R12')DECLARE @LogTable TABLE(LogTime DATETIME,recordcount int)INSERT INTO @LogTable VALUES('2014-04-08 8:00:00.000', 6),('2014-04-08 8:10:00.000', 6),('2014-04-08 8:20:00.000', 6);with LogTableAS( select LogTime ,recordCount ,ROW_NUMBER() OVER(Order By LogTime ASC) as Rn from @LogTable)UPDATE ASET A.ActivityTime=LT.LogTimeFROM @DataTable A INNER JOIN LogTable LT ON A.ActivityTime >= LT.LogTime INNER JOIN LogTable LT2 ON A.ActivityTime <= LT2.LogTime AND LT.rn+1=LT2.Rnselect * from @DataTable[/code]output[code]ActivityTime ID2014-04-08 08:00:00.000 R12014-04-08 08:00:00.000 R22014-04-08 08:00:00.000 R32014-04-08 08:00:00.000 R42014-04-08 08:00:00.000 R52014-04-08 08:00:00.000 R62014-04-08 08:10:00.000 R72014-04-08 08:10:00.000 R82014-04-08 08:10:00.000 R92014-04-08 08:10:00.000 R102014-04-08 08:10:00.000 R112014-04-08 08:10:00.000 R12[/code] sabinWeb MCP |
|
|
stepson
Aged Yak Warrior
545 Posts |
Posted - 2014-04-25 : 04:43:13
|
another way, the same outputUPDATE ASET A.ActivityTime=LT.LogTimeFROM @DataTable A OUTER APPLY ( SELECT TOP 1 LogTime FROM LogTable WHERE ActivityTime > = LogTime ORDER BY LogTime DESC )LT sabinWeb MCP |
|
|
|
|
|