-1
Good morning Everyone, I have a select column named "ROW" that starts a numerical sequence (1,2,3...[which sometimes can be more than 100]) when all columns that are Partition are equal, and when different the sequence goes back to 1 and will be restarted from it when the columns match again.
Goal:
Cause the ROW column to start counting only when the difference in the "DATHOR" column is greater than 00:20.
In other words, do not leave a stop (equal latitu and longit) less than 20 minutes start a sequence.
Currently:
╔═══════╦════════════════════════════╦═════════════════════╦════════════════════════╦═══════════╗
║PLACA ║ DATHOR ║ LATITU ║ LONGIT ║ ROW ║
╠═══════╬════════════════════════════╬═════════════════════╬════════════════════════╬═══════════╣
║AAA1111║ 2021-02-10 04:50:00 ║ -27,0960066666667 ║ -52,6380843333333 ║ 1 ║
║AAA1111║ 2021-02-10 04:32:00 ║ -27,0960966666667 ║ -52,6380933333333 ║ 1 ║
║AAA1111║ 2021-02-10 04:02:00 ║ -27,0961566666667 ║ -52,6380950000000 ║ 2 ║
║AAA1111║ 2021-02-10 03:39:00 ║ -27,0961566666667 ║ -52,6380950000000 ║ 1 ║
║AAA1111║ 2021-02-10 03:35:00 ║ -27,0961466666667 ║ -52,6381016666667 ║ 2 ║
║AAA1111║ 2021-02-10 03:25:00 ║ -27,0961466666667 ║ -52,6381016666667 ║ 1 ║
║AAA1111║ 2021-02-10 03:23:00 ║ -27,0963583333333 ║ -52,6381350000000 ║ 1 ║
╚═══════╩════════════════════════════╩═════════════════════╩════════════════════════╩═══════════╝
Ex of the objective:
╔═══════╦════════════════════════════╦═════════════════════╦════════════════════╦═══════╗
║PLACA..║..........DATHOR............║.......LATITU........║.........LONGIT.....║..ROW..║
╠═══════╬════════════════════════════╬═════════════════════╬════════════════════╬═══════╣
║AAA1111║.....2021-02-10 04:50:00....║..-27,0960066666667..║ -52,6380843333333..║...1...║
║AAA1111║.....2021-02-10 04:32:00....║..-27,0960966666667..║ -52,6380933333333..║...1...║
║AAA1111║.....2021-02-10 04:02:00....║..-27,0961566666667..║ -52,6380950000000..║...2...║
║AAA1111║.....2021-02-10 03:39:00....║..-27,0961566666667..║ -52,6380950000000 ║...1...║
║AAA1111║.....2021-02-10 03:35:00....║..-27,0961466666667..║ -52,6381016666667 ║...1...║
║AAA1111║.....2021-02-10 03:25:00....║..-27,0961466666667..║ -52,6381016666667 ║...1...║
║AAA1111║.....2021-02-10 03:23:00....║..-27,0963583333333..║ -52,6381350000000 ║...1...║
╚═══════╩════════════════════════════╩═════════════════════╩════════════════════╩═══════╝
SQL:
SELECT
v.PLACA,
v.DATHOR,
v.LATITU,
v.LONGIT,
ROW_NUMBER()
OVER (PARTITION BY
v.LATITU
,v.LONGIT
,v.PLACA
ORDER BY
v.PLACA
,v.DATHOR ASC
)
as ROW
FROM posicoes v
Obs: I could not edit correctly, if someone can help me in editing also for better understanding I am grateful.
Gave the error below:Message 530, Level 16, Status 1, Line 13 The statement terminated. The Maximum recursion 100 has been exhausted before statement Completion.
– Carlos Gabriel
Try adding at the end:
OPTION (MAXRECURSION 0);
– imex
It worked out! Thank you.
– Carlos Gabriel
I’m glad the answer helped. Kindly assess the possibility of accepting it as an answer.
– imex