mysql - 将 SUM 的多个查询合并为一个结果

标签 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(Contest1 + Contest2 + Contest3)

或者至少让它喜欢,

USER,比赛 1 总计,比赛 2 总计,比赛 3 总计

到目前为止,我这样做的方法是将这些结果中的每一个复制/粘贴到 excel 中,然后我使用 VLOOKUP 将它们相互匹配,这有点麻烦,我相信有一种方法可以在 SQL 中做到这一点。我是 SQL 的新手,我尝试加入并使用 ON 来匹配用户 ID,但我的语法有问题,我理解它全部插入自身以进行查询。

最佳答案

您需要对结果进行 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;

关于mysql - 将 SUM 的多个查询合并为一个结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18860489/

相关文章:

mysql - 类别、子类别和服务的数据库结构

php - fatal error : Call to a member function fetchAll

mysql - SQL 选择前 n% 行

mysql - 如何对此 MySQL 查询进行 ORDER BY?

sql-server - 更新和左外连接语句

mysql - 仅当不存在非空值时才选择空值?

mysql - 为什么 phpMyAdmin 没有完全配置?

mysql - 如何在两列上设置唯一键并在 MySQL 中仍然具有自动递增的主索引?

python - 执行包含 USE 语句的批处理时出现 "Invalid cursor state"错误

sql-server - MSSQL 聚合忽略 where 子句