Home Forums SQL Server 2019 SQL Server 2019 - Development Create League Standings use game data table with home and away on same line Reply To: Create League Standings use game data table with home and away on same line

  • wrote:

    Hi there! It sounds like you want to create league standings based on game data and team data in different seasons. Here's what you can do:

    First, you'll need to join the two tables together using the team ID. You can do this using a SQL query like this:

    vbnet Copy code SELECT games.Home_Team, games.Away_Team, games.Home_Score, games.Away_Score, teams.Team_Name FROM games JOIN teams ON games.Home_Team = teams.Team_ID This will give you a table with the team names included.

    Next, you'll want to calculate the wins, losses, and win percentage for each team in each season. You can do this using a combination of GROUP BY and CASE statements. Here's an example query:

    sql Copy code SELECT teams.Team_Name, YEAR(games.Game_Date) as Season, SUM(CASE WHEN games.Home_Team = teams.Team_ID AND games.Home_Score > games.Away_Score THEN 1 WHEN games.Away_Team = teams.Team_ID AND games.Away_Score > games.Home_Score THEN 1 ELSE 0 END) as Wins, SUM(CASE WHEN games.Home_Team = teams.Team_ID AND games.Home_Score < games.Away_Score THEN 1 WHEN games.Away_Team = teams.Team_ID AND games.Away_Score < games.Home_Score THEN 1 ELSE 0 END) as Losses, ROUND(SUM(CASE WHEN games.Home_Team = teams.Team_ID OR games.Away_Team = teams.Team_ID THEN 1 ELSE 0 END) / COUNT(*) * 100, 2) as Win_Percentage, SUM(CASE WHEN games.Home_Team = teams.Team_ID THEN games.Home_Score ELSE games.Away_Score END) as Points_For, SUM(CASE WHEN games.Home_Team = teams.Team_ID THEN games.Away_Score ELSE games.Home_Score END) as Points_Against FROM games JOIN teams ON games.Home_Team = teams.Team_ID OR games.Away_Team = teams.Team_ID GROUP BY teams.Team_Name, YEAR(games.Game_Date) ORDER BY Win_Percentage DESC This query will give you the team name, season, wins, losses, win percentage, points for, and points against for each team in each season. You can modify the query to filter by specific seasons or add additional columns as needed.

    Hope this helps! Let me know if you have any further questions.

    I'm pretty sure that, based on this poster's previous history of spam and the fact that this response was generated by one of the multiple forms of AI and the fact that this poster is responding to a nearly year old post were the OP has announce they 'got it to work", this is a prelude to spam.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)