Call now: 252-767-6166  
Oracle Training Oracle Support Development Oracle Apps

Free Oracle Tips

HTML Text

 Home
 E-mail Us
 Oracle Articles


 Oracle Training
 Oracle News

 Oracle Forum
 Class Catalog


 Our Staff
 Our Prices
 Help Wanted!

 Remote DBA
 Oracle Tuning
 Emergency 911
 RAC Support
 Apps Support
 Analysis
 Design
 Implementation
 Oracle Support


 SQL Tuning
 Security

 UNIX
 Oracle UNIX
 Linux
 Oracle Linux
 Monitoring
 Remote help

 Remote plans
 Remote
services
 Oracle C++
 Oracle Java
 Apache
 JDeveloper
 App Server

 Applications
 Oracle Forms
 Oracle Portal
 11i Upgrades
 SQL Server
 Oracle Concepts
 HTML-DB Tips
 Software Help

 Remote Help  
 Development  

 Implementation


 Financials Training
 Oracle 11i
 Oracle Apps 11i
 Oracle Workflow
 Oracle AR 11i Class
 Oracle AP 11i class
 Oracle GL 11i class
 Oracle HR 11i class
 Oracle FA 11i class
 11i Project Mgt
 11i procurement
 11i collections


 Oracle Posters
 Oracle Books

 Oracle Tuning Book
 Oracle RAC Book
 Oracle Security
 Easy Oracle Books
 Oracle Scripts
 SQL Server DBA
 SQL Design Patterns
 Ion
 Excel-DB   


 BC Oracle News


 Rednecks!
 Dress code
 Arabian Stallion

 Burleson Arabians
 Guide Horses
 Don Burleson Blog
 Golf & Travel


 Privacy Policy
 

 

 

 
 

Oracle SQL: displaying multiple column values per row

Oracle Tips by Burleson Consulting


Oracle SQL does not have a direct mechanism for allowing multiple values from the same column to be displayed in the same row of output.  SQL is designed not to disallow repeating values on a single line of output, but Oracle has several tricks for allowing repeating groups of same-column values on a single line of output.  For example, assume that we need to display all degrees for an employee on a single row of SQL*Plus output:

Jones:  BA Art History, MBA in Finance, PhD in business
Smith:  BS in computer science
Baker:  BBA in Business administration, MS in computer science

Approaches to display many columns on one line of output

Let's take a look at how we can consolidate multiple values into a single-row display.

 - Download Oracle data into Excel Pivot tables
 - Write a PL/SQL function
 - Use the SYS_CONNECT_BY_PATH operator
 - Use Oracle Cross Join

Let's look at each method to consolidate data from a table onto a single row.

Download data into Excel spreadsheet pivot table

Excel spreadsheets are a great way to pivot and analyze Oracle data, and tools like Excel-DB provide a fast API for downloading Oracle data into spreadsheets. Using Excel pivot 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.

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 "stragg" 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

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 of the sys_connect_by_path and over operators:

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 with
   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

 

Use a Cross join

Matt contributed this handy SQL techniques to pivot 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
    pivoter.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
    )pivoter
)

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:

SELECT    
   ename,   
   hiredate,sal,LAG (sal, 1, 0)   
   OVER (ORDER BY hiredate) AS PrevSal
FROM    
   emp
WHERE
    job = 'CLERK';

My Notes:

For more SQL tips and tricks, see the book "Easy Oracle SQL".  Also see these related notes on SQL output display consolidation:

If you like Oracle tuning, see the book "Oracle Tuning: The Definitive Reference", with 950 pages of tuning tips and scripts. 

You can buy it direct from the publisher for 30%-off and get instant access to the code depot of Oracle tuning scripts.


 

 

 

Oracle performance tuning software 
 
 

Oracle performance tuning book

 

 
 
 
 

Search oracle
 
Oracle performance Tuning 10g reference poster
 
 
 
Oracle training in Linux commands
 
Oracle training Excel
 
Oracle training & performance tuning books
 

 

Note: This Oracle documentation was created as a support and Oracle training reference for use by our DBA performance tuning consulting professionals.  Feel free to ask questions on our Oracle forum.

Verify experience! Anyone considering using the services of an Oracle support expert should independently investigate their credentials and experience, and not rely on advertisements and self-proclaimed expertise. All legitimate Oracle experts publish their Oracle qualifications.

Errata?  Oracle technology is changing and we strive to update our BC Oracle support information.  If you find an error or have a suggestion for improving our content, we would appreciate your feedback.  Just  e-mail:  and include the URL for the page.
 
 


Burleson Consulting

The Oracle of Database Support

&

Oracle Performance Tuning


 

Copyright © 1996 -  2009 by Burleson Enterprises, Inc. All rights reserved.

Oracle © is the registered trademark of Oracle Corporation.