How to transpose a table in SQLite?

前端 未结 3 1961
抹茶落季
抹茶落季 2021-01-17 18:43

Hello so I have a table as such in SQlite:

   User    |  Group  |   Role    
John Smith |   A     |   admin
John Smith |   B     |   user
Jane Doe   |   A            


        
3条回答
  •  半阙折子戏
    2021-01-17 18:44

    You can use row_number() & do aggregation :

    select User, 
           max(case when seq = 1 then role end) as a,
           max(case when seq = 2 then role end) as b,
           max(case when seq = 3 then role end) as c
    from (select t.*,
                 row_number() over (partition by User order by group) as seq
          from table t
         ) t
    group by User;
    

提交回复
热议问题