问题
In my SQL CE database I have three tables: customer
, list
and customerlist
(a junction table between customer
and list
- as it is a many-to-many relationship).
I am trying to run a query that will display all current lists with also the number of customers who are currently subscribed to that list (counting from customerlist
table).
Here is my current query:
select list.listid, count(customerlist.customerid) as numppl, list.ShortDesc
from list inner join customerlist on list.listid=customerlist.listid
group by list.ShortDesc, list.listid
order by numppl desc
The current structure of this database is:
[Customer] [List] [CustomerList]
CustomerId ListId CustomerListId
Name ShortDesc CustomerId
Other details ListId
This currently returns all the lists who have customers currently assigned to them - but not lists which are empty. Empty lists are hidden.
I would like to modify this query to also display empty lists but I am struggling. My desired output is:
Name numppl
listA 375
listB 45
listC 0
(In the example above, listC is currently not being returned).
Any thoughts on how to also show listC in the query?
回答1:
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
Update
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;
回答2:
You must use LEFT JOIN syntax.
select list.listid, case when count(customerlist.customerid) is null then 0 else count(customerlist.customerid) 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
Or you can use RIGHT JOIN :
select list.listid, case when count(customerlist.customerid) is null then 0 else count(customerlist.customerid) end as numppl, list.ShortDesc
from customerlist right join list on customerlist.listid=list.listid
group by list.ShortDesc, list.listid
order by numppl desc
Use COALESCE :
select list.listid, coalesce(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
来源:https://stackoverflow.com/questions/14976931/sql-joining-by-field-that-may-be-empty