2017-04-01 56 views
0

發現球隊贏得和輸掉比賽的次數? 找出球隊贏得和輸掉比賽的次數?

Find how many times team has won and loss the matches ? 
 
My Table Structure 
 

 
TeamA | TeamB | Won 
 
A  | D  | D 
 
B  | A  | A 
 
A  | D  | A 
 

 
Result should be like below 
 

 
TeamName | Won | Lost 
 
A  | 2 | 1 
 
B  | 0 | 1 
 
D  | 1 | 1

+2

你應該真正展示你的嘗試。 –

回答

0

您將使用此查詢得到正確的結果:

選擇team.t AS TeamName,(SELECT COUNT(*)FROM TeamTable WHERE贏得= team.t)AS贏了,( SELECT WHERE won!= team.t AND(TeamA = team.t || TeamB = team.t))AS從(選擇= TeamA AS t來自TeamTable UNION SELECT TeamB AS t FROM TeamTable)團隊

+0

非常感謝Gurubaksh的幫助。 –

0

條件聚合並結合使用以使數據標準化。

SELECT A.Team as TeamName, 
SUM(CASE WHEN A.TeamA=A.Won then 1 else 0 end) as Won 
SUM(CASE WHEN A.TeamA<>A.Won then 1 else 0 end) as Lost 
FROM (SELECT TeamA as Team, Won 
     FROM TABLE 
     UNION ALL 
     SELECT TeamB as Team, Won 
     FROM Table 
    ) A 
GROUP BY A.Team 
0

您需要先使用teamA和teamB的聯合來獲取所有團隊。

一旦完成,您就可以通過在贏得團隊中進行分組來進行獲勝球隊的統計。

如果team won爲空,則表示它失敗並且不爲null意味着如果贏了並且數不過是贏得比賽的次數。下面是同樣的查詢

選擇t.team,當won不爲空的情況下,然後贏得其他0結束爲贏得,當贏得爲null的情況下,則1其他0結束作爲丟失(從選擇teamA aa來自表 Union 從表中選擇teamB)t左加入 (選擇韓元,贏得韓元)韓元表示t.team = m.won

相關問題