Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Can you please help with a subquery?

Posted on 2013-01-17
6
Medium Priority
?
429 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 74

Assisted Solution

by:sdstuber
sdstuber earned 2000 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 2000 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

This post first appeared at Oracleinaction  (http://oracleinaction.com/undo-and-redo-in-oracle/)by Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

715 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