将SUM的多个查询连接到一个结果中

时间:2013-09-17 21:48:41

标签: mysql sql sql-server sum multiple-tables

所以....我们有三个不同的表,这些表与数据库记录他们为每个比赛收到多少积分的比赛有关。比赛1,2和3.每次用户完成某项操作时,将为该用户创建一个带有附加点的新行。因此,为了计算用户收到的所有点,我使用选择总和

SELECT userID, SUM(amount1) as "Contest 1 Points"
FROM [Company].[dbo].[Contest1]
WHERE userid not in (0,1)
GROUP BY userId
ORDER BY userid

因为我还有两场比赛,我也会对每个比赛进行查询......

SELECT userId, SUM(amount2)/.65 AS "Category 2 Points"
FROM [Company].[dbo].[Contest2]
WHERE dateGiven >=201301 AND dateGiven <= 201305
GROUP BY userId
ORDER BY userid



SELECT userid, SUM(amount3) AS "Category 3 Points"
FROM [Company].[dbo].[Contest3]
where userid not in (1,2)
GROUP BY userid
ORDER BY userid

我基本上需要将每个用户从每个比赛中收到的所有积分加到1列中,基本上显示结果 USERID,TOTAL OF TOTALS(比赛1 +比赛2 +比赛3)

或者至少有它,

USER,Contest1 Total,Contest2 Total,Contest3 Total

到目前为止我这样做的方法是将这些结果中的每一个复制/粘贴到excel中,然后我使用VLOOKUP将它们相互匹配,这有点麻烦,我确信这是在SQL中实现它的方法。我对SQL很陌生,我尝试加入并使用usig来匹配用户ID,但是我的语法有问题,以及我如何理解这一切都是为查询插入的。

1 个答案:

答案 0 :(得分:3)

你需要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;

当然每个用户的DI /类别只有一行,所以MIN或MAX会返回相同的结果。 对于不存在的数据,这将返回NULL,如果你想要0而不是使用COALESCE(“类别x点”,0)。

您也可以加入结果集,但除非保证每个用户参加每个比赛,否则您需要使用COALESCE进行全部联接:

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;