Query to display output horizontally

后端 未结 3 1760
野性不改
野性不改 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) 
    )
    
    0 讨论(0)
  • 2021-01-19 15:28

    To pivot, you should use the pivot clause of the select statement:

    select *
      from testtable
     pivot ( max(name)
             for id in (1,2,3,4)
           )
    

    This is not particularly pretty to do in SQL, so you should consider carefully whether this is what you want to do. I normally use Oracle Base for pivoting examples but there are many out there.

    Here's a little SQL Fiddle to demonstrate.

    0 讨论(0)
  • 2021-01-19 15:37

    PIVOT operator is what you are looking for.

    0 讨论(0)
提交回复
热议问题