0

I have tried a lot to figure how to get the count from two tables with respect to master table

I have three tables

enter image description here

Using these table values I need to get this output..

enter image description here

Tried but could get the desired result http://en.wikipedia.org/wiki/Join_(SQL) SQL - LEFT OUTER JOIN and WHERE clause http://forums.devshed.com/oracle-development-96/combination-of-left-outer-join-and-where-clause-383248.html

5
  • tried left outer join, right outer join, inner join, ... Commented Nov 25, 2011 at 8:47
  • I am trying the given solution, please give me a moment to mark the correct answer and vote up.. what make you guys to downgrade? Commented Nov 25, 2011 at 8:58
  • 2
    @AnandMohanAwasthi: There are many users that post a question without showing what they tried. many of them have not tried anything at all. I'm not saying that this is the case with you but if you don't show what you did, we can't be sure (and some will assume you just want others to do the job for you). Commented Nov 25, 2011 at 9:02
  • @ypercube: I was bit busy in trying the provided solution, check my profile you can easily figure that this is my third question . I want to say that before writing or asking question i do lot reseach, anyways I think I have found the solution.. Commented Nov 25, 2011 at 9:09
  • @ypercube: And hats off your solution.. you are genius.. Commented Nov 25, 2011 at 9:09

2 Answers 2

4

You have to first GROUP BY in subqueries, then JOIN to the main table:

SELECT
    a.AttributeId     
  , COALECSE(cntE, 0) AS cntE
  , COALECSE(cntM, 0) AS cntM
FROM
    AttributeMaster AS a
  LEFT JOIN
    ( SELECT
          AttributeId
        , COUNT(*) AS cntE
      FROM 
          EmployeeMaster 
      GROUP BY
          AttributeId
    ) em
      ON  em.AttributeId = a.AttributeId
  LEFT JOIN
    ( SELECT
          AttributeId
        , COUNT(*) AS cntM
      FROM 
          MonthlyDerivedMaster 
      GROUP BY
          AttributeId
    ) mdm
      ON mdm.AttributeId = a.AttributeId
Sign up to request clarification or add additional context in comments.

Comments

0
SELECT AttributeId,
(SELECT COUNT(Eid) FROM EmployeeMaster WHERE AttributeMaster.AttributeId = EmployeeMaster.AttributeId) as master_eid,
(SELECT COUNT(Eid) FROM MonthnlyDerivedMaster WHERE AttributeMaster.AttributeId = MonthnlyDerivedMaster.AttributeId) as monthly_eid
FROM AttributeMaster

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.