Documentos de Académico
Documentos de Profesional
Documentos de Cultura
Web www.oraxcel.com
menubar-top-links
* Home
* Products
* Download
* Order
* About us
* Contact
* Forums Get RSS feed from the forum
menubar-top-rechts
Home Help Search Login Register
Linker IT Software › SQL*XL › General Usage and Feature Troubleshooting ‹ Previous topic | Next
topic ›
Pages: 1
Oracle subquery - With syntax (Read 9260 times)
Gerrit-Jan Linker
YaBB Administrator
*****
Posts: 1649
Question:
I am using the Oracle subquery syntax. SQL*XL does not seem to recognise these with statements. Can
you fix that?
Answer:
Yes. I can confirm this is fixed in SQL*XL 4.0.42
The Oracle subquery syntax allows you to assign a name to a query block. You can reference this name
in multiple places in the statement. The syntax is:
[subquery_factoring_clause] select_statement
Example:
A good example of the use of a with statement is when you want to calculate a percentage. Consider the
emp table with columns ename, sal, deptno. An employee has a salary and a department number.
Suppose you need to calculate what the percentage of the salary is that a person gets in a particular
department. You could use the following syntax to first calculate the total salary per department and
then to join that against the emp table to compute the salary percentages:
with total_dept_sal as
( select sum(sal) total_sal
, deptno
from emp
group by deptno
)
select ename
, sal
, emp.deptno
, round(sal*100/total_dept_sal.total_sal,0) sal_percentage
from emp
, total_dept_sal
where emp.deptno = total_dept_sal.deptno
order by emp.deptno
select *
from mytable m
where m.start_date >= mypackage.convert_local_to_gmt(to_date('01-01-2007','dd-mm-yyyy'))
and m.end_date < mypackage.convert_local_to_gmt(to_date('01-01-2008','dd-mm-yyyy'))
with parameters
as
( select mypackage.convert_local_to_gmt(to_date('01-01-2007','dd-mm-yyyy'))
as start_date
, mypackage.convert_local_to_gmt(to_date('01-01-2008', 'dd-mm-yyyy'))
as end_date
)
select *
from mytable m
, parameters p
where m.start_date >= p.start_date
and m.end_date < p.end_date
Back to top
Gerrit-Jan Linker
Linker IT Software
Email WWW Gerrit-Jan Linker IP Logged
Gerrit-Jan Linker
YaBB Administrator
*****
Posts: 1649
Example:
with codes as (select /*+ materialize */ code from code_table)
select *
from mytable m
, codes c
where m.code = c.code
See also:
Pre materialize parts of complex query
http://www.oraxcel.com/cgi-bin/yabb2/YaBB.pl?num=1193345741/0
Back to top
Gerrit-Jan Linker
Linker IT Software
Email WWW Gerrit-Jan Linker IP Logged
Pages: 1
Linker IT Software › SQL*XL › General Usage and Feature Troubleshooting ‹ Previous topic | Next
topic ›