select count where displaying 0 when no record meets the where condition

  • Hi,

    I guess this question must have been asked before but I simply can't find it. So, I have a query that looks like :

    select id, count(*)

    from mytable

    where param = 1

    group by id

    It gives something like :

    id count

    ---------

    1 10

    2 25

    As it should, this query counts only the existing records. But I would like in my result to see, for every id where the condition is NOT met, to have a line saying that 0 record meets the condition. So something like :

    id count

    ---------

    1 10

    2 25

    3 0

    I guess I have to use not exists or not in somewhere, but I can't figure out how.

    Thanks for your help which is greatly appreciated.

  • Just move your condition into the count. As in...

    Select id, count(case when param=1 then 1 else null end) as myCount

    from mytable

    Group by ID

    ----------------------------------------------------------------------------------
    Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?

  • Just great!

    Thank you

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply