[英]sum up frags and group teams with sql query
我有一張有兩個團隊和碎片計數的桌子
| id | teamA_name | teamA_frags | teamB_name | teamB_frags |
| 1 | 5 | 3 | 7 | 9 |
| 2 | 5 | 4 | 7 | 1 |
| 3 | 3 | 2 | 6 | 4 |
| 4 | 3 | 8 | 6 | 8 |
是否可以總結所有碎片並像這樣呈現這些數據?
| team | frags |
| 5 | 7 |
| 7 | 10 |
| 3 | 10 |
| 6 | 12 |
編輯 1 :我嘗試使用 group by 和 group concat 進行查詢,但他們沒有給我我想要的 output。 例如:
SELECT teamA_name, teamB_name, SUM(teamA_frags), SUM(teamB_frags) FROM Table;
這是對碎片的總結,但打印團隊彼此相鄰。 我對 SQL 的了解有限。
基本上,您需要取消透視並重新聚合。 這是一種方法:
select team, sum(frags)
from ((select teamA_name as team, teamA_frags as frags
from t
) union all
(select teamB_name as team, teamB_frags as frags
from t
)
) t
group by team;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.