简体   繁体   English

用 sql 查询总结碎片和组队

[英]sum up frags and group teams with sql query

I have a table with two teams and frag counts我有一张有两个团队和碎片计数的桌子

| 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       |

Is it possible to sum up all frags and present this data like that?是否可以总结所有碎片并像这样呈现这些数据?

| team | frags |
|  5   |   7   |
|  7   |  10   |
|  3   |  10   |
|  6   |  12   |

EDIT 1 : I tried queries with group by and group concat, but they are not giving me the output I wanted.编辑 1 :我尝试使用 group by 和 group concat 进行查询,但他们没有给我我想要的 output。 For example:例如:

SELECT teamA_name, teamB_name, SUM(teamA_frags), SUM(teamB_frags) FROM Table;

This is summing up frags OK, but printing teams next to eachother.这是对碎片的总结,但打印团队彼此相邻。 My knowledge of SQL is limited.我对 SQL 的了解有限。

Basically, you need to unpivot and re-aggregate.基本上,您需要取消透视并重新聚合。 Here is one approach:这是一种方法:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM