In my SQL CE database I have three tables: customer
, list
and customerlist
(a junction table between customer
and list<
Use LEFT JOIN
instead with ISNULL
to replace NULL
with 0:
SELECT
list.listid,
ISNULL(count(customerlist.customerid), 0) AS numppl,
list.ShortDesc
FROM list
LEFT JOIN customerlist ON list.listid = customerlist.listid
GROUP BY list.ShortDesc,
list.listid
ORDER BY numppl DESC;
SQL Fiddle Demo
For SQL Server CE, try this:
SELECT
list.listid,
SUM(CASE WHEN customerlist.customerid IS NULL THEN 0 ELSE 1 END) AS numppl,
list.ShortDesc
FROM list
LEFT JOIN customerlist ON list.listid = customerlist.listid
GROUP BY list.ShortDesc,
list.listid
ORDER BY numppl DESC;