我想在SQL Server中对两个不同列的值进行求和并按团队进行分组,但是我没有取得太大的成功。
以下是数据示例:
在上图中,我希望如何总结。
这是我到目前为止编写的代码:
Select
Fut.HomeTeam As Equipa,
Sum(Fut.FTHG) As Golos
From
Database Fut
Group By
Fut.HomeTeam
Union All
Select
Fut.AwayTeam As Equipa,
Sum(Fut.FTAG) As Golos
From
Database Fut
Group By
Fut.AwayTeam
Order By
Golos Desc
答案 0 :(得分:2)
你非常接近,我认为这就是你想要的:
select
Equipa
, sum(Golos) as Golos
from (
Select
Fut.HomeTeam As Equipa,
Fut.FTHG As Golos
From Database Fut
Union All
Select
Fut.AwayTeam As Equipa,
Fut.FTAG As Golos
From Database Fut
) u
Group By Equipa
Order By Sum(Golos) Desc