3
I have a base with the 3 columns and one that I created.
╔═══════════════════════════╦═════════════════════╦════════════════════════╦═══════════╗
║ DATA ║ LATITU ║ LONGIT ║ ROW ║
╠═══════════════════════════╬═════════════════════╬════════════════════════╬═══════════╣
║ 2021-02-10 10:32:00 ║ -27,0960066666667 ║ -52,6380843333333 ║ 1 ║
║ 2021-02-10 09:28:00 ║ -27,0960966666667 ║ -52,6380933333333 ║ 2 ║
║ 2021-02-10 08:27:00 ║ -27,0960966666667 ║ -52,6380933333333 ║ 1 ║
║ 2021-02-10 07:26:00 ║ -27,0961566666667 ║ -52,6380950000000 ║ 1 ║
║ 2021-02-10 06:24:00 ║ -27,0961466666667 ║ -52,6381016666667 ║ 2 ║
║ 2021-02-10 05:24:00 ║ -27,0961466666667 ║ -52,6381016666667 ║ 1 ║
║ 2021-02-10 03:23:00 ║ -27,0963583333333 ║ -52,6381350000000 ║ 1 ║
╚═══════════════════════════╩═════════════════════╩════════════════════════╩═══════════╝
My goal is to create a column that sums the DATA column in format (hh:mm
) while in motion (the columns LATITU
and LONGIT
are different values), reset when stop and restart the sum when moving again. (Remembering that I summarized the data here, because in the real base the date comes every 5 minutes and the ROW column sometimes arrives until in a sequence that 100).
Example:
TIME
:
01:04
00:00
02:03
01:02
00:00
02:01
00:00 - INICIO
To help created this column called "ROW", in it whenever the Latitude and Longitude is different (this in motion) it results in "1" and when the LAT/LONG is equal (is stopped) starts a sequence (1,2,3,4) until changing the lat/long (return to the movement) where it results again in 1.
SQL
:
SELECT DATA, LATITU, LONGIT, ROW_NUMBER()
OVER (PARTITION BY LATITU,LONGIT ORDER BY DATA ASC) as ROW
FROM TABELA_LATLONG
ORDER BY
DATA DESC
It worked with SUBQUERY, but the TEMPO column brought in a not very user-friendly format, it brought as "aaaa-mm-dd hh:mm:ss:nnn". How can I change to bring only in hh:mm?
– Carlos Gabriel
Will the amount always be less than 24 hours? I think the preference would be to format in the presentation layer, but if it is always less than 24 hours you can change Datetime to Time in the Cast and Dateadd parameter 0 to Cast('00:00' as Time)
– imex
Yes in my case it is not more than 24 hours, with the cast as Voce spoke brought the result with the format "hh:mm:ss:nnnn", but in my case I only need the "hh:mm". How can I change to it? I also tried to use a Convert instead of the cast "CONVERT (varchar,'00:00',108)", but also unsuccessfully
– Carlos Gabriel
I edited the answer to add another suggestion with the formatting in the version with the subquery
– imex
It worked out! Thank you.
– Carlos Gabriel