sql - How do you explicitly show rows which have count(*) equal to 0 -
the query i'm running in db2
select yrb_customer.name, yrb_customer.city, case count(*) when 0 0 else count(*) end #uniclubs yrb_member, yrb_customer yrb_member.cid = yrb_customer.cid , yrb_member.club '%club%' group yrb_customer.name, yrb_customer.city order count(*)
shows me people part of clubs has word 'club' in it, , shows how many such clubs part of (#uniclubs
) along name , city. students not part of such club, still them show have 0 instead of them being hidden what's happening right now. cannot functionality count(*)
. can shed light? can explain further if above not clear enough.
you're going want left join
:
select yrb_customer.name, yrb_customer.city, count(yrb_member.club) clubcount yrb_customer left join yrb_member on yrb_member.cid = yrb_customer.cid , yrb_member.club '%club% group yrb_customer.name, yrb_customer.city order clubcount
also, if tuple (yrb_customer.name
, yrb_customer.city
) unique (or supposed - counting students same name same person?), might better performance out of following:
select yrb_customer.name, yrb_customer.city, coalesce(club.count, 0) yrb_customer left join (select cid, count(*) count yrb_member club '%club% group cid) club on club.cid = yrb_customer.cid order club.count
the reason original results being hidden because in original query, have implicit inner join, of course requires matching rows. implicit-join syntax (comma-separated from
clause) great inner (regular) joins, terrible left-joins, needed. use of implicit-join syntax (and types of related filtering in where
clause) considered deprecated.
Comments
Post a Comment