带连接的多个COUNT(*)

时间:2011-12-06 18:44:04

标签: sql-server sql-server-2008 tsql stored-procedures

我必须从多个表中计算一些行。在我可以做多个COUNT之前,我必须进行subselect。这里的问题是我需要加入一些值才能得到正确的结果。

SELECT
    sponsor.Name As SponsorName,
    COUNT(participants.[Table]) AS ParticipantCount,
    (   SELECT 
            COUNT(guestcards.[Table])
        FROM
            guestcards
        WHERE
            guestcards.EventID = @EventID
            AND
            guestcards.[Table] = @Table
            AND
            guestcards.SponsorID = participants.SponsorID
            -- Here lies the problem. 
            -- I will need to check up on another value to ensure I get the right rows, but  participants.SponsorID is not here because of no join :-(

    )  AS GuestParticipantCount
FROM
    participants
    LEFT JOIN
        sponsor
    ON
        sponsor.ID = participants.SponsorID
WHERE
    participants.EventID = @EventID
    AND
    participants.[Table] = @Table
GROUP BY
    sponsor.Name

Guestcards表包含:sponsorid,eventid,tablename

Participantstable持有:sponsorid,eventid,tablename

赞助商表持有:id,name

我需要计算有多少"参与者"有多少" Guestcards"在特定事件中。这些参与者有一张桌子(他们应该坐在哪里),宾客卡也是如此。我需要检查它是否相同"表"他们坐在哪里

所以我需要算一下有多少人坐在桌旁" A1"或表" A2"等

我追求的结果如下:

  

"赞助商名称有5个参与者和3个客人卡。他们坐在A1"

我希望自己清楚明白

1 个答案:

答案 0 :(得分:2)

这与您的查询完全等效(在sponsor.Name上分组):

SELECT  sponsor.name,
        COALESCE(SUM(participantCount), 0),
        COALESCE(SUM(guestcardsCount), 0)
FROM    (
        SELECT  sponsorId, COUNT(*) AS participantCount
        FROM    participants
        WHERE   eventId = @eventId
                AND [table] = @table
        GROUP BY
                sponsorId
        ) p
FULL JOIN
        (
        SELECT  sponsorId, COUNT(*) AS guestcardsCount
        FROM    guestdcards
        WHERE   eventId = @eventId
                AND [table] = @table
        GROUP BY
                sponsorId
        ) g
ON      g.sponsorId = p.sponsorId
FULL JOIN
        sponsor s
ON      s.id = COALESCE(p.sponsorId, g.sponsorId)
GROUP BY
        s.sponsorName

但是,我相信你想要更简单的东西:

SELECT  sponsorName, participantCount, guestcardsCount
FROM    sponsor s
CROSS APLLY
        (
        SELECT  COUNT(*) AS participantCount
        FROM    participants
        WHERE   sponsorId = s.id
                AND eventId = @eventId
                AND [table] = @table
        ) p
CROSS APLLY
        (
        SELECT  COUNT(*) AS guestcardsCount
        FROM    guestdcards
        WHERE   sponsorId = s.id
                AND eventId = @eventId
                AND [table] = @table
        ) g

<强>更新

SELECT  sponsor.name,
        COALESCE(participantCount, 0),
        COALESCE(guestcardsCount, 0)
FROM    (
        SELECT  sponsorId, COUNT(*) AS participantCount
        FROM    participants
        WHERE   eventId = @eventId
                AND [table] = @table
        GROUP BY
                sponsorId
        ) p
FULL JOIN
        (
        SELECT  sponsorId, COUNT(*) AS guestcardsCount
        FROM    guestdcards
        WHERE   eventId = @eventId
                AND [table] = @table
        GROUP BY
                sponsorId
        ) g
ON      g.sponsorId = p.sponsorId
JOIN    sponsor s
ON      s.id = COALESCE(p.sponsorId, g.sponsorId)