mysql-將多個SUM查詢合並為一個結果


標簽:multiple-tables sum sql mysql sql-server


所以…我們有3個與比賽有關的表格,其中DB跟蹤他們為每個比賽獲得的積分.競賽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,共TOTALS(競賽1競賽2競賽3)

或至少像

USER,共1個競賽,共2個競賽,共3個競賽

到目前為止,我這樣做的方法是將每個結果復制/粘貼到excel中,然后我使用VLOOKUP將它們相互匹配,這有點麻煩,並且肯定有一種方法可以在SQL中完成.我對SQL來說還很陌生,我曾嘗試加入並使用usig ON來匹配userid,但是我的語法以及我了解到查詢都可以插入其中的方式出了點問題.

解決方法:

您需要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將返回相同的結果.
如果想要0,則將為不存在的數據返回NULL,而應使用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;
 

標簽:multiple-tables,sum,sql,mysql,sql-server
來源: https://codeday.me/bug/20191122/2062214.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM