0

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<>''
4

1 回答 1

2

Can you use a view in Sql then run a Select from it?

CREATE VIEW [dbo].[vwMyView]
AS
    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

    GO

Then from your App that parses simply type:

    SELECT 
        pref_language,
        username,
        IsOpenIdAccount,
        facebookid,
        sex,
        firstname,
        middlename,
        lastname,
        email,
        objectid,
        objecttitle,
        objecturl,
        objecttype,
        CreateDate,
        description_nl,
        description_en,
        totalPhotos,
        totalReviews,
        totalFreeMedia
    from [dbo].vwMyView
于 2013-03-30T19:45:14.533 回答