Useful functions in Oracle 11g Part01

No.of Views1483
Bookmarked0 times
Downloads 
Votes0
By  Nimish Garg   On  01 Jul 2010 08:07:08
Tag : Oracle , View and Functions
In this article, i have given four useful function in oracle 11g.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

In this article, i have given example four useful function in oracle

  • Simulate Lead Analytical Function (Manual Lead)
  • PIVOT data (rows to columns)
  • Comma Separated String To Rows 10g Onwards
  • Comma Separated String To Rows 11g

Simulate Lead Analytical Function (Manual Lead)

http://nimishgarg.blogspot.com/2010/06/oracle-simulate-lead-analytical.html

ORIGINAL QUERY

select 
    deptno, empno, ename, 
    lead(ename) over(partition by deptno order by ename) lead_emp 
from scott.emp 
order by deptno, ename

SIMULATED QUERY

select deptno, empno, ename,(select ename from(select ename, deptno, rownum rn from(select empno, ename, deptno from scott.emp order by deptno, ename)) a where a.rn = emp.rn + 1 and a.deptno=emp.deptno
) lead_emp
from (select empno, ename, deptno, rownum rn from (select * from scott.emp order by deptno, ename)) emp
Image Loading

PIVOT data (rows to columns)

http://nimishgarg.blogspot.com/2010/06/oracle-pivot-data-rows-to-columns.html

ON ORACLE 11g

select * from(select 
        sal,deptno,jobfromscott.emp
)pivot
(sum(sal) for deptno in (10,20,30,40))

 BEFORE ORACLE 11g

select*from(select job,sum(decode(deptno,10,sal)) dept10,sum(decode(deptno,20,sal)) dept20,sum(decode(deptno,30,sal)) dept30,sum(decode(deptno,40,sal)) dept40from scott.empgroup by job)order by 1;

 Output 

Image Loading

Comma Separated String To Rows 10g Onwards

http://nimishgarg.blogspot.com/2009/12/oracle-sql-use-comma-seprated-string-to.html

with mytable as(select 'nimish' col,'1,2,3,4,5' txt from dualunionselect 'garg' col,'6,7,8,9' txt from dual
)select     
    mytable.col,   
    regexp_substr ( mytable.txt, '[^,]+', 1, n) as num 
from   
    mytable,(selectlevel nfrom(selectmax ( length (txt) - length (replace (txt, ',')))as max_commasfrom   
                    mytable)connect by level <= 1 + max_commas) ctr
wherectr.n<= 1 + length (txt) - length (replace (txt, ','))order by 
    mytable.col,ctr.n

Output 

Image Loading

Comma Separated String To Rows 11g

http://nimishgarg.blogspot.com/2009/12/oracle-sql-use-comma-seprated-string-to.html

FOR NUMBERS

with mytable as(select 'smith' ename,'1,2,3,4,5' mytext from dualunionselect 'ward' ename,'2,4,6,7' mytext from dual
)select ename,(column_value).getnumberval() 
from mytable,  xmltable(mytext)

Output

Image Loading

 FOR STRINGS USE

with mytable as(select 'smith' ename,'"a","b","c","d","e"' mytext from dualunionselect 'ward' ename,'"b","d","f","g"' mytext from dual
)select ename,(column_value).getstringval() 
from mytable,  xmltable(mytext)

 That's  all, i hope this is help to all you, in next part i will add more functions tips.

 
Sign Up to vote for this article
 
About Author
 
Nimish Garg
Occupation-Software Engineer
Company-Indiamart Intermesh Limited
Member Type-Junior
Location-India
Joined date-04 Jun 2010
Home Page-http://nimishgarg.blogspot.com/
Blog Page-http://nimishgarg.blogspot.com/
Oracle DBA (Performance Tuning and Query Optimization) IndiaMART InterMESH Limited, Noida
 
 
Other popularSectionarticles
Comments
By:KCDate Of Posted:7/1/2011 5:30:59 AM
Order by behavior in Oracle 11g
I want to ask if there is any difference in the behavior of Oracle 11g into the older version like 8i in terms of sorting using order by? Thanks
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top