选择何时和总和

时间:2013-06-19 07:26:11

标签: sql case

我有桌子匹配,我需要目标总和。如果Match.Home_team='Tupesy'我需要Match.Home_team='Tupesy'的总和,sum的其他Match.Away_goals

现在我有了这个,但是完全错了:/

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');

1 个答案:

答案 0 :(得分:1)

可能是这样的(SUM和CASE应该按相反顺序编写):

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