根据其他列的条件对多个列求和 - SQL Teradata

时间:2016-05-20 10:09:09

标签: sql group-by sum multiple-columns teradata

我想得到第3栏和第3栏的总分数。列1和1中的每个县都有4个如下表所示2:

County1    |    County2     |   Player1_Score   |   Player2_Score
Norfolk    |    Hampshire   |       5           |       7
Suffolk    |    Norfolk     |       10          |       6
Hampshire  |    Suffolk     |       16          |       12
Norfolk    |    Suffolk     |       78          |       50
Hampshire  |    Norfolk     |       4           |       8
Suffolk    |    Hampshire   |       9           |       19

所以我希望看到的结果如下:

Norfolk     |       97
Suffolk     |       77
Hampshire   |       32

任何人都可以帮忙吗?我尝试了一个SELECT/GROUP BY查询但对SQL很新,无法获得我想要的结果。

非常感谢

2 个答案:

答案 0 :(得分:3)

试试这个:

**This is ajax function**

  $.ajax({  
     type : "Get",   
     url : url, 
     data:$("#client").serialize(),
     dataType:'json',
     success : function(response) 
     {  

     },  
     error : function(e) 
     {  
      alert('Error: ' + e); 

     }  
    }); 

Demo here

答案 1 :(得分:0)

SEL CNTRY,SUM(SUM1) FROM
(
SEL COUNTRY1 AS CNTRY, SUM(player_score_1) AS SUM1 FROM VT1
GROUP BY 1
UNION 
SEL COUNTRY2 AS CNTRY, SUM(player_score_2) FROM VT1
GROUP BY 1
) A
GROUP BY 1