Note that there are some explanatory texts on larger screens.

plurals
  1. POSQL for getting match records of 2 teams
    primarykey
    data
    text
    <p>I am having a table with a list of games and it has the team Id's and the score in the game.</p> <p>Here is the table structure.</p> <pre><code>ID HOMETEAM HOMETEAMSCORE AWAYTEAM AWAYTEAMSCORE </code></pre> <ul> <li>The score of each teams in a game will be in the respective columns.</li> <li>When the game is not started, the score will be 0 for both teams.</li> <li>When the game is over, no game can have 0-0 score.</li> </ul> <p>I need to get the game records of 2 particular teams. For example, for team 1 and 2, I would need the output as 1-3, which mean team 1 has won once against team 2 and team 2 has won 3 times against team 1.</p> <p>I am using the below SQL and it works to some extend. But I need to get output as 0-0 for the games which has not yet started (scores of 0-0).</p> <pre><code>SELECT least(homeTeam, awayTeam) team1, greatest(homeTeam, awayTeam) team2, sum(case when awayTeam &gt; homeTeam then case when homeTeamScore &gt; awayTeamScore then 1 else 0 end else case when homeTeamScore &gt; awayTeamScore then 0 else 1 end end) team1Wins, sum(case when hometeam &gt; awayteam then case when homeTeamScore &gt; awayTeamScore then 1 else 0 end else case when homeTeamScore &gt; awayTeamScore then 0 else 1 end end) team2Wins FROM ow_sports_games GROUP BY least(homeTeam, awayTeam), greatest(homeTeam, awayTeam) </code></pre> <p>Also is there any better way to have a better SQL other than this?</p> <p><strong>SQL Fiddle: <a href="http://sqlfiddle.com/#!2/10326/4/1" rel="nofollow">http://sqlfiddle.com/#!2/10326/4/1</a></strong></p> <p><strong>EDIT:</strong></p> <p>Some sample data to explain my requirement in more detail:</p> <pre><code>HOMETEAM HOMETEAMSCORE AWAYTEAM AWAYTEAMSCORE 18 1 22 0 22 2 18 1 18 3 22 2 12 0 13 0 </code></pre> <p>For the teams 18,22 the output should be 2-1 as there is 2 win for team 18 and 1 for team 22</p> <p>For team 12 and 13 the output should be 0-0 as no games has been finished.</p> <p>OUTPUT sample to explain overall scenario:</p> <pre><code>HomeTeam AwayTeam Records 18 22 2-1 22 18 1-2 12 13 0-0 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload