Solved

Can you please help with a subquery?

Posted on 2013-01-17
6
422 Views
Last Modified: 2013-01-17
hi, i am trying to do a subquery or corelated query and i cannot get the right output.. can you please help?
i have attached a file with script and the desired output...
Subquery-Data.txt
0
Comment
Question by:Rao_S
  • 3
  • 2
6 Comments
 
LVL 73

Assisted Solution

by:sdstuber
sdstuber earned 500 total points
Comment Utility
SELECT   (SELECT LAST
            FROM staff s
           WHERE s.id = p.leadid)
             LEAD,
         (SELECT LAST
            FROM staff s
           WHERE s.id = c1.cioid)
             cio,
         COUNT(*)
    FROM proj p, cproj c1
   WHERE     p.projid = c1.projid
GROUP BY leadid, cioid
ORDER BY leadid;



I removed the "exists" clause because it doesn't really do anything except add work.  The inner join condition already forces mutual existence
0
 
LVL 73

Accepted Solution

by:
sdstuber earned 500 total points
Comment Utility
alternates using joins instead of scalar subqueries


SELECT LEAD, cio, COUNT
  FROM (SELECT   p.leadid, sp.LAST LEAD, sc.LAST cio, COUNT(*) COUNT
            FROM proj p, cproj c, staff sp, staff sc
           WHERE p.projid = c.projid AND sp.id(+) = p.leadid AND sc.id(+) = c.cioid
        GROUP BY p.leadid, sp.LAST, sc.LAST
        ORDER BY p.leadid);

SELECT LEAD, cio, COUNT
  FROM (SELECT   p.leadid, sp.LAST LEAD, sc.LAST cio, COUNT(*) COUNT
            FROM proj p
                 INNER JOIN cproj c ON p.projid = c.projid
                 INNER JOIN staff sp ON sp.id = p.leadid
                 LEFT OUTER JOIN staff sc ON sc.id = c.cioid
        GROUP BY p.leadid, sp.LAST, sc.LAST
        ORDER BY p.leadid);
0
 

Author Comment

by:Rao_S
Comment Utility
great!!!! thank you so much...it works!
0
 
LVL 73

Expert Comment

by:sdstuber
Comment Utility
glad I could help.

If you don't need anything else, please remember to close the question
0
 

Author Closing Comment

by:Rao_S
Comment Utility
thank you...!!!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and the…
Configuring and using Oracle Database Gateway for ODBC Introduction First, a brief summary of what a Database Gateway is.  A Gateway is a set of driver agents and configurations that allow an Oracle database to communicate with other platforms…
This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now