SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Update row in table by comparing data in table
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

batcater98
Starting Member

USA
20 Posts

Posted - 07/23/2012 :  15:34:49  Show Profile  Reply with Quote
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 row

EventID, Exception, Date, StartTime, EndTime, NewTime, UserID, PosDup

So 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-row1

Set 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
Flowing Fount of Yak Knowledge

5152 Posts

Posted - 07/23/2012 :  15:55:35  Show Profile  Reply with Quote
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?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
48029 Posts

Posted - 07/23/2012 :  16:43:48  Show Profile  Reply with Quote
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 MVP
http://visakhm.blogspot.com/

Go to Top of Page

Brendthess
Starting Member

USA
2 Posts

Posted - 07/23/2012 :  20:05:43  Show Profile  Reply with Quote
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.
Go to Top of Page

batcater98
Starting Member

USA
20 Posts

Posted - 07/31/2012 :  10:02:20  Show Profile  Reply with Quote
Example Data:

EventID Date Exp_Time End_Time New_Time UserID
B AAOCHI7 7/12/12 08:09:21 08:09:29 08:11:21 7323314
B AAOCHI7 7/12/12 08:09:23 08:09:31 08:11:23 7323314
B AAOCHI7 7/12/12 08:31:37 08:31:47 08:33:37 7323314

So 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 be

EventID Date Exp_Time End_Time New_Time UserID PosDup
B AAOCHI7 7/12/12 08:09:21 08:09:29 08:11:21 7323314 NULL
B AAOCHI7 7/12/12 08:09:23 08:09:31 08:11:23 7323314 1
B AAOCHI7 7/12/12 08:31:37 08:31:47 08:33:37 7323314 NULL


Thanks for the help!

Regards,
The Dark Knight
-Give What is Right, Not What is Left-

Edited by - batcater98 on 07/31/2012 10:05:49
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
48029 Posts

Posted - 07/31/2012 :  10:30:10  Show Profile  Reply with Quote

UPDATE t
SET t.PostDup=1
FROM table t
CROSS 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)t1
WHERE t1.End_Time> t.Exp_time


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
48029 Posts

Posted - 07/31/2012 :  10:36:53  Show Profile  Reply with Quote
if it is sql 2012, there's much better way


UPDATE t
SET t.PostDup=1
FROM 
(SELECT PostDup,
Exp_Time,
LAG(End_Time) OVER (PARTITION BY EventID,UserID,[Date] ORDER BY Exp_Time) AS PrevEndTime
FROM table
)t
WHERE PrevEndTime > Exp_Time




------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

batcater98
Starting Member

USA
20 Posts

Posted - 07/31/2012 :  13:29:23  Show Profile  Reply with Quote
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 PosDup
B AAOCHI7 7/12/12 08:09:21 08:09:29 08:11:21 7323314 NULL
B AAOCHI7 7/12/12 08:09:23 08:09:31 08:11:23 7323314 1
B AAOCHI7 7/12/12 08:31:37 08:31:47 08:33:37 7323314 NULL

Out Come current queury is providing:
EventID Date Exp_Time End_Time New_Time UserID PosDup
B AAOCHI7 7/12/12 08:09:21 08:09:29 08:11:21 7323314 1
B AAOCHI7 7/12/12 08:09:23 08:09:31 08:11:23 7323314 1
B AAOCHI7 7/12/12 08:31:37 08:31:47 08:33:37 7323314 NULL

How 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-
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
48029 Posts

Posted - 07/31/2012 :  15:01:09  Show Profile  Reply with Quote
]i dont think so you're doing it correctly, as i'm getting correct output

see illustration below


DECLARE @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 t
SET t.PostDup=1
FROM @Test t
CROSS 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)t1
WHERE t1.End_Time> t.Exp_time


select * from @test


output
-------------------------------------------------------
EventID	     Date	         Exp_Time	         End_Time	         New_Time	         UserID	PostDup
B AAOCHI7	     2012-07-12	08:09:21.0000000	08:09:29.0000000	08:11:21.0000000	7323314	NULL
B AAOCHI7	     2012-07-12	08:09:23.0000000	08:09:31.0000000	08:11:23.0000000	7323314	1
B AAOCHI7	     2012-07-12	08:31:37.0000000	08:31:47.0000000	08:33:37.0000000	7323314	NULL



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

batcater98
Starting Member

USA
20 Posts

Posted - 07/31/2012 :  15:49:24  Show Profile  Reply with Quote
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 UserID
B AAOCHI7 11 07/12/12 8:09:21 8:09:29 8:11:21 7323314
B AAOCHI7 11 07/12/12 8:09:23 8:09:31 8:11:23 7323314
B AAOCHI7 11 07/12/12 8:31:37 8:31:47 8:33:37 7323314
B AAOCHI7 11 07/12/12 8:31:39 8:31:49 8:33:39 7323314
B AAOCHI7 11 07/12/12 9:12:48 9:12:56 9:14:48 7323314
B AAOCHI7 11 07/12/12 9:12:50 9:12:58 9:14:50 7323314
B AAOCHI7 11 07/12/12 22:48:42 22:48:48 22:50:42 1466598
B AAOCHI7 11 07/12/12 22:48:44 22:48:50 22:50:44 1466598
B AAOLPC7 18 07/18/12 18:03:58 18:04:04 18:05:58 7435035
B AAOLPC7 18 07/18/12 18:03:59 18:04:05 18:05:59 7435035
B AAOLPC7 18 07/18/12 18:25:09 18:25:18 18:27:09 7435035
B AAOLPC7 18 07/18/12 18:25:10 18:25:19 18:27:10 7435035
B AAOLPC7 18 07/18/12 19:05:44 19:05:50 19:07:44 7435035
B AAOLPC7 18 07/18/12 19:05:45 19:05:51 19:07:45 7435035
B AAOLPC7 18 07/18/12 19:48:18 19:48:25 19:50:18 7435035
B AAOLPC7 18 07/18/12 19:48:19 19:48:26 19:50:19 7435035
B AAOLPC7 18 07/19/12 1:28:22 1:28:28 1:30:22 4094900
B AAOLPC7 18 07/19/12 6:01:28 6:01:38 6:03:28 1019421
B AAOLPC7 18 07/19/12 6:01:29 6:01:39 6:03:29 1019421
B AAOLPC7 18 07/19/12 7:06:22 7:06:29 7:08:22 1019421
B AAOLPC7 18 07/19/12 7:06:23 7:06:30 7:08:23 1019421
B AAOLPC7 18 07/19/12 7:06:24 7:06:31 7:08:24 1019421
B AAOLPC7 18 07/19/12 8:34:35 8:34:43 8:36:35 1019421
B AAOLPC7 18 07/19/12 8:34:36 8:34:44 8:36:36 1019421
B AAOLPC7 18 07/19/12 8:34:37 8:34:45 8:36:37 1019421
B AAOLPC7 18 07/19/12 9:04:19 9:04:25 9:06:19 1019421
B AAOLPC7 18 07/19/12 9:04:20 9:04:26 9:06:20 1019421
B AAOLPC7 18 07/19/12 9:04:21 9:04:27 9:06:21 1019421
B AAOLPC7 18 07/19/12 10:19:45 10:19:51 10:21:45 1019421
B AAOLPC7 18 07/19/12 10:19:46 10:19:52 10:21:46 1019421
B AAOLPC7 18 07/19/12 10:19:47 10:19:53 10:21:47 1019421

Again, thank you for all the help.

Regards,
The Dark Knight
-Give What is Right, Not What is Left-
Go to Top of Page

batcater98
Starting Member

USA
20 Posts

Posted - 07/31/2012 :  16:08:07  Show Profile  Reply with Quote
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-
Go to Top of Page

batcater98
Starting Member

USA
20 Posts

Posted - 07/31/2012 :  16:35:47  Show Profile  Reply with Quote
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 UserID
B AAOCHI7 11 07/12/12 8:09:21 8:09:29 8:11:21 7323314
B AAOCHI7 11 07/12/12 8:09:23 8:09:31 8:11:23 7323314
B AAOCHI7 11 07/12/12 8:31:37 8:31:47 8:33:37 7323314
B AAOCHI7 11 07/12/12 8:31:39 8:31:49 8:33:39 7323314
B AAOCHI7 11 07/12/12 9:12:48 9:12:56 9:14:48 7323314
B AAOCHI7 11 07/12/12 9:12:50 9:12:58 9:14:50 7323314
B AAOCHI7 11 07/12/12 22:48:42 22:48:48 22:50:42 1466598
B AAOCHI7 11 07/12/12 22:48:44 22:48:50 22:50:44 1466598
B AAOLPC7 18 07/18/12 18:03:58 18:04:04 18:05:58 7435035
B AAOLPC7 18 07/18/12 18:03:59 18:04:05 18:05:59 7435035
B AAOLPC7 18 07/18/12 18:25:09 18:25:18 18:27:09 7435035
B AAOLPC7 18 07/18/12 18:25:10 18:25:19 18:27:10 7435035
B AAOLPC7 18 07/18/12 19:05:44 19:05:50 19:07:44 7435035
B AAOLPC7 18 07/18/12 19:05:45 19:05:51 19:07:45 7435035
B AAOLPC7 18 07/18/12 19:48:18 19:48:25 19:50:18 7435035
B AAOLPC7 18 07/18/12 19:48:19 19:48:26 19:50:19 7435035
B AAOLPC7 18 07/19/12 1:28:22 1:28:28 1:30:22 4094900
B AAOLPC7 18 07/19/12 6:01:28 6:01:38 6:03:28 1019421
B AAOLPC7 18 07/19/12 6:01:29 6:01:39 6:03:29 1019421
B AAOLPC7 18 07/19/12 7:06:22 7:06:29 7:08:22 1019421
B AAOLPC7 18 07/19/12 7:06:23 7:06:30 7:08:23 1019421
B AAOLPC7 18 07/19/12 7:06:24 7:06:31 7:08:24 1019421
B AAOLPC7 18 07/19/12 8:34:35 8:34:43 8:36:35 1019421
B AAOLPC7 18 07/19/12 8:34:36 8:34:44 8:36:36 1019421
B AAOLPC7 18 07/19/12 8:34:37 8:34:45 8:36:37 1019421
B AAOLPC7 18 07/19/12 9:04:19 9:04:25 9:06:19 1019421
B AAOLPC7 18 07/19/12 9:04:20 9:04:26 9:06:20 1019421
B AAOLPC7 18 07/19/12 9:04:21 9:04:27 9:06:21 1019421
B AAOLPC7 18 07/19/12 10:19:45 10:19:51 10:21:45 1019421
B AAOLPC7 18 07/19/12 10:19:46 10:19:52 10:21:46 1019421
B AAOLPC7 18 07/19/12 10:19:47 10:19:53 10:21:47 1019421

Desired Output

EventID Date Exp_Time End_Time New_Start_Time UserID PosDup
B AAOCHI7 11 07/12/12 8:09:21 8:09:29 8:11:21 7323314 NULL
B AAOCHI7 11 07/12/12 8:09:23 8:09:31 8:11:23 7323314 1
B AAOCHI7 11 07/12/12 8:31:37 8:31:47 8:33:37 7323314 NULL
B AAOCHI7 11 07/12/12 8:31:39 8:31:49 8:33:39 7323314 1
B AAOCHI7 11 07/12/12 9:12:48 9:12:56 9:14:48 7323314 NULL
B AAOCHI7 11 07/12/12 9:12:50 9:12:58 9:14:50 7323314 1
B AAOCHI7 11 07/12/12 22:48:42 22:48:48 22:50:42 1466598 NULL
B AAOCHI7 11 07/12/12 22:48:44 22:48:50 22:50:44 1466598 1
B AAOLPC7 18 07/18/12 18:03:58 18:04:04 18:05:58 7435035 NULL
B AAOLPC7 18 07/18/12 18:03:59 18:04:05 18:05:59 7435035 1
B AAOLPC7 18 07/18/12 18:25:09 18:25:18 18:27:09 7435035 NULL
B AAOLPC7 18 07/18/12 18:25:10 18:25:19 18:27:10 7435035 1
B AAOLPC7 18 07/18/12 19:05:44 19:05:50 19:07:44 7435035 NULL
B AAOLPC7 18 07/18/12 19:05:45 19:05:51 19:07:45 7435035 1
B AAOLPC7 18 07/18/12 19:48:18 19:48:25 19:50:18 7435035 NULL
B AAOLPC7 18 07/18/12 19:48:19 19:48:26 19:50:19 7435035 1
B AAOLPC7 18 07/19/12 1:28:22 1:28:28 1:30:22 4094900 NULL
B AAOLPC7 18 07/19/12 6:01:28 6:01:38 6:03:28 1019421 NULL
B AAOLPC7 18 07/19/12 6:01:29 6:01:39 6:03:29 1019421 1
B AAOLPC7 18 07/19/12 7:06:22 7:06:29 7:08:22 1019421 NULL
B AAOLPC7 18 07/19/12 7:06:23 7:06:30 7:08:23 1019421 1
B AAOLPC7 18 07/19/12 7:06:24 7:06:31 7:08:24 1019421 1
B AAOLPC7 18 07/19/12 8:34:35 8:34:43 8:36:35 1019421 NULL
B AAOLPC7 18 07/19/12 8:34:36 8:34:44 8:36:36 1019421 1
B AAOLPC7 18 07/19/12 8:34:37 8:34:45 8:36:37 1019421 1
B AAOLPC7 18 07/19/12 9:04:19 9:04:25 9:06:19 1019421 NULL
B AAOLPC7 18 07/19/12 9:04:20 9:04:26 9:06:20 1019421 1
B AAOLPC7 18 07/19/12 9:04:21 9:04:27 9:06:21 1019421 1
B AAOLPC7 18 07/19/12 10:19:45 10:19:51 10:21:45 1019421 NULL
B AAOLPC7 18 07/19/12 10:19:46 10:19:52 10:21:46 1019421 1
B AAOLPC7 18 07/19/12 10:19:47 10:19:53 10:21:47 1019421 1

Multiple 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 t
SET t .PosDup = 1
FROM 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) t1
WHERE t1.End_Time > t .Exp_time

Output I am currently getting:

EventID Date Exp_Time End_Time New_Start_Time User_ID PosDup
B AAOCHI7 11 07/12/12 8:09:21 8:09:29 8:11:21 7323314 NULL
B AAOCHI7 11 07/12/12 8:09:23 8:09:31 8:11:23 7323314 1
B AAOCHI7 11 07/12/12 8:31:37 8:31:47 8:33:37 7323314 NULL
B AAOCHI7 11 07/12/12 8:31:39 8:31:49 8:33:39 7323314 NULL
B AAOCHI7 11 07/12/12 9:12:48 9:12:56 9:14:48 7323314 NULL
B AAOCHI7 11 07/12/12 9:12:50 9:12:58 9:14:50 7323314 NULL
B AAOCHI7 11 07/12/12 22:48:42 22:48:48 22:50:42 1466598 NULL
B AAOCHI7 11 07/12/12 22:48:44 22:48:50 22:50:44 1466598 1
B AAOLPC7 18 07/18/12 18:03:58 18:04:04 18:05:58 7435035 NULL
B AAOLPC7 18 07/18/12 18:03:59 18:04:05 18:05:59 7435035 1
B AAOLPC7 18 07/18/12 18:25:09 18:25:18 18:27:09 7435035 NULL
B AAOLPC7 18 07/18/12 18:25:10 18:25:19 18:27:10 7435035 NULL
B AAOLPC7 18 07/18/12 19:05:44 19:05:50 19:07:44 7435035 NULL
B AAOLPC7 18 07/18/12 19:05:45 19:05:51 19:07:45 7435035 NULL
B AAOLPC7 18 07/18/12 19:48:18 19:48:25 19:50:18 7435035 NULL
B AAOLPC7 18 07/18/12 19:48:19 19:48:26 19:50:19 7435035 NULL
B AAOLPC7 18 07/19/12 1:28:22 1:28:28 1:30:22 4094900 NULL
B AAOLPC7 18 07/19/12 6:01:28 6:01:38 6:03:28 1019421 NULL
B AAOLPC7 18 07/19/12 6:01:29 6:01:39 6:03:29 1019421 1
B AAOLPC7 18 07/19/12 7:06:22 7:06:29 7:08:22 1019421 NULL
B AAOLPC7 18 07/19/12 7:06:23 7:06:30 7:08:23 1019421 NULL
B AAOLPC7 18 07/19/12 7:06:24 7:06:31 7:08:24 1019421 NULL
B AAOLPC7 18 07/19/12 8:34:35 8:34:43 8:36:35 1019421 NULL
B AAOLPC7 18 07/19/12 8:34:36 8:34:44 8:36:36 1019421 NULL
B AAOLPC7 18 07/19/12 8:34:37 8:34:45 8:36:37 1019421 NULL
B AAOLPC7 18 07/19/12 9:04:19 9:04:25 9:06:19 1019421 NULL
B AAOLPC7 18 07/19/12 9:04:20 9:04:26 9:06:20 1019421 NULL
B AAOLPC7 18 07/19/12 9:04:21 9:04:27 9:06:21 1019421 NULL
B AAOLPC7 18 07/19/12 10:19:45 10:19:51 10:21:45 1019421 NULL
B AAOLPC7 18 07/19/12 10:19:46 10:19:52 10:21:46 1019421 NULL
B AAOLPC7 18 07/19/12 10:19:47 10:19:53 10:21:47 1019421 NULL


Regards,
The Dark Knight
-Give What is Right, Not What is Left-

Edited by - batcater98 on 07/31/2012 16:39:42
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
48029 Posts

Posted - 07/31/2012 :  16:51:38  Show Profile  Reply with Quote
why are you comparing eventid to trainid?
also you dont require other fields in ORDER BY as you're already comparing on them in where


UPDATE t
SET t .PosDup = 1
FROM AlerterAlarmAudible_Temp t CROSS APPLY
(SELECT TOP 1 End_Time
FROM AlerterAlarmAudible_Temp
WHERE EventID = t .TrainIDEventID AND [Date] = t.[Date] AND UserID = t.UserID AND Exp_Time < t.Exp_Time
ORDER BY EventID, Date, Exp_Time, UserID DESC) t1
WHERE t1.End_Time > t .Exp_time



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

batcater98
Starting Member

USA
20 Posts

Posted - 07/31/2012 :  17:04:53  Show Profile  Reply with Quote
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-
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
48029 Posts

Posted - 07/31/2012 :  17:16:10  Show Profile  Reply with Quote
ok...cool

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

batcater98
Starting Member

USA
20 Posts

Posted - 07/31/2012 :  17:30:12  Show Profile  Reply with Quote
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-
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
48029 Posts

Posted - 07/31/2012 :  17:39:56  Show Profile  Reply with Quote
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 MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.19 seconds. Powered By: Snitz Forums 2000