下表的SQL查询

时间:2018-06-26 13:58:37

标签: sql

表格

Team   Results
A        Win
B        Win
A        Loss
B        Draw
C        Loss 
C        Loss

输出

Team  Win   Loss Draw Total
A      1      1    0    2
B      1      0    1    2
C      0      2    0    2

1 个答案:

答案 0 :(得分:1)

请查看以下查询:

SELECT Team, SUM(w) AS Win, SUM(l) AS Loss, SUM(d) AS Draw, SUM(w)+ SUM(l)+ SUM(d) AS 
Total
FROM 
(
SELECT Team, IF(Results = 'Win',1,0) AS w, IF(Results = 'Loss',1,0) AS l, IF(Results = 
'Draw',1,0) AS d
FROM table1) a
GROUP BY Team