Select c.Name, g.Name, Count(u.CityId) From User u Right outer Join Group g ON (g.ID = U.GroupID And u.Joindate Between @startdate and @enddate) Right outer Join City c ON (c.ID = u.CityID) Group by c.Name, g.Name ORDER BY c.Name, g.Name
select city.name as City, group.name as [Group], isnull(subqty.qty, 0) as Qtyfrom dbo.Citycross join dbo.Groupleft outer join (select cityid, groupid, count(*) as Qty from dbo.User where joindate >= @startdate and joindate < @enddate group by cityid, groupid) SubQty on city.cityid = subqty.cityid and group.groupid = subqty.groupidorder by city, group
select cityname as City, groupname as [Groupname], isnull(subqty.qty, 0) as Qtyfrom (select groupid, group.name groupname, city.name Cityname, cityid from dbo.Group CROSS JOIN city) cartesianXleft outer join (select cityid, groupid, count(*) as Qty from dbo.User where joindate >= @startdate and joindate < @enddate group by cityid, groupid) SubQty on cartesianX.cityid = subqty.cityid and cartesianX.groupid = subqty.groupidorder by cityname, groupname