Solved

Can you please help with a subquery?

Posted on 2013-01-17
6
425 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 74

Assisted Solution

by:sdstuber
sdstuber earned 500 total points
ID: 38787165
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 74

Accepted Solution

by:
sdstuber earned 500 total points
ID: 38787198
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
ID: 38787214
great!!!! thank you so much...it works!
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 38787267
glad I could help.

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

Author Closing Comment

by:Rao_S
ID: 38787636
thank you...!!!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…

792 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