mysql 將多個SUM查詢合併為乙個結果

2022-02-02 09:50:15 字數 3230 閱讀 7475

標籤:multiple-tables

sumsql

mysql

sql-server

所以…我們有3個與比賽有關的**,其中db跟蹤他們為每個比賽獲得的積分.競賽1、2和3.每當使用者獲得成就時,就會為該使用者建立乙個新行,並附加得分.因此,為了計算使用者獲得的所有積分,我使用選擇總和

select userid,sum(amount1)as"contest 1 points"from [company].[dbo].[contest1]where userid notin(0,1)group by userid

order by userid

因為我還有另外兩個比賽,所以我也要查詢每個比賽…

select userid,sum(amount2)/.65as "category 2 points"from [company].[dbo].[contest2]where dategiven >=201301and dategiven <=201305group by userid

order by userid

select userid,sum(amount3)as "category 3 points"from [company].[dbo].[contest3]whereuserid notin(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)/.65as "category 2 points"from [company].[dbo].[contest2]where dategiven >=201301and dategiven <=201305group 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 2desc;

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)/.65as "category 2 points"from [company].[dbo].[contest2]where dategiven >=201301and dategiven <=201305group 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 2desc;

當然,每個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)/.65as "category 2 points"from [company].[dbo].[contest2]where dategiven >=201301and dategiven <=201305group 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 2desc;

標籤:multiple-tables,sum,sql,mysql,sql-server

mysql將查詢到的多個id合併成id字串

開發介面遇到乙個情景,查詢符合條件的id,將查詢出來的id作為字串更新到另乙個欄位中。但查詢到的結果是這樣的 需要將查詢到的值合併成字串,更新到另乙個欄位中,用函式group concat完美解決。將查詢到的id語句加上這個函式即可。update asm vip passenger set acco...

mysql 合併查詢

該操作符用於取得兩個結果集的並集。當使用該操作符時,會自動去掉結果集中重複行 mysql select ename,sal,job from emp where sal 2500 union select ename,sal,job from emp where job manager ename ...

如何將多個字典合併?

在stackoverflow中看到的這個問題。現有兩個字典,使用update方法 python x y z x.update y print z print x 12 3 4 5 6 x y z x update y print z print x 結果z卻是空,x卻被改變了。python none...