I have following SQL:
select
s.Id,
s.SPOSiteId,
s.Url,
COUNT(*) as ItemCountScan,
s.ItemCount as ItemCountList,
SUM(f.CountUniqueRoleAssignments) as SharedWithCount,
SUM(s.MemberCount) as MembersCount,
SUM(s.OwnerCount) as OwnersCount,
s.Created,
s.LastItemUserModifiedDate,
s.Template,
s.TimeZoneId,
s.Language
from sites s
LEFT JOIN Files f ON f.SiteId = s.Id
GROUP BY
s.Id,
s.SPOSiteId,
s.Url,
s.ItemCount,
s.Forvaltning,
s.KoncernEnhed,
s.Enhed,
s.KKOrg,
s.BestiltAf,
s.DataAnsvarlig,
s.DirekteChef,
s.Created,
s.LastItemUserModifiedDate,
s.Template,
s.TimeZoneId,
s.Language,
s.GroupSettings,
s.ExternalAccess,
s.Inserted
As you can see it joins two tables - files and sites. Everything works as expected unless this in the select:
COUNT(*) as ItemCountScan
The column should tell how many files are there on the given site. If the site has files it works as expected. However if the site does not have any files then it returns 1 (because the site count it self in the group by). Is there any way to make this work both for sites with no files and sites with files?