0

I would like to in SQL Server to sum values from two different columns and grouping by team, but I'm not getting too much success.

Here's an example of the data:

Dataset example

I should be getting the following result:

And on the above image, how i would like it to be summed.

Here's the code I've written so far:

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
2
  • 1
    Do FTHG and FTAG have anything to do with this calculation? They are not very meaningful. Commented Mar 18, 2017 at 20:17
  • My guess would be Full Time {Away|Home} Goals Commented Mar 18, 2017 at 20:31

1 Answer 1

2

You were pretty close, I think this is what you want:

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.