DBAsupport.com Forums - Powered by vBulletin
Results 1 to 6 of 6

Thread: Count query where count(*) is 0 ???

  1. #1
    Join Date
    Mar 2004
    Posts
    55

    Question Count query where count(*) is 0 ???

    I have basically two tables as below and wish to do a count


    EMPLOYEE

    Occupation_Code Name
    -----------------------------
    EL John
    PL Dick
    BD Charlse
    BD Fanny


    OCCUPATION
    Occupation_Code Occupation_Name

    EL Electrician
    Pl Plumber
    BD Builder
    CL Cleaner
    CK Cook


    Now i need to do a count of how many people I have and group them by Occupation_Code for every single Occupation code that exists in OCCUPATION.

    Now if i do a simple count on EMPLOYYE then i will only get a result for occupation_code that exists in this table

    ie Occupation_Code Count(*)
    EL 1
    PL 1
    BD 2


    What i want is also the CL and CK occupations code and i want them to read 0

    So i want this result

    Occupation_Code Count(*)
    EL 1
    PL 1
    BD 2
    CK 0
    CL 0



    Any tips on how to get this query out?

    Thankyou

  2. #2
    Join Date
    Nov 2002
    Location
    Geneva Switzerland
    Posts
    3,142
    Try something along these lines (untested):
    Code:
    select o.Occupation_Code, sum(e.sums)
    from   occupation o,
    (select Occupation_Code, count(*) sums
     from   employee
     group by Occupation_Code) e
    where  o.Occupation_Code = e.Occupation_Code(+)
    group by o.Occupation_Code

  3. #3
    Join Date
    Feb 2004
    Posts
    77
    Try this also.

    select o.occupation_code, count(*)
    from occupation o, employee e
    where o.occupation_code = e.occupation_code(+)
    group by o.occupation_code

  4. #4
    Join Date
    Nov 2002
    Location
    Geneva Switzerland
    Posts
    3,142
    Originally posted by oracle_faq
    select o.occupation_code, count(*)
    from occupation o, employee e
    where o.occupation_code = e.occupation_code(+)
    group by o.occupation_code
    I think that will give a count of 1 for Occupations with no Employees.
    count(employee_name) should work if there are no rows where employee_name is NULL.

    P.S. better: count(e.occupation_code) !

  5. #5
    Join Date
    Feb 2004
    Posts
    77
    Agree with DaPi....didn't run the query to see the results in the forst place.

    or something like that

    select o.occupation_code,
    sum(decode(e.occupation_code, null, 0, 1))....

  6. #6
    Join Date
    Aug 2002
    Location
    Colorado Springs
    Posts
    5,253
    Or ...
    Code:
    select o.occupation_code, count(e.occupation_code)
    from occupation o, employee e
    where o.occupation_code = e.occupation_code(+)
    group by o.occupation_code
    David Aldridge,
    "The Oracle Sponge"

    Senior Manager, Business Intelligence Development
    XM Satellite Radio
    Washington, DC

    Oracle ACE

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Click Here to Expand Forum to Full Width