For this question, suppose this table structure is:
People: PersonID int PK Name varchar(50) Place int NULL FK -> Places.PlaceID MovedIn datetime Places: PlaceID int PK Name varchar(50)
I want to determine how many people live in each place:
SELECT pla.PlaceID, COUNT(*) FROM Places AS pla LEFT JOIN People as peo ON peo.PlaceID = pla.PlaceID GROUP BY pla.PlaceID
This query will omit places where there are no people living there. Is there any way to do this count 0 instead?
(I am targeting SQL Server 2005, in case this is important)
EDIT: Here is my real (anonymous) request, trying to adapt Steve's solution:
SELECT ft.FooTypeID, COUNT(f.FooID) FROM FooType as ft LEFT OUTER JOIN Foo f ON ft.FooTypeID = f.FooTypeID LEFT JOIN FooConfig fc ON ft.NotificationConfigID = fc.FooConfigID WHERE DateDiff(day, GetDate(), f.Date) > 0 AND DateDiff(day, GetDate(), f.Date) < fc.Days GROUP BY ft.FooTypeID
(Translation between my original example and the following: Foo → People, FooType → Places, FooConfig → Third table, for added pleasure) I can do this work with the Fosco solution, but I would prefer Steve.
source share