Query to display output horizontally

后端 未结 3 1758
野性不改
野性不改 2021-01-19 15:10

I need to display a query output in a horizontal manner. I have some example data

create table TestTable (id number, name varchar2(10))

insert into TestTabl         


        
3条回答
  •  离开以前
    2021-01-19 15:19

    Maybe it will help you:

    select 'id', LISTAGG(id, ' ') WITHIN GROUP (ORDER BY name)      
    from testtable
    union 
    select 'name', LISTAGG(name, ' ') WITHIN GROUP (ORDER BY name)
    from testtable
    

    EDIT:

    or with pivot:

    create table TestTable2 (id varchar2(30), name varchar2(10));
    
    insert into TestTable2 values ('id', 'name');
    
    
    insert into TestTable2
    select cast(id as varchar2(30)) as id , name
    from testtable
    
      select *
        from testtable2
        pivot (  max(name)
                 for id in ('id',1,2,3,4) 
    )
    

提交回复
热议问题