您需要 UNION 结果:
SELECT userID, SUM(Points) AS total
FROM
(
SELECT userID, SUM(amount1) AS "Points"
FROM [Company].[dbo].[Contest1]
WHERE userid NOT IN (0,1)
GROUP BY userId
UNION ALL
SELECT userId, SUM(amount2)/.65 AS "Category 2 Points"
FROM [Company].[dbo].[Contest2]
WHERE dateGiven >=201301 AND dateGiven <= 201305
GROUP BY userId
UNION ALL
SELECT userid, SUM(amount3) AS "Category 3 Points"
FROM [Company].[dbo].[Contest3]
WHERE userid NOT IN (1,2)
GROUP BY userid
) AS dt
GROUP BY userID
ORDER BY 2 DESC;
编辑:要获得三个单独的列,您只需使用三个 SUM 而不是一个:
SELECT userID, SUM("Category 1 Points"), SUM("Category 2 Points"), SUM("Category 3 Points")
FROM
(
SELECT userID, SUM(amount1) AS "Category 1 Points"
FROM [Company].[dbo].[Contest1]
WHERE userid NOT IN (0,1)
GROUP BY userId
UNION ALL
SELECT userId, SUM(amount2)/.65 AS "Category 2 Points"
FROM [Company].[dbo].[Contest2]
WHERE dateGiven >=201301 AND dateGiven <= 201305
GROUP BY userId
UNION ALL
SELECT userid, SUM(amount3) AS "Category 3 Points"
FROM [Company].[dbo].[Contest3]
WHERE userid NOT IN (1,2)
GROUP BY userid
) AS dt
GROUP BY userID
ORDER BY 2 DESC;
当然,每个 userDI/类别只有一行,因此 MIN 或 MAX 将返回相同的结果。这将为不存在的数据返回 NULL,如果您想要 0 而不是使用 COALESCE("Category x Points", 0)。
您也可以加入结果集,但除非保证每个用户都参加了每场比赛,否则您需要使用 COALESCE 进行 FULL OUTER JOIN:
SELECT userID, "Category 1 Points", "Category 2 Points", "Category 3 Points"
FROM
(
SELECT userID, SUM(amount1) AS "Category 1 Points"
FROM [Company].[dbo].[Contest1]
WHERE userid NOT IN (0,1)
GROUP BY userId
) AS t1
FULL JOIN
ON t1.userID = t2.userID
(
SELECT userId, SUM(amount2)/.65 AS "Category 2 Points"
FROM [Company].[dbo].[Contest2]
WHERE dateGiven >=201301 AND dateGiven <= 201305
GROUP BY userId
) AS t2
FULL JOIN
(
SELECT userid, SUM(amount3) AS "Category 3 Points"
FROM [Company].[dbo].[Contest3]
WHERE userid NOT IN (1,2)
GROUP BY userid
) AS t3
ON COALESCE(t1.userID, t2.userID) = t3.userID
ORDER BY 2 DESC;