Solved

Loop through one table and update data in another table

Posted on 2014-11-24
35
163 Views
Last Modified: 2014-11-27
I have 2 tables relating to tracking a Lorry/Truck trip

1. TRIP- start and end Dates/ID's
2. TRACKING_DATA- This contains all data i.e. start time/position, end time/position, and in-between time/positions


I want to be able to EXEC an SP every 30 seconds to update the TRIP table,  it will look at the TRIP Table WHERE END ID is NULL - and pull the correct ID/DATE from the TRACKINGDATA table

Here is a pic of the trip table:
trip3There are 2 trips which aren't "completed" so i need to look up the "Ignition Off" Event in the TrackingData Table (ReportId = 8)


Here is the TrackingData Table
It show Ignition on and Ignition off
Trip4The selected Row is the Ignition Off for the bottom row of first image.

Essentially, i just need to "complete" the trip in the top table with the information from selected row of 2nd image

So I guess  i would need something like this

Select * from tblTrip_Master
Where nEndEventInst = NULL
{
-- Loop through each result and get the correct Ignition Off ID from TrackingData table
-- HOWEVER....IF the Ignition Off ID has already been used, do NOT re use it to "complete" a trip)!
-- The vSequenceID's aren't reliable enough (they dont always come in order), so we have order by dGPSDateTime

--on current loop:
Update current Trip row
where dGpsDateTime > dStartTime
And DeviceID = currentRowDeviceID
AND vReportID = 8
AND vSequenceID(This is the row we are picking and think has the correct END ID) does not already exist in tblTrip_Master.nEndEventInst - i.e. it hasn't been used before
}




The above is what i need initally
However, its worth mentioning, that due to trucks going out of Cellular signal and other factors, sometimes the "correct" igntion off ID may not be in the TrackingData table when this SP runs..... but a later ignition off might be.... this would cause the trip table to go out of sync as it would marry the wrong ignition on and off
 ...however, this is out of my control....so i was thinking of having some sort of CLEANING AGENT which grabs the start and end GPSdate ... and looks up if any other ignition offs are inbetween these for the deviceID.... and then tries to sort it out.....however, i'd like to get the first part working first, and can tackle this bit later


Any help appreciated!


I've uploaded a .bak file of the stripped down database with these 2 table....get it here: Download bak file
the correct table name/columns are here
SELECT ipkTripID
      ,dStartTime
      ,dEndTime
      ,nStartEventInst
      ,nEndEventInst
      ,vpkDeviceID as DeviceID
  FROM tblTrip_Master

SELECT ipkCommanTrackingID
      ,dGPSDateTime
      ,vSequenceID
      ,vReportID
	  ,vEventName
      ,vpkDeviceID as DeviceId
  FROM tblCommonTrackingData

Open in new window




I've tried doing something similar to what's explained here, but its not working so i'm taking a new approach and hoping this will work.....
0
Comment
Question by:websss
  • 17
  • 12
  • 6
35 Comments
 
LVL 9

Assisted Solution

by:teebon
teebon earned 100 total points
ID: 40463941
Hi websss,

This should do it:

DECLARE @ipkTripID INT
DECLARE @dStartTime DATETIME
DECLARE @deviceID INT
DECLARE @dEndTime DATETIME
DECLARE @nEndEventInst INT
 
DECLARE db_cursor CURSOR FOR  
SELECT ipkTripID
      ,dStartTime
      ,vpkDeviceID 
FROM tblTrip_Master
WHERE nEndEventInst IS NULL

OPEN db_cursor  
FETCH NEXT FROM db_cursor INTO @ipkTripID, @dStartTime, @deviceID
WHILE @@FETCH_STATUS = 0  
BEGIN  

SELECT TOP 1 @dEndTime = dGPSDateTime, @nEndEventInst = vSequenceID
  FROM tblCommonTrackingData WHERE 
  vpkDeviceID = @deviceID AND
  vReportID = 8 AND
  dGpsDateTime >= @dStartTime
  vSequenceID NOT IN 
  (SELECT DISTINCT nEndEventInst FROM tblTrip_Master)


  IF @dEndTime IS NOT NULL THEN
  BEGIN

  UPDATE tblTrip_Master SET dEndTime = @dEndTime, nEndEventInst = @nEndEventInst WHERE ipkTripID = @ipkTripID

  END
    

SET @dEndTime = NULL
SET @nEndEventInst = 0

 FETCH NEXT FROM db_cursor INTO @ipkTripID, @dStartTime, @deviceID
END  

CLOSE db_cursor  
DEALLOCATE db_cursor 

Open in new window


I not yet test it on your sample database. Let me know if you face any problem.
0
 

Author Comment

by:websss
ID: 40463947
Thanks
I'm getting the following error with that
eror
0
 
LVL 9

Expert Comment

by:teebon
ID: 40463953
Updated.

DECLARE @ipkTripID INT
DECLARE @dStartTime DATETIME
DECLARE @deviceID INT
DECLARE @dEndTime DATETIME
DECLARE @nEndEventInst INT
 
DECLARE db_cursor CURSOR FOR  
SELECT ipkTripID
      ,dStartTime
      ,vpkDeviceID 
FROM tblTrip_Master
WHERE nEndEventInst IS NULL

OPEN db_cursor  
FETCH NEXT FROM db_cursor INTO @ipkTripID, @dStartTime, @deviceID
WHILE @@FETCH_STATUS = 0  
BEGIN  

SELECT TOP 1 @dEndTime = dGPSDateTime, @nEndEventInst = vSequenceID
  FROM tblCommonTrackingData WHERE 
  vpkDeviceID = @deviceID AND
  vReportID = 8 AND
  dGpsDateTime >= @dStartTime AND
  vSequenceID NOT IN 
  (SELECT DISTINCT nEndEventInst FROM tblTrip_Master)


  IF @dEndTime IS NOT NULL THEN
  BEGIN

  UPDATE tblTrip_Master SET dEndTime = @dEndTime, nEndEventInst = @nEndEventInst WHERE ipkTripID = @ipkTripID

  END
    

SET @dEndTime = NULL
SET @nEndEventInst = 0

 FETCH NEXT FROM db_cursor INTO @ipkTripID, @dStartTime, @deviceID
END  

CLOSE db_cursor  
DEALLOCATE db_cursor 
                                        

Open in new window

0
 
LVL 12

Expert Comment

by:Koen Van Wielink
ID: 40463976
I'm pretty sure you can do this without a cursor, which would give much better performance. Try this:

Select *
into #openTrips
From	(Select *
		from 	tblTrip_Master
		where	nEndEventInst	is NULL) as trip;
		
update	tblTrip_Master
Set			nEndEventInst = b.vSequenceID
		,	dEndTime = b.dGPSDateTime
From	(Select a.*
		From	(select		Row_Number() over (partition by DeviceId order by dGPSDateTime desc) as RowNum
						,	dGPSDateTime
						,	vSequenceID
						,	DeviceID
				from	tblCommonTrackingData
				where	vReportID = 8) as a
		where	a.RowNum = 1) as b
		, #openTrips ot
Where	tblTrip_Master.DeviceID = b.DeviceID
and		tblTrip_Master.ipkTripID = ot.ipkTripID
and		not exists
		(select 1
		from	tblTrip_Master
		where	nEndEventInst = b.vSequenceID);

Open in new window


It selects all the records with the NULL value you're interested in into a temp table. It then updates the master data by selecting the latest record from the common tracking data table for each device and setting the ID and end time for the ignition off events in the master table. It only does this for records existing in the #openTrips temp table. The not exists clause makes sure that the selected event doesn't already exist in the table.
0
 

Author Comment

by:websss
ID: 40463980
Hi Koen

I adjusted your code slightly to ammend the errors
Select *
into #openTrips
From	(Select *
		from 	tblTrip_Master
		where	nEndEventInst	is NULL) as trip;
		
update	tblTrip_Master
Set			nEndEventInst = b.vSequenceID
		,	dEndTime = b.dGPSDateTime
From	(Select a.*
		From	(select		Row_Number() over (partition by vpkDeviceID order by dGPSDateTime desc) as RowNum
						,	dGPSDateTime
						,	vSequenceID
						,	vpkDeviceID
				from	tblCommonTrackingData
				where	vReportID = 8) as a
		where	a.RowNum = 1) as b
		, #openTrips ot
Where	tblTrip_Master.vpkDeviceID = b.vpkDeviceID
and		tblTrip_Master.ipkTripID = ot.ipkTripID
and		not exists
		(select 1
		from	tblTrip_Master
		where	nEndEventInst = b.vSequenceID);

Open in new window


I get the results

(8 row(s) affected)

(0 row(s) affected)


The trip table does not get updated :(
0
 
LVL 12

Expert Comment

by:Koen Van Wielink
ID: 40463986
That's odd. When I ran a test on my data I got a result. Here's the full test code I used:

Create table #tblTrip_Master
( ipkTripID int 
 ,dStartTime datetime
 ,dEndTime datetime NULL
 ,nStartEventInst int
 ,nEndEventInst int NULL
 ,DeviceID bigint
 );

Create table #tblCommonTrackingData
( ipkCommanTrackingID int
,dGPSDateTime datetime
,vSequenceID int
,vReportID int
,vEventName nvarchar(100)
,DeviceId bigint
 );
 
 insert into #tblTrip_Master
 values		(9276, '2014-11-25 05:49:02.000', '2014-11-15 05:50:12.000', 1767179, 1767193, 352599046955303)
		,	(9271, '2014-11-24 18:02:07.000', NULL, 13853, NULL, 862170018835676)
		
insert into #tblCommonTrackingData
values		(314228, '2014-11-25 06:17:54.000', 13913, 8, 'ingition switched off', 862170018835676)
		,	(313837, '2014-11-24 18:02:07.000', 13853, 7, 'ingition switched on', 862170018835676)
		,	(313258, '2014-11-24 15:36:46.000', 13842, 8, 'ingition switched off', 862170018835676)
		,	(313141, '2014-11-24 13:51:15.000', 13788, 7, 'ingition switched on', 862170018835676)
		
select *
from	#tblTrip_Master

Select *
into #openTrips
From	(Select *
		from 	#tblTrip_Master
		where	nEndEventInst	is NULL) as trip;
		
update	#tblTrip_Master
Set			nEndEventInst = b.vSequenceID
		,	dEndTime = b.dGPSDateTime
From	(Select a.*
		From	(select		Row_Number() over (partition by DeviceId order by dGPSDateTime desc) as RowNum
						,	dGPSDateTime
						,	vSequenceID
						,	DeviceID
				from	#tblCommonTrackingData
				where	vReportID = 8) as a
		where	a.RowNum = 1) as b
		, #openTrips ot
Where	#tblTrip_Master.DeviceID = b.DeviceID
and		#tblTrip_Master.ipkTripID = ot.ipkTripID
and		not exists
		(select 1
		from	#tblTrip_Master
		where	nEndEventInst = b.vSequenceID);
		
select *
from	#tblTrip_Master;
		
drop table #openTrips;
drop table #tblTrip_Master;
drop table #tblCommonTrackingData;

Open in new window


As you can see from the before and after select statements the record is altered.
I was not able to restore your database backup so I couldn't work on your full sample data. Are you using SQL 2005 or 2008?
0
 

Author Comment

by:websss
ID: 40463997
I'm using 2014

I've managed to get it running now
however, it inserted the same END ID on 2 different rows in trip table
see here for before and after
cap4

here is the trackingData table
Capture5.PNG
0
 
LVL 12

Expert Comment

by:Koen Van Wielink
ID: 40464004
Right... That's because we're not processing the data line by line. When the check takes place whether the number is already used once, none of the updates have been written yet, so it will not find the number for each of the 2 trip lines.
But does this happen in your application? Can there be multiple non-completed trips for the same device, and if so, what's the correct way of handling this? In other words, what's the expected outcome?
On a side note, is there a particular reason you're using a stored procedure instead of a trigger on the tblCommonTrackingData table? You could fire the trigger to update the master trip table each time an event with ID 8 is inserted.
Final note, since you're using SQL 2014, better remove the SQL 2005 and SQL 2008 tags from the question.
0
 

Author Comment

by:websss
ID: 40464007
Yes that makes sense

I was actually originally using a trigger, but due to data integrity I can a lot of miss matches with ENDing trips
I found if I wait 30 secs, the data integrity is near 99%
...of course i'll need to modify the code to only process data older than 30 sec

When you ask
"But does this happen in your application? Can there be multiple non-completed trips for the same device,"

Yes, without going into massive detail, there is hardware that does reports this
In most cases not... but when it does happen, it really messes up data integrity, so i have to get it right

....do you think the cursor way is the correct way?
0
 

Author Comment

by:websss
ID: 40464008
Hi Teebon
I've adjusted your code so it works
DECLARE @ipkTripID INT
DECLARE @dStartTime DATETIME
DECLARE @deviceID varchar
DECLARE @dEndTime DATETIME
DECLARE @nEndEventInst INT
 
DECLARE db_cursor CURSOR FOR  
SELECT ipkTripID
      ,dStartTime
      ,vpkDeviceID 
FROM tblTrip_Master
WHERE nEndEventInst IS NULL

OPEN db_cursor  
FETCH NEXT FROM db_cursor INTO @ipkTripID, @dStartTime, @deviceID
WHILE @@FETCH_STATUS = 0  
BEGIN  

SELECT TOP 1 @dEndTime = dGPSDateTime, @nEndEventInst = vSequenceID
  FROM tblCommonTrackingData WHERE 
  vpkDeviceID = @deviceID AND
  vReportID = 8 AND
  dGpsDateTime >= @dStartTime AND
  vSequenceID NOT IN 
  (SELECT DISTINCT nEndEventInst FROM tblTrip_Master)


  IF @dEndTime IS NOT NULL 
  BEGIN

  UPDATE tblTrip_Master SET dEndTime = @dEndTime, nEndEventInst = @nEndEventInst WHERE ipkTripID = @ipkTripID

  END
    

SET @dEndTime = NULL
SET @nEndEventInst = 0

 FETCH NEXT FROM db_cursor INTO @ipkTripID, @dStartTime, @deviceID
END  

CLOSE db_cursor  
DEALLOCATE db_cursor 

Open in new window


I get
Command(s) completed successfully.

However, no records are updated in trip table :(
0
 
LVL 9

Expert Comment

by:teebon
ID: 40464012
Hi websss,

Have you restore your database before the testing?
I will try restore your sample database and check.
0
 

Author Comment

by:websss
ID: 40464019
Hi Teebon
Yes I have just tried that and it worked with SQL server 2014

You will need to set the dEndTime and nEndEventInst rows to NULL as they are all completed at present... run this
update  tblTrip_Master
set 
dEndTime= NULL
,nEndEventInst= NULL
where ipkTripId > 9404

Open in new window


then run the adapter code i posted...you will see they remain NULL
0
 
LVL 12

Expert Comment

by:Koen Van Wielink
ID: 40464024
Ok, based on your last comment on my query you might not have a choice but to go for a cursor. Teebon's example could work (haven't tested it but not doubting his skills), but for completeness here's how I'd tackle it:

Create table #tblTrip_Master
( ipkTripID int 
 ,dStartTime datetime
 ,dEndTime datetime NULL
 ,nStartEventInst int
 ,nEndEventInst int NULL
 ,DeviceID bigint
 );

Create table #tblCommonTrackingData
( ipkCommanTrackingID int
,dGPSDateTime datetime
,vSequenceID int
,vReportID int
,vEventName nvarchar(100)
,DeviceId bigint
 );
 
 insert into #tblTrip_Master
 values		(9276, '2014-11-25 05:49:02.000', '2014-11-15 05:50:12.000', 1767179, 1767193, 352599046955303)
		,	(9271, '2014-11-24 18:02:07.000', NULL, 13853, NULL, 862170018835676)
		
insert into #tblCommonTrackingData
values		(314228, '2014-11-25 06:17:54.000', 13913, 8, 'ingition switched off', 862170018835676)
		,	(313837, '2014-11-24 18:02:07.000', 13853, 7, 'ingition switched on', 862170018835676)
		,	(313258, '2014-11-24 15:36:46.000', 13842, 8, 'ingition switched off', 862170018835676)
		,	(313141, '2014-11-24 13:51:15.000', 13788, 7, 'ingition switched on', 862170018835676)
		
select *
from	#tblTrip_Master

Declare		@Trip_id int
		,	@Device_ID bigint
		,	@endEventId int
		,	@endEventDate datetime
		,	@counter int
		,	@error smallint

set nocount on
Print 'Beginning transaction.'
Begin transaction T1

Select	@counter = 0

	Declare T_trip	cursor for
		
			Select		ipkTripID
					,	DeviceID
			From	#tblTrip_Master
			where	nEndEventInst is NULL
		
	Open T_Trip
	Fetch	Next From T_Trip
	Into
			@Trip_Id
		,	@Device_ID
	While (@@FETCH_STATUS <> -1)
	Begin
	
	Select @counter = @counter + 1
	
	Select		@endEventDate =	a.dGPSDateTime
			,	@endEventId = a.vSequenceID
	From	(select		Row_Number() over (partition by DeviceId order by dGPSDateTime desc) as RowNum
					,	dGPSDateTime
					,	vSequenceID
			from	#tblCommonTrackingData
			where	vReportID = 8
			and		DeviceId = @Device_ID) as a
	where	a.RowNum = 1
		
	update	#tblTrip_Master
	Set			nEndEventInst = @endEventId
			,	dEndTime =@endEventDate
	where	DeviceID = @Device_ID
	and		ipkTripID = @Trip_id
	and	not exists
		(select 1
		from	#tblTrip_Master
		where	nEndEventInst = @endEventId)
		
	If	@@ERROR <> 0
	Begin
	Set @error = 1
	Break
	End

	Fetch	Next From T_Trip
	Into
			@Trip_Id
		,	@Device_ID
	End
	
	If	@error <> 0
	Begin
	
	Print	'An error occurred. Begin rolling back transaction.'
	Rollback Transaction T1
	
	Print	'Transaction rolled back.'
	Print	'Number of records rolled back: ' + LTRIM(Str(@counter,8,0))
	
	Close T_trip
	Deallocate T_Trip
	End
	
	Else
	Begin
	
	Print	'Begin committing transaction.'
	Commit transaction T1
	Print	'Transaction committed.'
	Print	'Number of records updated: ' + LTRIM(Str(@counter,8,0))
	
	Close T_Trip
	Deallocate T_trip
	
	End
	
Set nocount off
	
select *
from	#tblTrip_Master;
		
drop table #tblTrip_Master;
drop table #tblCommonTrackingData;

Open in new window


I've left my test data in there for you to compare. I've also added some error handling code in the cursor in case something does go wrong.
Let me know how that goes.
0
 
LVL 9

Expert Comment

by:teebon
ID: 40464125
Hi websss,

I'm unable to restore your database backup. Can you try to execute the following (I added printing of message to check):

DECLARE @ipkTripID INT
DECLARE @dStartTime DATETIME
DECLARE @deviceID varchar
DECLARE @dEndTime DATETIME
DECLARE @nEndEventInst INT
 
DECLARE db_cursor CURSOR FOR  
SELECT ipkTripID
      ,dStartTime
      ,vpkDeviceID 
FROM tblTrip_Master
WHERE nEndEventInst IS NULL

OPEN db_cursor  
FETCH NEXT FROM db_cursor INTO @ipkTripID, @dStartTime, @deviceID
WHILE @@FETCH_STATUS = 0  
BEGIN  

SELECT TOP 1 @dEndTime = dGPSDateTime, @nEndEventInst = vSequenceID
  FROM tblCommonTrackingData WHERE 
  vpkDeviceID = @deviceID AND
  vReportID = 8 AND
  dGpsDateTime >= @dStartTime AND
  vSequenceID NOT IN 
  (SELECT DISTINCT nEndEventInst FROM tblTrip_Master)

PRINT @ipkTripID

  IF @dEndTime IS NOT NULL 
  BEGIN

PRINT @dEndTime 
PRINT @nEndEventInst

  UPDATE tblTrip_Master SET dEndTime = @dEndTime, nEndEventInst = @nEndEventInst WHERE ipkTripID = @ipkTripID


  END
    

SET @dEndTime = NULL
SET @nEndEventInst = 0

 FETCH NEXT FROM db_cursor INTO @ipkTripID, @dStartTime, @deviceID
END  

CLOSE db_cursor  
DEALLOCATE db_cursor 

Open in new window

0
 
LVL 12

Expert Comment

by:Koen Van Wielink
ID: 40464161
Teebon, I think you're missing an order by statement in your cursor query. You're selecting the top 1 record, but it's not guaranteed that it's the last record created. You have to order the records by the GPS datetime descending to make sure only the latest record is returned.
0
 

Author Comment

by:websss
ID: 40464286
Hi Teebon

I tried your statement on the whole table after setting all trip records as Not Complete
I had the following results:
157
158
159
206
223
299
308
326
337
355
230
250
259
263
265
272
285
294
307
342
343
201
202
207
212
215
219
224
232
267
274
291
306
352
185
188
190
194
237
236
324
325
348
149
150
155
203
211
213
221
225
227
239
253
266
329
332
210
256
28
63
66
67
74
75
76
77
80
139
142
143
252
323
327
333
1374
1415
1417
1421
1431
1446
1450
1458
1460
1461
1463
1464
1465
1466
1468
1515
1618
1620
2101
2103
2106
2173
2177
2178
2180
2342
2576
2635
2641
2644
2651
2708
2711
2713
2717
2727
2729
2793
2796
2798
2800
2802
2807
2809
2811
2812
2815
2817
2820
2823
2825
2827
2830
2831
2833
2837
2839
2840
2841
2843
2856
2863
2878
2900
2917
2921
2926
2929
2971
2973
3095
3102
3113
3125
3142
3184
3190
3191
3198
3199
3200
3201
3228
3284
3291
3296
3301
3322
3380
3381
3392
3394
3431
3434
3438
3439
3544
3552
3572
3616
3620
3645
3648
3653
3688
3690
3697
3698
3700
3725
3728
3733
3734
3735
3741
3745
3757
3758
3759
3762
3763
3768
3772
3775
3779
3825
3828
3834
3835
3855
3859
3860
3866
3867
3869
3871
3873
3876
3878
3881
3883
3885
3892
3895
3903
3907
3918
3919
3927
3928
4903
4906
4908
4935
4938
4942
4960
4964
4977
4982
4989
4990
4992
5014
5016
5019
5022
5027
5030
5039
5043
5046
5048
5052
5056
5061
5062
5065
5067
5068
5070
5072
5076
5077
5078
5081
5084
5085
5089
5090
6087
6088
6141
6146
6147
6150
6151
6178
6180
6187
6194
6217
6222
6223
6232
6237
6313
6318
6319
6323
6324
6325
6327
6330
6331
6333
6335
6357
6358
6374
6375
6382
6415
6821
6823
6824
6826
6833
6834
6835
6836
6838
6841
6842
6845
6848
6853
6861
6993
6998
7001
7002
7010
7012
7132
7134
7158
7165
7168
7320
7325
7329
7359
7365
7478
7631
7637
7663
7676
7866
7918
8369
8817
8955
8971
8980
8991
9013
9015
9017
9061
9064
9065
9072
9183
9210
9247
9260
6943
6944
6945
6946
6948
6949
6950
6951
6952
6953
6954
6955
6975
6977
7004
7006
7014
7018
7039
7040
7047
7058
7059
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7136
7143
7144
7145
7147
7148
7149
7150
7151
7152
7153
7154
7155
7161
7163
7164
7171
7184
7185
7186
7187
7188
7190
7191
7192
7197
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7244
7245
7246
7247
7248
7249
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7280
7281
7282
7283
7284
7285
7286
7287
7288
7298
7318
7331
7352
7353
7360
7361
7362
7363
7367
7368
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7482
7483
7484
7485
7486
7487
7488
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7639
7640
7641
7644
7645
7646
7647
7648
7649
7651
7659
7660
7661
7664
7665
7666
7667
7669
7670
7671
7672
7673
7674
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7994
7997
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8023
8024
8025
8026
8027
8028
8029
8030
8047
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8107
8108
8109
8112
8113
8114
8115
8116
8117
8118
8119
8121
8126
8127
8128
8130
8158
8163
8164
8167
8168
8169
8170
8171
8172
8173
8174
8175
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8238
8239
8240
8241
8242
8245
8365
8366
8367
8368
8415
8416
8434
8436
8437
8438
8439
8440
8441
8442
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8465
8466
8467
8469
8470
8503
8519
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8696
8697
8698
8699
8700
8701
8702
8704
8705
8706
8707
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8743
8744
8745
8752
8753
8754
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8779
8780
8781
8783
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8910
8911
8912
8913
8914
8915
8916
8917
8918
8931
8932
8962
8963
8964
8965
8966
8967
8968
8969
8970
9031
9032
9035
9036
9037
9038
9039
9040
9041
9042
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9097
9101
9102
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9182
9185
9186
9189
9190
9191
9192
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9213
9214
9215
9216
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9276
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9292
9293
9294
9295
9296
9297
9298
9299
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
9386
9387
9388
9389
9390
9391
9392
9393
9394
9395
9396
9397
9398
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9416
9417
9418
9420
9424
9425
9426
9427
9428
9429
9431
9432
9433
9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
9445
9446
9447
9448
9449
9450
9451
9452
9453
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6779
6784
6794
6797
6820
6837
6844
6857
6868
6887
6897
6899
6902
6905
6907
6912
6914
6916
6919
6922
6924
6927
6931
6936
6957
6959
6965
6968
6987
6991
7017
7025
7031
7032
7037
7042
7043
7049
7051
7054
7138
7142
7156
7162
7174
7183
7199
7206
7207
7278
7289
7293
7296
7300
7304
7305
7306
7307
7308
7309
7312
7313
7319
7333
7334
7335
7336
7337
7338
7340
7342
7343
7344
7345
7346
7347
7348
7349
7480
7489
7633
7653
7655
7658
7662
7680
7755
7861
7991
8049
8053
8081
8097
8100
8102
8105
8110
8125
8160
8243
8244
8246
8370
8418
8431
8432
8447
8450
8452
8472
8504
8619
8621
8703
8721
8751
8782
8785
8787
8790
8878
8879
8957
8959
8974
8985
8986
8987
9002
9005
9007
9008
9020
9043
9070
9098
9187
9217
9219
9241
9244
9274
9421
9430
9454
30
64
65
68
69
70
71
72
73
118
119
140
141
251
255
334
335
336
340
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1416
1418
1422
1432
1447
1448
1449
1452
1453
1454
1455
1456
1457
1459
1467
1516
1619
1621
2102
2104
2105
2174
2176
2179
2181
2182
2183
2317
2318
2343
2403
2407
2409
2563
2565
2570
2574
2575
2634
2636
2637
2638
2643
2645
2652
2709
2712
2714
2718
2728
2730
2794
2795
2799
2801
2803
2808
2810
2813
2814
2816
2818
2821
2824
2826
2828
2829
2832
2834
2836
2838
2842
2844
2857
2860
2862
2879
2899
2918
2922
2927
2930
2972
2974
3094
3103
3114
3126
3143
3144
3185
3192
3193
3194
3195
3196
3197
3229
3285
3290
3295
3302
3321
3323
3378
3379
3382
3383
3391
3395
3432
3433
3435
3436
3545
3551
3573
3617
3621
3644
3649
3654
3689
3691
3696
3699
3701
3726
3727
3731
3732
3736
3742
3746
3756
3760
3761
3764
3765
3769
3773
3774
3780
3824
3829
3833
3836
3854
3858
3861
3865
3868
3870
3872
3874
3875
3877
3882
3884
3886
3893
3894
3896
3902
3906
3917
3920
3926
3929
4902
4904
4907
4936
4939
4943
4961
4965
4976
4983
4988
4991
4993
5015
5017
5020
5021
5028
5031
5035
5038
5042
5047
5049
5051
5057
5060
5063
5064
5066
5069
5071
5073
5074
5075
5079
5080
5082
5083
5086
5087
5088
6089
6090
6140
6148
6149
6152
6153
6154
6177
6179
6188
6193
6218
6221
6224
6233
6235
6314
6317
6320
6321
6322
6326
6328
6329
6332
6334
6336
6359
6360
6373
6376
6381
6414
6822
6825
6827
6828
6839
6840
6843
6846
6847
6849
6850
6851
6852
6854
6862
6992
6999
7000
7003
7009
7011
7131
7135
7159
7166
7169
7321
7326
7328
7358
7364
7366
7477
7632
7638
7642
7668
7675
7683
7865
7917
8388
8818
8956
8972
8981
8992
9012
9014
9018
9060
9063
9066
9071
9181
9209
9246
9261
177
186
189
193
214
226
257
318
319
341
182
183
196
216
217
240
241
242
328
330
339
350
161
162
163
164
165
166
167
168
169
170
204
296
317
260
264
271
282
290
292
305
320
338
353
234
235
258
268
289
349
233
244
249
262
269
270
273
287
301
321
218
220
243
280
345
354
153
195
231
246
248
322
344
293
351
209
229
346
173
175
181
199
261
156
180
197
208
247
278
279
284
288
191
192
275
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7990
7993
7998
7999
8000
8022
8031
8032
8033
8034
8035
8036
8037
8038
8039
8041
8043
8044
8045
8046
8048
8050
8051
8052
8054
8055
8056
8057
8059
8060
8061
8062
8063
8064
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8162
8176
8177
8204
8205
8206
8208
8209
8210
8211
8213
8214
8215
8217
8221
8222
8223
8225
8226
8227
8228
8229
8230
8234
8235
8236
8237
8268
8269
8270
8271
8272
8273
8274
8275
8277
8278
8279
8280
8281
8282
8283
8284
8285
8287
8288
8289
8290
8291
8292
8293
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8319
8320
8321
8328
8329
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8371
8372
8375
8379
8380
8383
8384
8385
8386
8387
8389
8391
8392
8393
8394
8396
8397
8398
8399
8401
8406
8411
8414
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8443
8444
8445
8446
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8505
8506
8511
8512
8514
8515
8517
8520
8521
8616
8617
8618
8623
8624
8625
8695
8708
8709
8710
8711
8715
8750
9094
9095
9172
9173
9174
9175
9176
9177
9178
9179
9266
9267
9268
9269
9270
9273
9275
9300
9412
9413
9414
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8276
8286
8294
8306
8307
8318
8322
8323
8324
8325
8326
8327
8330
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8468
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8746
8747
8748
8749
8919
8920
8921
8922
8923
8924
8925
8926
8927
8929
8930
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
9021
9023
9085
9086
9087
9088
9089
9090
9248
9249
9250
9253
9254
9255
9256
9257
9258
9259
9262
9263
9264
9265
6803
6804
6805
6806
6807
6808
6809
6810
6832
6855
6860
6870
6872
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6890
6892
6894
6906
6910
6917
6920
6928
6929
6930
6933
6934
6935
6938
6939
6940
6947
6956
6961
6962
6963
6969
6972
6974
6976
6978
6980
6981
6982
6983
6989
6994
6996
6997
7015
7021
7022
7023
7027
7029
7034
7035
7045
7046
7053
7057
7130
7146
7157
7167
7173
7176
7177
7181
7189
7194
7196
7200
7201
7202
7203
7205
7291
7292
7297
7301
7303
7310
7316
7322
7324
7327
7330
7350
7351
7354
7355
7356
7357
7369
7635
7643
7654
7678
7920
7922
7924
7925
7989
8058
8094
8095
8104
8120
8123
8129
8157
8231
8247
8250
8251
8252
8331
8390
8402
8413
8435
8448
8449
8463
8464
8502
8513
8516
8596
8615
8659
8713
8716
8979
8993
8995
8996
8997
8999
9010
9011
9016
9019
9022
9024
9025
9026
9027
9029
9030
9055
9058
9062
9074
9076
9077
9078
9079
9080
9081
9082
9083
9084
9091
9092
9093
9096
9100
9167
9170
9171
9184
9193
9220
9221
9222
9223
9224
9225
9226
9227
9228
9245
9251
9252
9271
9291
9415
9423
9455
2325
2328
2339
2340
2348
2350
2351
2353
2359
2360
2364
2384
2385
2386
2399
2400
2402
2404
2405
2406
2416
2417
2425
2427
2428
2464
2465
2468
2469
2470
2471
2485
2486
2487
2488
2507
2512
2517
2519
2521
2524
2526
2529
2551
2552
2553
2581
2582
2585
2586
2629
2630
2631
2640
2649
2660
2669
2672
2673
2699
2700
2707
2710
2716
2722
2723
2724
2725
2726
2735
2737
2738
2744
2746
2771
2772
2775
2777
2778
2779
2780
2781
2782
2783
2784
2785
2791
2792
2797
2804
2805
2819
2822
2835
2845
2846
2864
2868
2896
2902
2903
2907
2910
2913
2915
2931
2933
2961
2963
2967
2968
2969
2970
2991
2993
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3085
3111
3112
3130
3132
3134
3136
3137
3147
3148
3151
3152
3171
3202
3210
3211
3215
3236
3241
3243
3269
3270
3271
3324
3328
3332
3335
3342
3343
3373
3407
3409
3422
3426
3427
3448
3457
3473
3478
3504
3518
3521
3522
3523
3524
3525
3526
3530
3532
3533
3538
3542
3554
3555
3557
3558
3562
3563
3564
3565
3567
3570
3574
3575
3579
3585
3588
3590
3593
3594
3601
3614
3618
3619
3627
3628
3629
3630
3631
3632
3635
3640
3641
3642
3643
3646
3647
3650
3658
3659
3660
3662
3663
3665
3666
3667
3669
3670
3672
3673
3674
3679
3680
3681
3682
3683
3684
3685
3686
3687
3692
3693
3694
3695
3704
3705
3706
3707
3708
3709
3710
3711
3719
3720
3721
3722
3723
3724
3738
3740
3749
3750
3754
3771
3776
3777
3778
3781
3782
3783
3784
3788
3795
3796
3802
3803
3804
3805
3806
3807
3810
3811
3812
3814
3819
3822
3823
3831
3837
3838
3839
3840
3842
3843
3844
3845
3846
3849
3850
3852
3853
3856
3857
3862
3863
3880
3887
3888
3899
3901
3904
3905
3909
3910
3911
3912
3913
3914
3915
3921
3923
3924
3930
4909
4910
4911
4914
4916
4919
4920
4921
4922
4923
4924
4925
4927
4929
4931
4932
4937
4940
4941
4945
4946
4947
4948
4949
4950
4956
4957
4958
4959
4962
4966
4967
4968
4969
4970
4971
4973
4974
4975
4978
4979
4980
4981
4984
4985
4986
4999
5003
5004
5005
5007
5008
5009
5012
5013
5037
5040
5041
5044
5058
5059
6092
6094
6096
6097
6098
6100
6101
6102
6104
6105
6106
6107
6108
6110
6111
6112
6114
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6137
6138
6139
6142
6145
6155
6156
6157
6159
6163
6165
6168
6169
6170
6171
6172
6173
6174
6176
6191
6192
6199
6200
6204
6205
6206
6207
6215
6216
6234
6238
6241
6242
6243
6245
6246
6256
6257
6258
6259
6262
6264
6265
6266
6267
6268
6269
6270
6271
6272
6280
6281
6290
6292
6293
6296
5033
5045
5050
5055
6091
6093
6095
6099
6103
6109
6113
6115
6135
6136
6181
6182
6183
6184
6185
6186
6189
6195
6197
6198
6210
6211
6212
6213
6214
6219
6220
6225
6226
6229
6230
6236
6240
6244
6247
6248
6249
6250
6251
6252
6253
6254
6255
6260
6261
6273
6275
6276
6277
6302
6305
6306
6309
6351
6352
6356
6361
6377
6380
6407
6410
6782
6787
6801
6802
6811
6812
6813
6814
6815
6816
6817
6818
6819
6830
6831
6856
6859
6863
6864
6865
6866
6869
6871
6873
6874
6888
6889
6891
6893
6895
6900
6901
6909
6911
6966
6970
6971
6973
6979
6988
7005
7007
7008
7020
7024
7028
7036
7060
7133
7139
7170
7172
7178
7179
7180
7193
7195
7243
7250
7315
7323
7479
7636
7677
7681
7682
7862
7863
7864
7916
7919
7921
7923
7995
8040
8042
8080
8165
8518
8552
8620
8622
8712
8714
8717
8718
8719
8720
8742
8755
8776
8777
8778
8784
8789
8909
8928
8958
8973
9028
9033
9034
9073
9075
9099
9168
9169
9180
9211
9212
9272
9277
6338
6340
6341
6342
6344
6346
6347
6348
6349
6364
6367
6368
6379
6384
6385
6386
6388
6391
6393
6394
6395
6396
6397
6401
6402
6403
6679
6736
6771
6778
6788
6789
6790
6791
6793
87
90
91
92
93
120
122
123
124
125
127
136
198
238
245
297
302
303
304
309
310
311
312
313
314
315
316
331
347
1321
1322
1325
1327
1328
1330
1331
1332
1333
1334
1337
1341
1342
1343
1344
1345
1347
1419
1420
1423
1424
1425
1426
1427
1428
1429
1430
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1451
1472
1476
1477
1517
1520
1521
1524
1525
1567
1568
1569
1575
1576
1577
1578
1579
1580
1584
1823
1824
1827
1832
1842
1869
1870
1914
1916
1920
1963
1964
1965
1966
1974
1975
1976
1977
1978
1981
2023
2026
2027
2028
2029
2037
2093
2094
2095
2096
2097
2098
2099
2100
2116
2121
2123
2162
2163
2164
2165
2166
2167
2190
2191
2196
2199
2268
2269
2270
2271
2272
2273
2274
2275
2278
2279
2280
2281
2320
2323
2330
2331
2332
2333
2334
2335
2336
2337
2338
2341
2346
2347
2387
2408
2410
2420
2472
2513
2556
2557
2558
2559
2562
2566
2567
2568
2569
2571
2577
2632
2646
2653
2654
2657
2659
2668
2671
2674
2675
2703
2706
2719
2720
2733
2773
2774
2776
3096
3097
3098
3099
3100
3101
3104
3105
3106
3107
3108
3109
3115
3116
3117
3118
3119
3120
3121
3122
3124
3173
3175
3177
3182
3186
3187
3188
3203
3204
3205
3206
3207
3208
3209
3212
3213
3214
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3272
3273
3274
3275
3276
3277
3278
3279
3280
3282
3283
3286
3287
3288
3289
3292
3293
3294
3298
3299
3300
3303
3304
3305
3307
3308
3309
3314
3317
3318
3320
3327
3336
3337
3339
3374
3375
3376
3377
3384
3400
3413
3414
3424
3429
3456
3458
3461
3462
3463
3464
3465
3466
3468
3469
3470
3474
3475
3476
3477
3484
3485
3487
3488
3490
3497
3498
3499
3500
3501
3502
3503
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3519
3520
3529
3603
3604
3613
3623
3624
3626
3636
3639
3651
3652
3657
3730
3737
3739
3744
3747
3748
3751
3752
3753
3766
3770
3786
3799
3800
3801
3808
3809
3813
3815
3821
3826
3830
3832
3841
3847
3864
3879
3889
3890
3897
3900
4915
4928
4930
4944
4951
4953
4955
4963
4972
4987
4996
4997
4998
5000
5001
5006
5011
5023
5024
5029
5036
5053
5054
6143
6144
6158
6160
6161
6162
6164
6166
6167
6175
6190
6196
6201
6202
6203
6208
6209
6227
6228
6231
6239
6263
6274
6279
6284
6289
6297
6299
6301
6312
6315
6316
6337
6350
6355
6362
6363
6365
6371
6383
6404
6405
6408
6412
6628
6780
6785
6786
6795
6798
6799
6829
6858
6867
6886
6896
6898
6903
6904
6908
6913
6915
6918
6921
6923
6925
6926
6932
6937
6958
6960
6964
6967
6984
6985
6986
6990
6995
7013
7016
7019
7026
7030
7033
7038
7041
7044
7048
7050
7052
7055
7056
7137
7140
7141
7160
7175
7182
7198
7204
7208
7279
7290
7294
7295
7299
7302
7311
7314
7317
7332
7339
7341
7481
7634
7650
7652
7656
7657
7679
7992
7996
8065
8082
8096
8098
8099
8101
8103
8106
8111
8122
8124
8159
8161
8166
8207
8212
8216
8218
8219
8220
8224
8232
8233
8248
8249
8373
8374
8376
8377
8378
8381
8382
8395
8400
8403
8404
8405
8407
8408
8409
8410
8412
8417
8430
8433
8451
8471
8507
8508
8509
8510
8786
8788
8960
8961
8975
8976
8977
8978
8982
8983
8984
8988
8989
8990
8994
8998
9000
9001
9003
9004
9006
9009
9056
9057
9059
9067
9068
9069
9103
9188
9208
9218
9242
9243
9419
9422
31
32
78
82
83
84
85
86
132
133
2184
2185
2187
2188
2229
2235
2276
2277
2319
2322
2356
2365
2397
2398
2401
2422
2436
2437
2463
2466
2467
2473
2474
2476
2477
2478
2480
2505
2514
2554
2555
2560
2633
2665
2666
2677
2678
2679
2704
2705
2715
2721
2790
2806
2859
2861
2871
2872
2877
2897
2898
2901
2957
2958
2966
2975
2982
2992
3006
3026
3027
3042
3043
3044
3045
3046
3047
3048
3092
3093
3110
3178
3179
3180
3181
3183
3189
3281
3297
3306
3310
3319
3389
3396
3398
3399
3405
3425
3428
3430
3437
3440
3441
3444
3446
3447
3449
3450
3452
3453
3454
3467
3602
3615
3622
3625
3633
3634
3637
3638
3655
3656
3661
3664
3668
3671
3675
3676
3677
3678
3702
3703
3712
3713
3714
3715
3716
3717
3718
3729
3743
3755
3767
3785
3787
3789
3790
3791
3792
3793
3794
3797
3798
3816
3817
3818
3820
3827
3848
3851
3891
3898
3908
3916
3922
3925
4905
4912
4913
4917
4918
4926
4933
4934
4952
4954
4994
4995
5002
5010
5018
5025
5026
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
33
34
35
36
37
38
39
40
41
42
43
44
45
46
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
79
81
88
89
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
121
126
128
129
130
131
134
135
137
138
174
184
254
276
281
283
286
295
298
300
1323
1324
1326
1329
1335
1336
1338
1339
1340
1346
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1414
1462
1469
1470
1471
1473
1474
1475
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1518
1519
1522
1523
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1570
1571
1572
1573
1574
1581
1582
1583
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1825
1826
1828
1829
1830
1831
1833
1834
1835
1836
1837
1838
1839
1840
1841
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1915
1917
1918
1919
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1967
1968
1969
1970
1971
1972
1973
1979
1980
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2024
2025
2030
2031
2032
2033
2034
2035
2036
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2107
2108
2109
2110
2111
2112
2113
2114
2115
2117
2118
2119
2120
2122
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2168
2169
2170
2171
2172
2175
2186
2189
2192
2193
2194
2195
2197
2198
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2230
2231
2232
2233
2234
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2324
2326
2327
2329
2344
2345
2349
2352
2354
2355
2357
2358
2361
2362
2363
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2388
2389
2390
2391
2392
2393
2394
2395
2396
2411
2412
2413
2414
2415
2418
2419
2421
2423
2424
2426
2429
2430
2431
2432
2433
2434
2435
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2475
2479
2481
2482
2483
2484
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2506
2508
2509
2510
2511
2515
2516
2518
2520
2522
2523
2525
2527
2528
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2561
2564
2572
2573
2578
2579
2580
2583
2584
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2639
2642
2647
2648
2650
2655
2656
2658
2661
2662
2663
2664
2667
2670
2676
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2701
2702
2731
2732
2734
2736
2739
2740
2741
2742
2743
2745
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2786
2787
2788
2789
2847
2848
2849
2850
2851
2852
2853
2854
2855
2858
2865
2866
2867
2869
2870
2873
2874
2875
2876
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2904
2905
2906
2908
2909
2911
2912
2914
2916
2919
2920
2923
2924
2925
2928
2932
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2959
2960
2962
2964
2965
2976
2977
2978
2979
2980
2981
2983
2984
2985
2986
2987
2988
2989
2990
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3038
3039
3040
3041
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3086
3087
3088
3089
3090
3091
3123
3127
3128
3129
3131
3133
3135
3138
3139
3140
3141
3145
3146
3149
3150
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3172
3174
3176
3226
3227
3230
3231
3232
3233
3234
3235
3237
3238
3239
3240
3242
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3311
3312
3313
3315
3316
3325
3326
3329
3330
3331
3333
3334
3338
3340
3341
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3385
3386
3387
3388
3390
3393
3397
3401
3402
3403
3404
3406
3408
3410
3411
3412
3415
3416
3417
3418
3419
3420
3421
3423
3442
3443
3445
3451
3455
3459
3460
3471
3472
3479
3480
3481
3482
3483
3486
3489
3491
3492
3493
3494
3495
3496
3527
3528
3531
3534
3535
3536
3537
3539
3540
3541
3543
3546
3547
3548
3549
3550
3553
3556
3559
3560
3561
3566
3568
3569
3571
3576
3577
3578
3580
3581
3582
3583
3584
3586
3587
3589
3591
3592
3595
3596
3597
3598
3599
3600
3605
3606
3607
3608
3609
3610
3611
3612

Open in new window


however, now selecting the TOP 1 from trip table has taken 2 minutes without returning any results ?!?
0
 

Author Comment

by:websss
ID: 40464293
i'm getting the following errro

TITLE: Microsoft SQL Server Management Studio
------------------------------

Lock request time out period exceeded. (Microsoft SQL Server, Error: 1222)

For help, click: http://go.microsoft.com/fwlink?ProdName=Microsoft%20SQL%20Server&ProdVer=12.00.2000&EvtSrc=MSSQLServer&EvtID=1222&LinkId=20476

------------------------------
BUTTONS:

OK
------------------------------
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

Author Comment

by:websss
ID: 40464295
...ok, maybe the query need some work

googled it and used this code
DBCC opentran()

kill 58

Open in new window


seems to respond now..
0
 

Author Comment

by:websss
ID: 40464323
Right, so i ran both versions of the Cursor code
However, its not updating any of the records
What should i try next?

Koen.... your code does update a few records.... but very few compared to the amount i'm trying to update
0
 
LVL 12

Expert Comment

by:Koen Van Wielink
ID: 40464361
Can you post some more sample data, preferably in Excel so that i can import it? I'm sure we're almost there, but iNeed some more data to see where this goes wrong.
0
 

Author Comment

by:websss
ID: 40464416
I've uploaded the 2 full tables in CSV and excel

EDIT... amended the files to match your columns
TrackingData.csv
TrackingData.xlsx
TripData.xlsx
TripData.csv
0
 
LVL 12

Expert Comment

by:Koen Van Wielink
ID: 40464456
Hi Websss,

Ok, after seeing this dataset I understand why only a few records are being updated. Only the max record for each deviceId is processed. I see from your dataset now that this is not what you intended to happen.
But now I have another question. How do you know which ignition_off event is the "CORRECT" event to match with any ignition_on event? I cannot find any common field between the 2 tables other than the deviceID. Also, the starttime for the first 50 or so records falls in between the start and end time of the very first record, so it's also not chronological.
Please explain how we can know from the 75 or so ignition_off events which one belongs to which tripId?
0
 

Author Comment

by:websss
ID: 40464512
:)
sure, sorry i forgot to explain this

Basically its GPSDateTime

so if you have a start trip at 6pm, we need to filter by:
Where vReportId = 8 and (the nearest GPSDateTime to dStartTime)

so i guess you would:
select top(1) dGPSDateTime
Where   vReportId = 8
And GPSDateTime =>dStartTime

this should get you the next actual Ignition off
We need the = just in case some one flicks on/off with ignition key in the same second (It happens!)
0
 
LVL 12

Accepted Solution

by:
Koen Van Wielink earned 400 total points
ID: 40464592
Ok, think I finally have it sorted. Try this, the data seems to look ok on my side after the update:

Declare		@Trip_id int
		,	@Device_ID bigint
		,	@starttime	datetime
		,	@endEventId int
		,	@endEventDate datetime
		,	@counter int
		,	@error smallint;

set nocount on;
Print 'Beginning transaction.';
Begin transaction T1

Select	@counter = 0;

	Declare T_trip	cursor for
		
			Select		ipkTripID
					,	dStartTime
					,	vpkDeviceID
			From	tblTrip_Master
			where	nEndEventInst is NULL
		
	Open T_Trip
	Fetch	Next From T_Trip
	Into
			@Trip_Id
		,	@starttime
		,	@Device_ID
	While (@@FETCH_STATUS <> -1)
	Begin
	
	Select @counter = @counter + 1
	
	Select		@endEventDate =	a.dGPSDateTime
			,	@endEventId = a.vSequenceID
	From	(select		top 1
						dGPSDateTime
					,	vSequenceID
			from	tblCommonTrackingData
			where	vReportID = 8
			and		vpkDeviceId = @Device_ID
			and		dGPSDateTime >= @starttime
			order by dGPSDateTime) as a
	
	--print	'Trip: ' + str(@Trip_Id,8,0) + ' ,StartTime: ' + cast(@starttime as nvarchar) + ' , EndTime: ' + cast(@endEventDate as nvarchar) + ' ,Device: ' + str(@Device_ID,20,0)
		
	update	tblTrip_Master
	Set			nEndEventInst = @endEventId
			,	dEndTime =@endEventDate
	where	vpkDeviceID = @Device_ID
	and		ipkTripID = @Trip_id
	and	not exists
		(select 1
		from	tblTrip_Master
		where	nEndEventInst = @endEventId)
		
	If	@@ERROR <> 0
	Begin
	Set @error = 1
	Break
	End

	Fetch	Next From T_Trip
	Into
			@Trip_Id
		,	@starttime
		,	@Device_ID
	End
	
	If	@error <> 0
	Begin
	
	Print	'An error occurred. Begin rolling back transaction.'
	Rollback Transaction T1
	
	Print	'Transaction rolled back.'
	Print	'Number of records rolled back: ' + LTRIM(Str(@counter,8,0))
	
	Close T_trip
	Deallocate T_Trip
	End
	
	Else
	Begin
	
	Print	'Begin committing transaction.'
	Commit transaction T1
	Print	'Transaction committed.'
	Print	'Number of records updated: ' + LTRIM(Str(@counter,8,0))
	
	Close T_Trip
	Deallocate T_trip
	
	End;
	
Set nocount off;

Open in new window

0
 

Author Comment

by:websss
ID: 40465277
yess, this works!

i've set it to run for data in the last 30 mins
however it takes 2 mins 40 to run on just 293 records

here is what im using
/****** Object:  StoredProcedure [dbo].[sp_Trip_UpdateIgnitionOff]    Script Date: 25/11/2014 22:40:51 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[sp_Trip_UpdateIgnitionOff] 
AS
BEGIN
Declare		@Trip_id int
		,	@Device_ID bigint
		,	@starttime	datetime
		,	@endEventId int
		,	@endEventDate datetime
		,	@counter int
		,	@error smallint;

set nocount on;
Print 'Beginning transaction.';
Begin transaction T1

Select	@counter = 0;

	Declare T_trip	cursor for
		
			Select		ipkTripID
					,	dStartTime
					,	vpkDeviceID
			From	tblTrip_Master
			where	nEndEventInst is NULL
			AND dStartTime < DATEADD(SECOND, -30, SYSDATETIMEOFFSET())
			and dStartTime >  DATEADD(DAY, -1, SYSDATETIMEOFFSET())


	Open T_Trip
	Fetch	Next From T_Trip
	Into
			@Trip_Id
		,	@starttime
		,	@Device_ID
	While (@@FETCH_STATUS <> -1)
	Begin
	
	Select @counter = @counter + 1
	
	Select		@endEventDate =	a.dGPSDateTime
			,	@endEventId = a.vSequenceID
	From	(select		top 1
						dGPSDateTime
					,	vSequenceID
			from	tblCommonTrackingData
			where	vReportID = 8
			and		vpkDeviceId = @Device_ID
			and		dGPSDateTime >= @starttime
			AND     dGPSDateTime < DATEADD(SECOND, -30, SYSDATETIMEOFFSET())
			order by dGPSDateTime) as a
	
	--print	'Trip: ' + str(@Trip_Id,8,0) + ' ,StartTime: ' + cast(@starttime as nvarchar) + ' , EndTime: ' + cast(@endEventDate as nvarchar) + ' ,Device: ' + str(@Device_ID,20,0)
		
	update	tblTrip_Master
	Set			nEndEventInst = @endEventId
			,	dEndTime =@endEventDate
	where	vpkDeviceID = @Device_ID
	and		ipkTripID = @Trip_id
	and	not exists
		(select 1
		from	tblTrip_Master
		where	nEndEventInst = @endEventId)
		
	If	@@ERROR <> 0
	Begin
	Set @error = 1
	Break
	End

	Fetch	Next From T_Trip
	Into
			@Trip_Id
		,	@starttime
		,	@Device_ID
	End
	
	If	@error <> 0
	Begin
	
	Print	'An error occurred. Begin rolling back transaction.'
	Rollback Transaction T1
	
	Print	'Transaction rolled back.'
	Print	'Number of records rolled back: ' + LTRIM(Str(@counter,8,0))
	
	Close T_trip
	Deallocate T_Trip
	End
	
	Else
	Begin
	
	Print	'Begin committing transaction.'
	Commit transaction T1
	Print	'Transaction committed.'
	Print	'Number of records updated: ' + LTRIM(Str(@counter,8,0))
	
	Close T_Trip
	Deallocate T_trip
	
	End;
	
Set nocount off;
END

Open in new window


does it look as optimised as possible?
0
 
LVL 9

Expert Comment

by:teebon
ID: 40465935
Hi websss,

May I know if you own the design of this database?

If you do, I would suggest to add one bit flag in tblCommonTrackingData, mark this flag true once it's been used. This could save the checking of the trip table each time for duplicates.
0
 
LVL 12

Expert Comment

by:Koen Van Wielink
ID: 40466200
When I ran the update on the test data you sent me, it ran in less than a second. That was to update 66 records. So if you need multiple minutes to run that for 239 records, something's fishy.
I think the issue is with adding the -30 seconds to the cursor. Have you tried running it without that statement, just to compare?
Why don't you use a scheduled job that runs the procedure as it was, every 30 seconds? Why add the criteria to the select statement in the cursor?
By the way, I agree with Teebon that if you have some control over the database design that some modifications might be in order. The flag he mentions is one useful addition. But you should also see if there's a better way to match up the records between the 2 tables. Was this an in-house developed tool you're using?
0
 

Author Comment

by:websss
ID: 40466271
Yes it is an in-house design which was done by a previous dev, and now i've taken over

so you think if i add a new column to the trackingData Table that says its processed, i can by pass the lookup
and	not exists
		(select 1
		from	tblTrip_Master
		where	nEndEventInst = @endEventId)

Open in new window


sounds reasonable..
Is there anyway to analyse the SP to see which bits are taking longest?

I've adjust the SP to just check for data in the last 1 days and its now taking 4 secs to process 20 records....still seems quite slow for just 20 records
0
 

Author Comment

by:websss
ID: 40466312
I ran the execution plan on SQL server, i've got it from 23 secs to 10 secs with indexes and casting
however, still a long time

.... This is a good stop gap for now...at least the app is working!

Koen, your original code was VERY fast... because we cant use duplicate END ID's i'm not sure if that method is possible ... even if we adjust tracking data table to contain the flag?
0
 
LVL 12

Expert Comment

by:Koen Van Wielink
ID: 40466488
Hi Websss,

Not sure that the original query is possible for the reasons identified earlier. Because it's a bulk insert there's a chance of duplication. It would be if it was clear from the start which off event belongs to which on event. Then you don't have to make things so complicated. For example, if the engine on and engine off event share the same trip ID, all you have to do is join on that. But because there's no unique ID linking both together things get complex.
0
 

Author Comment

by:websss
ID: 40466510
shame!

I figured out that I have a trigger running on the UPDATE of EndEventInstId which works out average speed, max speed and distance

I disabled the trigger and the query ran in a quarter of the time
Would i gain any benefit by moving it to the SP?

ALTER TRIGGER [dbo].[tblTrip_Master__TRG_UPD]
ON [dbo].[tblTrip_Master]
AFTER UPDATE
AS
SET NOCOUNT ON;

			declare @SecondsInHour float = 3600
			declare @startDistance float = 0
			declare @endDistance float = 0

SELECT TOP(1) @endDistance = tm.vOdometer 
FROM tblCommonTrackingData tm, inserted i 
WHERE tm.vSequenceID = i.nEndEventInst and tm.vpkDeviceID = i.vpkDeviceID 
order by tm.ipkCommanTrackingID desc

if @endDistance = 0
SELECT TOP(1) @endDistance = tm.vOdometer 
FROM tblCommonTrackingData tm, inserted i 
WHERE tm.vSequenceID < (i.nEndEventInst) and tm.vpkDeviceID = i.vpkDeviceID and vReportID = 124 and vOdometer > 0
order by tm.ipkCommanTrackingID desc

	
SELECT TOP(1) @startDistance = tm.vOdometer 
FROM tblCommonTrackingData tm, inserted i 
WHERE tm.vSequenceID = i.nStartEventInst  and tm.vpkDeviceID = i.vpkDeviceID 
order by tm.ipkCommanTrackingID desc

if @startDistance = 0
SELECT TOP(1) @startDistance = tm.vOdometer 
FROM tblCommonTrackingData tm, inserted i 
WHERE tm.vSequenceID > (i.nStartEventInst) and tm.vpkDeviceID = i.vpkDeviceID  and vReportID = 124 and vOdometer > 0
order by tm.ipkCommanTrackingID asc


UPDATE tm
SET
	iDistance = (@endDistance - @startDistance) 
	,iAverageSpeed = CASE WHEN DATEDIFF(second, i.dStartTime, i.dEndTime) > 0
		THEN (@endDistance - @startDistance) / (convert(float, DATEDIFF(second, i.dStartTime, i.dEndTime)) / @SecondsInHour) 
			ELSE 999 
			END
	,iMaxSpeed  = (SELECT TOP(1) vVehicleSpeed FROM tblCommonTrackingData WHERE vSequenceID >= i.nStartEventInst and  vSequenceID <= i.nEndEventInst and vpkDeviceID = i.vpkDeviceID order by vVehicleSpeed desc)	
FROM tblTrip_Master tm
INNER JOIN inserted i ON
    i.ipkTripID = tm.ipkTripID --AND i.key_col2 = tm.key_col2
WHERE
    i.dEndTime IS NOT NULL AND 
    i.nEndEventInst IS NOT NULL AND
    i.dStartTime >= DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()) - 13, 0)

Open in new window

0
 
LVL 12

Expert Comment

by:Koen Van Wielink
ID: 40466514
That would explain the slow performance. The trigger goes off for each update you apply probably.
You could give it a shot to add the details into the SP, but I'm not so sure it would make things that much faster. Don't know until you try though.
What might help is to run the update of the EndEventInstId first for all records, and after that do a bulk update on the speed and distance. That way you get rid of the row by row processing.
Sounds to me the system itself could do with a proper overhaul :p.
0
 

Author Closing Comment

by:websss
ID: 40470002
thanks both for your invaluable help!
0
 
LVL 9

Expert Comment

by:teebon
ID: 40470005
You're most welcome :)
0
 
LVL 12

Expert Comment

by:Koen Van Wielink
ID: 40470043
Glad we could help. Feel free to post a new question if you want to incorporate that trigger into the procedure.
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
MS SQL Backup 24 70
SQL Server 2012 Row Selection 2 30
Row-Level Security 2 20
Log Backup 2 13
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now