Kate
Kate

Reputation: 372

Select with when and sum

i have table Match and i need sum of goals. If Match.Home_team='Tupesy' I need sum of Match.Home_team='Tupesy', else sum of Match.Away_goals.

Now i have this, but is totally wrong :/

SELECT *,
    CASE (Match.Home_team)
        WHEN (Match.Home_team='Tupesy')THEN (SUM(Match.Home_goals))
        ELSE (SUM(Match.Away_goals))
        END
FROM Match
Where (Match.ID_match='1');

Upvotes: 0

Views: 139

Answers (1)

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186668

It could be like that (SUM and CASE should be written in reversed order):

select Sum(case
             when (Home_Team = 'Tupesy') then
               Home_goals
             else 
               Away_goals 
           end)
  from Match
 where (ID_match = '1')

Upvotes: 1

Related Questions