1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

  2. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

dense_rank in sql partition by id and session id but ordered by timestamp

Discussão em 'Outras Linguagens' iniciado por Stack, Abril 20, 2021.

  1. Stack

    Stack Membro Participativo

    I have a table as following:

    User ID Session ID Timestamp
    100 7e938c4437a0 1:30:30
    100 7e938c4437a0 1:30:33
    100 c1fcfd8b1a25 2:40:00
    100 7b5e86d91103 3:20:00
    200 bda6c8743671 2:20:00
    200 bda6c8743671 2:25:00
    200 aac5d66421a0 3:10:00
    200 aac5d66421a0 3:11:00

    I am trying to rank each session_id for by user_id, sequenced(ordered by) timestamp. I want something like the second table.

    I am doing the following but it does not order by timestamp:

    dense_rank() over (partition by user_id order by session_id) as visit_number


    it outputs in wrong order and when I add timestamp in the order by it behaves like a row_number() function.

    Below is what I am really looking for to get as a result:

    User ID Session ID Timestamp Rank
    100 7e938c4437a0 1:30:30 1
    100 7e938c4437a0 1:30:33 1
    100 c1fcfd8b1a25 2:40:00 2
    100 7b5e86d91103 3:20:00 3
    200 bda6c8743671 2:20:00 1
    200 bda6c8743671 2:25:00 1
    200 aac5d66421a0 3:10:00 2
    200 aac5d66421a0 3:11:00 2

    Continue reading...

Compartilhe esta Página