SQL查询使用表中的连接对数据进行分组/计数/汇总

时间:2015-05-28 10:40:55

标签: sql-server join count sql-server-2012 grouping

我有以下表结构:

tables

我希望最终得到一个表,其中包含每个u.MemberUserSetRef的GroupId(可能的两个值)和Cluster(可能的两个值)的每个组合的计数。

如果我这样做:

SELECT        u.MemberUserSetRef, COUNT(r1_1.UserSurveyResultRef) AS count1_1
FROM            [User] AS u INNER JOIN
                         UserSurvey AS s ON u.UserRef = s.UserRef LEFT OUTER JOIN
                         UserSurveyResult AS r1_1 ON s.UserSurveyRef = r1_1.UserSurveyRef
WHERE        (r1_1.GroupId = 'Group1') AND (r1_1.Cluster = 1)
GROUP BY u.MemberUserSetRef

我得到三行,每个唯一的MemberUserSetRef一行,包含UserSurveyResults的计数,其中GroupId为'Group1'且Cluster为1。

我想做的是返回另一列(count1_2),其中GroupId为'Group1'且Cluster为2。

我试过了:

SELECT        u.MemberUserSetRef, COUNT(r1_1.UserSurveyResultRef) AS count1_1, COUNT(r1_2.UserSurveyResultRef) AS count1_2
FROM            [User] AS u INNER JOIN
                         UserSurvey AS s ON u.UserRef = s.UserRef RIGHT OUTER JOIN
                         UserSurveyResult AS r1_1 ON s.UserSurveyRef = r1_1.UserSurveyRef RIGHT OUTER JOIN
                         UserSurveyResult AS r1_2 ON s.UserSurveyRef = r1_2.UserSurveyRef
WHERE        (r1_1.GroupId = 'Group1') AND (r1_1.Cluster = 1) AND (r1_2.GroupId = 'Group1') AND (r1_2.Cluster = 2)
GROUP BY u.MemberUserSetRef

但是我在这个例子中没有得到任何结果。

请指点我正确的方向,所以我可以得到一张如下表:

MemberUserSetRef|count1_1|count1_2|count2_1|count2_2

包含每个MemberUserSetRef的适当计数。

谢谢。

2 个答案:

答案 0 :(得分:1)

Select T1.MemberUserSetRef,T1.count1_1,T1.count2_1
From 
    (SELECT u.MemberUserSetRef, COUNT(r1_1.UserSurveyResultRef) AS count1_1
    FROM [User] AS u INNER JOIN
         UserSurvey AS s ON u.UserRef = s.UserRef LEFT OUTER JOIN
         UserSurveyResult AS r1_1 ON s.UserSurveyRef = r1_1.UserSurveyRef
    WHERE (r1_1.GroupId = 'Group1') AND (r1_1.Cluster = 1)
    GROUP BY u.MemberUserSetRef) T1,
    (SELECT u.MemberUserSetRef, COUNT(r1_1.UserSurveyResultRef) AS count1_1
    FROM [User] AS u INNER JOIN
         UserSurvey AS s ON u.UserRef = s.UserRef LEFT OUTER JOIN
         UserSurveyResult AS r1_1 ON s.UserSurveyRef = r1_1.UserSurveyRef
    WHERE (r1_1.GroupId = 'Group1') AND (r1_1.Cluster = 2)
    GROUP BY u.MemberUserSetRef) T2
Where T1.MemberUserSetRef=T2.MemberUserSetRef

答案 1 :(得分:0)

您可以详细了解GROUPING SETS。 CodeProject上

SELECT        u.MemberUserSetRef, COUNT(r1_1.UserSurveyResultRef) AS count1_1, COUNT(r1_2.UserSurveyResultRef) AS count1_2
FROM            [User] AS u INNER JOIN
                         UserSurvey AS s ON u.UserRef = s.UserRef RIGHT OUTER JOIN
                         UserSurveyResult AS r1_1 ON s.UserSurveyRef = r1_1.UserSurveyRef RIGHT OUTER JOIN
                         UserSurveyResult AS r1_2 ON s.UserSurveyRef = r1_2.UserSurveyRef
WHERE        (r1_1.GroupId = 'Group1') AND (r1_1.Cluster = 1) AND (r1_2.GroupId = 'Group1') AND (r1_2.Cluster = 2)
GROUP BY GROUPING SETS 
 ((u.MemberUserSetRef), (u.MemberUserSetRef, s.UserRef),
 (u.MemberUserSetRef, s.UserRef, r1_1.UserSurveyRef)
 )