N methods of Oracle row-column Conversion

Source: Internet
Author: User
Document directory
  • Oracle SQL-sorting ting one row of several columns into one column of several rows
  • Display Oracle SQL output rows on one single line
Oracle SQL-sorting ting one row of several columns into one column of several rows

Oracle tips by Burleson Consulting

There are always ways to use Oracle to keep column data for display on a single row:

0-download SQL into Excel spreadsheet External table

Excel spreadsheets are a great way to submit and analyze ORACLE data, and tools like Excel-DB provide a fast API for downloading ORACLE data into spreadsheets. using Excel uses tables with Oracle Data is a fast, easy way to use Oracle Business Intelligence without buying expensive OLAP solutions (Hyperion, Oracle BI suite ). here is an example.

1-write a PL/SQL function

You can write a PL/SQL function to display multiple rows values on a single line. martin Chadderton has written a PL/SQL function called "strled" that you can define to display multiple SQL rows on one single line.

2-Use the sys_connect_by_path Operator

This article by Younes Naguib describes how to display multiple values from a single column in a single output row. in his example, he displays multiple values of the last name column on a single row. note his use ofSys_connect_by_pathAndOverOperators:

Select
Deptno,
Substr (sys_connect_by_path (lname, ','), 2) name_list
From
(
Select
Lname,
Deptno,
Count (*) over (partition by deptno) CNT,
Row_number () over (partition by deptno order by lname) seq
From
Igribun. EMP
Where
Deptno is not null)
Where
SEQ = CNT
Start
SEQ = 1
Connect by prior
SEQ + 1 = seq
And prior
Deptno = deptno;
Deptno name_list
1 komers, mokrel, stenko
2 Hung, Tong
3 Hamer
4 Mansur

3-cross join

Matt contributed this handy SQL techniques to have one row of several columns into a single column with several row, using the Oracle cross join syntax. matt notes that the cross join "has other uses in conjunction with a where clause to create triangular result sets for Rolling totals etc (though thanks to Analytic Functions those things are very nice and easy) ".

Select
ITE,
Case
When ite = 'item1' then Item1
When ite = 'item2' then item2
When ite = 'item3' then item3
End as Val
From
(
Select
Ter. ite,
Item1,
Item2,
Item3
From
Sometable
Cross join
(
Select 'item1' as ite from dual
Union all
Select 'item2' as ite from dual
Union all
Select 'item3' as ite from dual
) Setter
)

4-Oracle analytic lag-over Function

Analytic functions have a pronounced performance improvement since they avoid an expensive self-join and only make one full-Table scan to get the results. this site shows an example of using the Oracle Lag Function to display multiple rows on a single column:

Matt contributed this handy SQL techniques to have one row of several columns into a single column with several row, using the Oracle cross join syntax. matt notes that the cross join "has other uses in conjunction with a where clause to create triangular result sets for Rolling totals etc (though thanks to Analytic Functions those things are very nice and easy) ".

Also see how to display Oracle SQL output rows on one single line.

Select
ITE,
Case
When ite = 'item1' then Item1
When ite = 'item2' then item2
When ite = 'item3' then item3
End as Val
From
(
Select
Ter. ite,
Item1,
Item2,
Item3
From
Sometable
Cross join
(
Select 'item1' as ite from dual
Union all
Select 'item2' as ite from dual
Union all
Select 'item3' as ite from dual
) Setter

Appendix:

Display Oracle SQL output rows on one single line

Oracle tips by Burleson Consulting

Question:How to I get my SQL output rows to display on one line? Select ename from EMP;
Smith Jones Baker
I want the SQL output on one line, like this: Select ename from EMP;
Smith, Jones, Baker how do I get multiple rows onto one line of output?
Answer:I have complete notes and samples of displaying multiple columns on a single row. you can also use the uninitialized ented SQL wm_concat function. you can displaying multiple columns per row with sys_connect_by_path, Or you can write a PL/SQL function to display multiple rows values on a single line.
Also look at using Oracle analytics (the lag and over functions) to display data in a single row of output.
Martin Chadderton has written a PL/SQL function called "strled" that you can define to display multiple SQL rows on one single line. once defined, you embed the function within your SQL to display your output on a single line:

Select stragg (ename) from EMP;
Smith, Jones, Baker

Sorting SQL output on a single line

Note that sorting is tricky, and even though the output displays on a single line, the "Order by" does not sort the rows on the one line:

Select stragg (ename) from EMP order by ename;
Smith, Jones, Baker

The in-line view ensures that the SQL output appears sorted on one line of output:

Select stragg (ename)
From
(Select stragg (ename) from EMP order by ename );
Baker, Jones, Smith

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.