• + 0 comments

    I ran into this issue - Wrote a pretty much identical query to his and ran into this problem. Here was my original code:

    SELECT      C.Company_Code,
                            C.Founder,
                            COUNT(DISTINCT L.Lead_Manager_Code),
                            COUNT(DISTINCT S.Senior_Manager_Code),
                            COUNT(DISTINCT M.Manager_Code),
                            COUNT(DISTINCT E.Employee_Code)            
    FROM        Company C,
                            Lead_Manager L,
                            Senior_Manager S,
                            Manager M,
                            Employee E
    WHERE       C.Company_Code = L.Company_Code
            AND     L.Lead_Manager_Code = S.Lead_Manager_Code
            AND     S.Senior_Manager_Code = M.Senior_Manager_Code
            AND     M.Manager_Code = E.Manager_Code
    GROUP BY    C.Company_Code
    ORDER BY    C.Company_Code;
    

    As you can see all the selected columns are aggregate functions except for the first two. Since we are grouping by Company Code, it reduces the entries for that code to one row so it can match aggregates like COUNT(). But if we're not grouping by Founder, we will still end up with multiple entries since we've only selected Founder.

    Let me know if that makes sense. It's just my beginner-level understanding of it.