Author |
Topic |
batcater98
Starting Member
22 Posts |
Posted - 2012-07-23 : 15:34:49
|
I have a table that when sorted the correct way I want to be able to compare from row to row and look for certain things and if those things are matched I want to update a field on the compared to row.Example of fields in a rowEventID, Exception, Date, StartTime, EndTime, NewTime, UserID, PosDupSo then row by row I want to compare row1 to row2 and update row2 if match is made.Match is made if? EventID-row2 = EventID-row1 and Date-row2 = Date-row1 and UserID-row2 = UserID-row1 and StartTime-row2 < Newtime-row1Set PosDup-row2 = "1"I am wanting to do this in one SQL statement if possible but having issues working out the logic. I want to continue this througout the whole table comparing each row to the next. |
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2012-07-23 : 15:55:35
|
Two questions:1. Do you have another column in the table that can be used to order the rows? Or perhaps the rows can be ordered based on an the existing columns such as StartTime? By definition, rows in a table are unordered set, so we will need some way to order it to be able to do this.2. If you had three consecutive rows, row1, row2, and row3 which were such that, row1 and row2 satisfied your four conditions and also row2 and row3 satisfied those conditions, did you want the PosDup for all three rows to be assigned the PosDup of the first row, or would row3 get the Posdup of row2 and row2 then get the Posdup of row1? |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-07-23 : 16:43:48
|
it would be much better if you can explain with a data sample of what you're trying to achieve so as to enable us to help you better------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
Brendthess
Starting Member
2 Posts |
Posted - 2012-07-23 : 20:05:43
|
I have had to do some work like this on legacy data in the past where rows had to be matched based on similar data and minor differences. in that case, the only way to successfully complete the process was to use a cursor. I suspect that your issue might be solvable without one, but that depends on what the data actually looks like. So please, give us some sample data including any sorting needed.If you are counting on the actual order that the data was inserted into the database; remember, order in SQL Server is not absolute, and counting on the data to be returned in the order inserted is unreliable unless there is a field that can the data can be ordered by. |
|
|
batcater98
Starting Member
22 Posts |
Posted - 2012-07-31 : 10:02:20
|
Example Data:EventID Date Exp_Time End_Time New_Time UserIDB AAOCHI7 7/12/12 08:09:21 08:09:29 08:11:21 7323314B AAOCHI7 7/12/12 08:09:23 08:09:31 08:11:23 7323314B AAOCHI7 7/12/12 08:31:37 08:31:47 08:33:37 7323314So with the example data here when sorted by EventID, Date, Exp_Time, UserID since EventID of row2=row1 and Date row2=row1 and UserID row2=row1 and End_Time row2 <= New_Time row1 - I would update PosDup with a "1". But when looking at row3 - row2 all conditions match except End_Time row3 is NOT <= New_Time row2, so I would not want to update PosDup row3 with a "1" but leave it NULL.I am wanting to walk through the whole table in this manor row by row updating PosDup when the criteria are met.So resulting data of the update query would beEventID Date Exp_Time End_Time New_Time UserID PosDupB AAOCHI7 7/12/12 08:09:21 08:09:29 08:11:21 7323314 NULLB AAOCHI7 7/12/12 08:09:23 08:09:31 08:11:23 7323314 1B AAOCHI7 7/12/12 08:31:37 08:31:47 08:33:37 7323314 NULLThanks for the help!Regards,The Dark Knight-Give What is Right, Not What is Left- |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-07-31 : 10:30:10
|
[code]UPDATE tSET t.PostDup=1FROM table tCROSS APPLY (SELECT TOP 1 End_Time FROM table WHERE EventID = t.EventID AND [Date] = t.[Date] AND userID = t.UserID AND Exp_Time < t.Exp_Time ORDER BY Exp_Time DESC)t1WHERE t1.End_Time> t.Exp_time[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-07-31 : 10:36:53
|
if it is sql 2012, there's much better wayUPDATE tSET t.PostDup=1FROM (SELECT PostDup,Exp_Time,LAG(End_Time) OVER (PARTITION BY EventID,UserID,[Date] ORDER BY Exp_Time) AS PrevEndTimeFROM table)tWHERE PrevEndTime > Exp_Time ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
batcater98
Starting Member
22 Posts |
Posted - 2012-07-31 : 13:29:23
|
visakh16 - Thanks for the help, this is working great. One thing is that in addition to flagging the row that is found to be a duplicate it is also flagging the row it compared it to. So example below.Desired Out Come:EventID Date Exp_Time End_Time New_Time UserID PosDupB AAOCHI7 7/12/12 08:09:21 08:09:29 08:11:21 7323314 NULLB AAOCHI7 7/12/12 08:09:23 08:09:31 08:11:23 7323314 1B AAOCHI7 7/12/12 08:31:37 08:31:47 08:33:37 7323314 NULLOut Come current queury is providing:EventID Date Exp_Time End_Time New_Time UserID PosDupB AAOCHI7 7/12/12 08:09:21 08:09:29 08:11:21 7323314 1B AAOCHI7 7/12/12 08:09:23 08:09:31 08:11:23 7323314 1B AAOCHI7 7/12/12 08:31:37 08:31:47 08:33:37 7323314 NULLHow can I modify it so that it only flags row2 in this example and not both?Again, Thanks.. Oh and I only wish we were on 2012.Regards,The Dark Knight-Give What is Right, Not What is Left- |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-07-31 : 15:01:09
|
]i dont think so you're doing it correctly, as i'm getting correct outputsee illustration belowDECLARE @Test table(EventID varchar(20),[Date] date,Exp_Time time,End_Time time,New_Time time,UserID int,PostDup bit)INSERT INTO @Test (EventID,Date,Exp_Time,End_Time,New_Time,UserID)VALUES ('B AAOCHI7','7/12/2012','8:09:21','8:09:29','8:11:21',7323314),('B AAOCHI7','7/12/2012','8:09:23','8:09:31','8:11:23',7323314), ('B AAOCHI7','7/12/2012','8:31:37','8:31:47','8:33:37',7323314) UPDATE tSET t.PostDup=1FROM @Test tCROSS APPLY (SELECT TOP 1 End_Time FROM @Test WHERE EventID = t.EventID AND [Date] = t.[Date] AND userID = t.UserID AND Exp_Time < t.Exp_Time ORDER BY Exp_Time DESC)t1WHERE t1.End_Time> t.Exp_timeselect * from @testoutput-------------------------------------------------------EventID Date Exp_Time End_Time New_Time UserID PostDupB AAOCHI7 2012-07-12 08:09:21.0000000 08:09:29.0000000 08:11:21.0000000 7323314 NULLB AAOCHI7 2012-07-12 08:09:23.0000000 08:09:31.0000000 08:11:23.0000000 7323314 1B AAOCHI7 2012-07-12 08:31:37.0000000 08:31:47.0000000 08:33:37.0000000 7323314 NULL ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
batcater98
Starting Member
22 Posts |
Posted - 2012-07-31 : 15:49:24
|
visakh16: This is my fault for not explaining my dataset completely - since it has multiple EventID's in it - sorting by just Exp_Time will not work as it does not put the records in the correct order. Below is a more complete example of my data and how it is structured. If I expand upon your Order By statement to include EventID, Exp_Date, Exp_Time, UserID it works, but adds value in PosDup for both dup records.New Example of data.EventID Date Exp_Time End_Time New_Start_Time UserIDB AAOCHI7 11 07/12/12 8:09:21 8:09:29 8:11:21 7323314B AAOCHI7 11 07/12/12 8:09:23 8:09:31 8:11:23 7323314B AAOCHI7 11 07/12/12 8:31:37 8:31:47 8:33:37 7323314B AAOCHI7 11 07/12/12 8:31:39 8:31:49 8:33:39 7323314B AAOCHI7 11 07/12/12 9:12:48 9:12:56 9:14:48 7323314B AAOCHI7 11 07/12/12 9:12:50 9:12:58 9:14:50 7323314B AAOCHI7 11 07/12/12 22:48:42 22:48:48 22:50:42 1466598B AAOCHI7 11 07/12/12 22:48:44 22:48:50 22:50:44 1466598B AAOLPC7 18 07/18/12 18:03:58 18:04:04 18:05:58 7435035B AAOLPC7 18 07/18/12 18:03:59 18:04:05 18:05:59 7435035B AAOLPC7 18 07/18/12 18:25:09 18:25:18 18:27:09 7435035B AAOLPC7 18 07/18/12 18:25:10 18:25:19 18:27:10 7435035B AAOLPC7 18 07/18/12 19:05:44 19:05:50 19:07:44 7435035B AAOLPC7 18 07/18/12 19:05:45 19:05:51 19:07:45 7435035B AAOLPC7 18 07/18/12 19:48:18 19:48:25 19:50:18 7435035B AAOLPC7 18 07/18/12 19:48:19 19:48:26 19:50:19 7435035B AAOLPC7 18 07/19/12 1:28:22 1:28:28 1:30:22 4094900B AAOLPC7 18 07/19/12 6:01:28 6:01:38 6:03:28 1019421B AAOLPC7 18 07/19/12 6:01:29 6:01:39 6:03:29 1019421B AAOLPC7 18 07/19/12 7:06:22 7:06:29 7:08:22 1019421B AAOLPC7 18 07/19/12 7:06:23 7:06:30 7:08:23 1019421B AAOLPC7 18 07/19/12 7:06:24 7:06:31 7:08:24 1019421B AAOLPC7 18 07/19/12 8:34:35 8:34:43 8:36:35 1019421B AAOLPC7 18 07/19/12 8:34:36 8:34:44 8:36:36 1019421B AAOLPC7 18 07/19/12 8:34:37 8:34:45 8:36:37 1019421B AAOLPC7 18 07/19/12 9:04:19 9:04:25 9:06:19 1019421B AAOLPC7 18 07/19/12 9:04:20 9:04:26 9:06:20 1019421B AAOLPC7 18 07/19/12 9:04:21 9:04:27 9:06:21 1019421B AAOLPC7 18 07/19/12 10:19:45 10:19:51 10:21:45 1019421B AAOLPC7 18 07/19/12 10:19:46 10:19:52 10:21:46 1019421B AAOLPC7 18 07/19/12 10:19:47 10:19:53 10:21:47 1019421Again, thank you for all the help.Regards,The Dark Knight-Give What is Right, Not What is Left- |
|
|
batcater98
Starting Member
22 Posts |
Posted - 2012-07-31 : 16:08:07
|
Visakh16: I did find an error in my sql statement. I am getting good results now from my complete data set. Thanks for all the help!Regards,The Dark Knight-Give What is Right, Not What is Left- |
|
|
batcater98
Starting Member
22 Posts |
Posted - 2012-07-31 : 16:35:47
|
I must come back - It is not carring through the dataset and looking at each record and I cannot figure out how.Data Input:EventID Date Exp_Time End_Time New_Start_Time UserIDB AAOCHI7 11 07/12/12 8:09:21 8:09:29 8:11:21 7323314B AAOCHI7 11 07/12/12 8:09:23 8:09:31 8:11:23 7323314B AAOCHI7 11 07/12/12 8:31:37 8:31:47 8:33:37 7323314B AAOCHI7 11 07/12/12 8:31:39 8:31:49 8:33:39 7323314B AAOCHI7 11 07/12/12 9:12:48 9:12:56 9:14:48 7323314B AAOCHI7 11 07/12/12 9:12:50 9:12:58 9:14:50 7323314B AAOCHI7 11 07/12/12 22:48:42 22:48:48 22:50:42 1466598B AAOCHI7 11 07/12/12 22:48:44 22:48:50 22:50:44 1466598B AAOLPC7 18 07/18/12 18:03:58 18:04:04 18:05:58 7435035B AAOLPC7 18 07/18/12 18:03:59 18:04:05 18:05:59 7435035B AAOLPC7 18 07/18/12 18:25:09 18:25:18 18:27:09 7435035B AAOLPC7 18 07/18/12 18:25:10 18:25:19 18:27:10 7435035B AAOLPC7 18 07/18/12 19:05:44 19:05:50 19:07:44 7435035B AAOLPC7 18 07/18/12 19:05:45 19:05:51 19:07:45 7435035B AAOLPC7 18 07/18/12 19:48:18 19:48:25 19:50:18 7435035B AAOLPC7 18 07/18/12 19:48:19 19:48:26 19:50:19 7435035B AAOLPC7 18 07/19/12 1:28:22 1:28:28 1:30:22 4094900B AAOLPC7 18 07/19/12 6:01:28 6:01:38 6:03:28 1019421B AAOLPC7 18 07/19/12 6:01:29 6:01:39 6:03:29 1019421B AAOLPC7 18 07/19/12 7:06:22 7:06:29 7:08:22 1019421B AAOLPC7 18 07/19/12 7:06:23 7:06:30 7:08:23 1019421B AAOLPC7 18 07/19/12 7:06:24 7:06:31 7:08:24 1019421B AAOLPC7 18 07/19/12 8:34:35 8:34:43 8:36:35 1019421B AAOLPC7 18 07/19/12 8:34:36 8:34:44 8:36:36 1019421B AAOLPC7 18 07/19/12 8:34:37 8:34:45 8:36:37 1019421B AAOLPC7 18 07/19/12 9:04:19 9:04:25 9:06:19 1019421B AAOLPC7 18 07/19/12 9:04:20 9:04:26 9:06:20 1019421B AAOLPC7 18 07/19/12 9:04:21 9:04:27 9:06:21 1019421B AAOLPC7 18 07/19/12 10:19:45 10:19:51 10:21:45 1019421B AAOLPC7 18 07/19/12 10:19:46 10:19:52 10:21:46 1019421B AAOLPC7 18 07/19/12 10:19:47 10:19:53 10:21:47 1019421Desired OutputEventID Date Exp_Time End_Time New_Start_Time UserID PosDupB AAOCHI7 11 07/12/12 8:09:21 8:09:29 8:11:21 7323314 NULLB AAOCHI7 11 07/12/12 8:09:23 8:09:31 8:11:23 7323314 1B AAOCHI7 11 07/12/12 8:31:37 8:31:47 8:33:37 7323314 NULLB AAOCHI7 11 07/12/12 8:31:39 8:31:49 8:33:39 7323314 1B AAOCHI7 11 07/12/12 9:12:48 9:12:56 9:14:48 7323314 NULLB AAOCHI7 11 07/12/12 9:12:50 9:12:58 9:14:50 7323314 1B AAOCHI7 11 07/12/12 22:48:42 22:48:48 22:50:42 1466598 NULLB AAOCHI7 11 07/12/12 22:48:44 22:48:50 22:50:44 1466598 1B AAOLPC7 18 07/18/12 18:03:58 18:04:04 18:05:58 7435035 NULLB AAOLPC7 18 07/18/12 18:03:59 18:04:05 18:05:59 7435035 1B AAOLPC7 18 07/18/12 18:25:09 18:25:18 18:27:09 7435035 NULLB AAOLPC7 18 07/18/12 18:25:10 18:25:19 18:27:10 7435035 1B AAOLPC7 18 07/18/12 19:05:44 19:05:50 19:07:44 7435035 NULLB AAOLPC7 18 07/18/12 19:05:45 19:05:51 19:07:45 7435035 1B AAOLPC7 18 07/18/12 19:48:18 19:48:25 19:50:18 7435035 NULLB AAOLPC7 18 07/18/12 19:48:19 19:48:26 19:50:19 7435035 1B AAOLPC7 18 07/19/12 1:28:22 1:28:28 1:30:22 4094900 NULLB AAOLPC7 18 07/19/12 6:01:28 6:01:38 6:03:28 1019421 NULLB AAOLPC7 18 07/19/12 6:01:29 6:01:39 6:03:29 1019421 1B AAOLPC7 18 07/19/12 7:06:22 7:06:29 7:08:22 1019421 NULLB AAOLPC7 18 07/19/12 7:06:23 7:06:30 7:08:23 1019421 1B AAOLPC7 18 07/19/12 7:06:24 7:06:31 7:08:24 1019421 1B AAOLPC7 18 07/19/12 8:34:35 8:34:43 8:36:35 1019421 NULLB AAOLPC7 18 07/19/12 8:34:36 8:34:44 8:36:36 1019421 1B AAOLPC7 18 07/19/12 8:34:37 8:34:45 8:36:37 1019421 1B AAOLPC7 18 07/19/12 9:04:19 9:04:25 9:06:19 1019421 NULLB AAOLPC7 18 07/19/12 9:04:20 9:04:26 9:06:20 1019421 1B AAOLPC7 18 07/19/12 9:04:21 9:04:27 9:06:21 1019421 1B AAOLPC7 18 07/19/12 10:19:45 10:19:51 10:21:45 1019421 NULLB AAOLPC7 18 07/19/12 10:19:46 10:19:52 10:21:46 1019421 1B AAOLPC7 18 07/19/12 10:19:47 10:19:53 10:21:47 1019421 1Multiple Dups when comparing row to row - Is using Top 1 only comparing the top record to others?My SQL as I am running it right now.UPDATE tSET t .PosDup = 1FROM AlerterAlarmAudible_Temp t CROSS APPLY (SELECT TOP 1 End_Time FROM AlerterAlarmAudible_Temp WHERE EventID = t .TrainID AND [Date] = t.[Date] AND UserID = t.UserID AND Exp_Time < t.Exp_Time ORDER BY EventID, Date, Exp_Time, UserID DESC) t1WHERE t1.End_Time > t .Exp_timeOutput I am currently getting:EventID Date Exp_Time End_Time New_Start_Time User_ID PosDupB AAOCHI7 11 07/12/12 8:09:21 8:09:29 8:11:21 7323314 NULLB AAOCHI7 11 07/12/12 8:09:23 8:09:31 8:11:23 7323314 1B AAOCHI7 11 07/12/12 8:31:37 8:31:47 8:33:37 7323314 NULLB AAOCHI7 11 07/12/12 8:31:39 8:31:49 8:33:39 7323314 NULLB AAOCHI7 11 07/12/12 9:12:48 9:12:56 9:14:48 7323314 NULLB AAOCHI7 11 07/12/12 9:12:50 9:12:58 9:14:50 7323314 NULLB AAOCHI7 11 07/12/12 22:48:42 22:48:48 22:50:42 1466598 NULLB AAOCHI7 11 07/12/12 22:48:44 22:48:50 22:50:44 1466598 1B AAOLPC7 18 07/18/12 18:03:58 18:04:04 18:05:58 7435035 NULLB AAOLPC7 18 07/18/12 18:03:59 18:04:05 18:05:59 7435035 1B AAOLPC7 18 07/18/12 18:25:09 18:25:18 18:27:09 7435035 NULLB AAOLPC7 18 07/18/12 18:25:10 18:25:19 18:27:10 7435035 NULLB AAOLPC7 18 07/18/12 19:05:44 19:05:50 19:07:44 7435035 NULLB AAOLPC7 18 07/18/12 19:05:45 19:05:51 19:07:45 7435035 NULLB AAOLPC7 18 07/18/12 19:48:18 19:48:25 19:50:18 7435035 NULLB AAOLPC7 18 07/18/12 19:48:19 19:48:26 19:50:19 7435035 NULLB AAOLPC7 18 07/19/12 1:28:22 1:28:28 1:30:22 4094900 NULLB AAOLPC7 18 07/19/12 6:01:28 6:01:38 6:03:28 1019421 NULLB AAOLPC7 18 07/19/12 6:01:29 6:01:39 6:03:29 1019421 1B AAOLPC7 18 07/19/12 7:06:22 7:06:29 7:08:22 1019421 NULLB AAOLPC7 18 07/19/12 7:06:23 7:06:30 7:08:23 1019421 NULLB AAOLPC7 18 07/19/12 7:06:24 7:06:31 7:08:24 1019421 NULLB AAOLPC7 18 07/19/12 8:34:35 8:34:43 8:36:35 1019421 NULLB AAOLPC7 18 07/19/12 8:34:36 8:34:44 8:36:36 1019421 NULLB AAOLPC7 18 07/19/12 8:34:37 8:34:45 8:36:37 1019421 NULLB AAOLPC7 18 07/19/12 9:04:19 9:04:25 9:06:19 1019421 NULLB AAOLPC7 18 07/19/12 9:04:20 9:04:26 9:06:20 1019421 NULLB AAOLPC7 18 07/19/12 9:04:21 9:04:27 9:06:21 1019421 NULLB AAOLPC7 18 07/19/12 10:19:45 10:19:51 10:21:45 1019421 NULLB AAOLPC7 18 07/19/12 10:19:46 10:19:52 10:21:46 1019421 NULLB AAOLPC7 18 07/19/12 10:19:47 10:19:53 10:21:47 1019421 NULLRegards,The Dark Knight-Give What is Right, Not What is Left- |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-07-31 : 16:51:38
|
why are you comparing eventid to trainid?also you dont require other fields in ORDER BY as you're already comparing on them in whereUPDATE tSET t .PosDup = 1FROM AlerterAlarmAudible_Temp t CROSS APPLY(SELECT TOP 1 End_TimeFROM AlerterAlarmAudible_TempWHERE EventID = t .TrainIDEventID AND [Date] = t.[Date] AND UserID = t.UserID AND Exp_Time < t.Exp_TimeORDER BY EventID, Date, Exp_Time, UserID DESC) t1WHERE t1.End_Time > t .Exp_time ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
batcater98
Starting Member
22 Posts |
Posted - 2012-07-31 : 17:04:53
|
Thank you for clearification - I believe I have it working like it should.Batcater98 - Out!Regards,The Dark Knight-Give What is Right, Not What is Left- |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-07-31 : 17:16:10
|
ok...cool------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
batcater98
Starting Member
22 Posts |
Posted - 2012-07-31 : 17:30:12
|
visa: You have been so helpful - let me put one more step to you. Now that I finally got all of the dups from the table I want to modify the query just a little to update and find potitial sequincial events based on the New_Start_Time of row1 and the End_Time of row2.So in English Terms from same data structure above - But now say if End_Time(row2)<= New_Start_Time(row1) then update Seq_Alrm = 1 If not Seq_Alrm = NULL. And traverse throught the table row by row. Making sure EventID = t.EventID and Date = t.date and userid = t.userid.Regards,The Dark Knight-Give What is Right, Not What is Left- |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-07-31 : 17:39:56
|
quote: Originally posted by batcater98 visa: You have been so helpful - let me put one more step to you. Now that I finally got all of the dups from the table I want to modify the query just a little to update and find potitial sequincial events based on the New_Start_Time of row1 and the End_Time of row2.So in English Terms from same data structure above - But now say if End_Time(row2)<= New_Start_Time(row1) then update Seq_Alrm = 1 If not Seq_Alrm = NULL. And traverse throught the table row by row. Making sure EventID = t.EventID and Date = t.date and userid = t.userid.Regards,The Dark Knight-Give What is Right, Not What is Left-
its very much similar to the requirement you asked earlier.extend my logic and try this one yourself thats the only way you'll understand how it actually works.Let me know in case you face any hurdles and I'll help you out.------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
|