CONDITIONAL ROW NUMBER

Asked

Viewed 68 times

-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.

1 answer

1


Here is a suggested test using a recursive CTE to get the "Row":

WITH
  CTE_RN AS
  (
    SELECT
      PLACA,
      DATHOR,
      LATITU,
      LONGIT,
      ROW_NUMBER() OVER(PARTITION BY PLACA ORDER BY DATHOR) AS RN
    FROM POSICOES
  ),
  
  CTE_REC AS
  (
    SELECT
      PLACA,
      DATHOR,
      LATITU,
      LONGIT,
      RN,
      1 AS ROW,
      DATHOR AS DATINI
    FROM CTE_RN
    WHERE
      RN = 1
      
    UNION ALL
    
    SELECT
      N.PLACA,
      N.DATHOR,
      N.LATITU,
      N.LONGIT,
      N.RN,
      CASE 
        WHEN 
          C.LATITU = N.LATITU AND 
          C.LONGIT = N.LONGIT AND 
          DATEDIFF(MINUTE, C.DATINI, N.DATHOR) > 20
        THEN C.ROW + 1
        ELSE 1
      END,
      CASE 
        WHEN 
          C.LATITU = N.LATITU AND 
          C.LONGIT = N.LONGIT
        THEN C.DATINI
        ELSE N.DATHOR
      END
    FROM CTE_REC AS C
    INNER JOIN CTE_RN AS N
      ON
        N.PLACA = C.PLACA AND 
        N.RN = C.RN + 1
  )

SELECT
  PLACA,
  DATHOR,
  LATITU,
  LONGIT,
  ROW
FROM CTE_REC
ORDER BY 
  PLACA,
  DATHOR DESC

I hope it helps

  • 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.

  • Try adding at the end: OPTION (MAXRECURSION 0);

  • It worked out! Thank you.

  • I’m glad the answer helped. Kindly assess the possibility of accepting it as an answer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.