Is there a way for me to perform a COUNT on joined tables without having to use the GROUP BY statement? I have to insert this SQL into a legacy application which is parsing this result but can't handle the GROUP BY part.
This is what I have now (works in sql-server, but not in the other app):
SELECT TOP 10 au.pref_language,au.username,am.IsOpenIdAccount,am.facebookid,au.sex,au.firstname,au.middlename,au.lastname,am.email,
c.id AS objectid,c.title AS objecttitle,c.friendlyurl as objecturl,c.objecttype,am.CreateDate,c.description_nl,c.description_en,
COUNT(distinct ap.id) as totalphotos,
COUNT(distinct ar.id) as totalreviews,
COUNT(distinct fm.id) as totalfreemedia
FROM locations c
INNER JOIN aspnet_users au on au.UserId=c.userid
INNER JOIN aspnet_membership am ON am.userid=au.userid
LEFT JOIN location_photos ap on ap.objectid=c.id
LEFT JOIN location_reviews ar on ar.objectid=c.id
LEFT JOIN freemedia fm on fm.objectid=c.id AND fm.objecttype=c.objecttype
WHERE c.title<>''
GROUP BY au.pref_language,au.username,am.IsOpenIdAccount,am.facebookid,au.sex,au.firstname,au.middlename,au.lastname,am.email,
c.id ,c.title ,c.friendlyurl ,c.objecttype,am.CreateDate,c.description_nl,c.description_en
And I need something like this:
SELECT TOP 10 au.pref_language,au.username,am.IsOpenIdAccount,am.facebookid,au.sex,au.firstname,au.middlename,au.lastname,am.email,
c.id AS objectid,c.title AS objecttitle,c.friendlyurl as objecturl,c.objecttype,am.CreateDate,c.description_nl,c.description_en,
COUNT(distinct ap.id) as totalphotos,
COUNT(distinct ar.id) as totalreviews,
COUNT(distinct fm.id) as totalfreemedia
FROM locations c
INNER JOIN aspnet_users au on au.UserId=c.userid
INNER JOIN aspnet_membership am ON am.userid=au.userid
LEFT JOIN location_photos ap on ap.objectid=c.id
LEFT JOIN location_reviews ar on ar.objectid=c.id
LEFT JOIN freemedia fm on fm.objectid=c.id AND fm.objecttype=c.objecttype
WHERE c.title<>''