Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Enterprise
Expertise: Advanced
Aug 25, 2000

SubQueries in the FROM Clause of SELECT

When a SubQuery is used in a FROM clause instead of a Table name, Oracle executes the subquery and then uses the resulting rows as a view in the FROM clause. It's a very useful method for performing reports with various types of counts.

Consider the following classic query, which counts the number of awards accepted or rejected by employees. It does an outer join to take care of employees who might have only accepted or only rejected awards. This will give one row per employee in an emp_status table:

 select a.id, nvl(accepted, 0)  TotAccepted, 
nvl(rejected, 0) TotRejected, fname from emp_status a, (select count(*) accepted, id,
code , status from emp_status where code = 'AWARD' and status = 'A' group by id, code, status ) b , (select count(*) rejected, id,
code , status from emp_status where code = 'AWARD' and status = 'R' group by id, code, status) c , (select id, fname from employee) d where a. code = 'AWARD' and a.status in ('R', 'A') and a.id = b.id(+) and a.id = c.id(+) and a.id = d.id group by a.id, accepted, rejected, fname
Trupti Rajparia
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap