Querying sequences of rows in SQL

前端 未结 3 2258
傲寒
傲寒 2021-02-14 23:25

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:17

    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.

提交回复
热议问题