2017-03-18 144 views
0

我想在SQL Server中總結來自兩個不同列的值和團隊分組,但我沒有得到太多的成功。SQL Server總計多個列和組

下面是數據的一個例子:

Dataset example

I should be getting the following result:

而且上面的圖片,我怎麼會喜歡上求和。

這裏是我到目前爲止已經編寫的代碼:

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

待辦事項FTHG和FTAG有任何與此如何計算?他們不是很有意義。 –

+0

我的猜測是全職{Away | Home}目標 –

回答

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

它工作。謝謝。 :) –

+0

@VitorBarreto樂意幫忙! – SqlZim