Querying sequences of rows in SQL

前端 未结 3 919
被撕碎了的回忆
被撕碎了的回忆 2021-02-14 23:24

Suppose I am storing events associated with users in a table as follows (with dt standing in for the timestamp of the event):



        
3条回答
  •  不思量自难忘°
    2021-02-15 00:15

    For Oracle (version 11g R2):

    By chance if you are using Oracle DB 11g R2, take look at listagg. The below code should work, but I haven't tested. The point is: you can use listagg.

    SQL> select user,
      2         listagg( event, '' ) 
      3         within group (order by dt) events
      4     from users
      5    group by user
      6    order by dt
      7   /
    
         USER   EVENTS
    ---------  --------------------
    1          ADBCB
    2          BBAAC
    

    In prior versions you can do with CONNECT BY clause. More details on listagg.

提交回复
热议问题