How to use an Inner Join to get desired result in SQL Server 2008

CrashBandicoot

I have 2 tables called Players and Teams. There are about 100 rows of data.

  • Players columns: Player_ID, Player_Name, Team_ID, Country_ID, Captain_ID, Matches_Played

  • Teams columns: Team_ID, Team_Name, Manager_ID, Matches_Won, Matches_Lost, Country_ID

Players table:

  --------------------------------------------------------------------------
  | Player_ID  Player_Name  Team_Id  Country_ID  Captain_ID  Matches_Played|
  -------------------------------------------------------------------------- 
  |    1         Ronaldo       1         1           1            250      |
  |    2         Messi         2         2           2            220      |
  |    3         Marcelo       1         1           1            185      |
  |    4         Suarez        2         2           2            193      |
  --------------------------------------------------------------------------

I want to find the player in each team who has played the most games, using an INNER JOIN.

Desired result:

  --------------------------------------------------------------------------
  | Player_ID  Player_Name  Team_Id  Country_ID  Captain_ID  Matches_Played|
  -------------------------------------------------------------------------- 
  |    1         Ronaldo       1         1           1            250      |
  |    2         Messi         2         2           2            220      |
  --------------------------------------------------------------------------  

The query I tried using:

SELECT 
    p.Player_Name, t.Team_Name, src.Matches_Played AS Matches_Played
FROM 
    Players p
INNER JOIN 
    Teams t ON p.Team_ID = t.Team_ID
INNER JOIN 
    (SELECT Team_ID, MAX(Matches_Played) AS Matches_Played
     FROM Players
     GROUP BY Team_ID) src ON t.Team_ID = src.Team_ID
                           AND p.Team_ID = src.Team_ID;

This query returns the whole table, with the same MAX value of Matches_Played next to each player.

How would I go about fixing my query to get the desired result?

etsa

If I understood your question, I think you can try:

SELECT p.Player_Name, t.Team_Name, src.Matches_Played AS Matches_Played
FROM Players p
INNER JOIN Teams t
ON p.Team_ID = t.Team_ID
INNER JOIN (
            SELECT Team_ID, MAX(Matches_Played) AS Matches_Played
            FROM Players
            GROUP BY Team_ID)src
ON p.Team_ID = src.Team_ID
AND p.Matches_Played = src.Matches_Played;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to get desired result in SQL Server

how to use with and inner join in Sql server query?

Wrong Result With Sum Function When Use Inner Join In SQL Server

Inner join issue to update in SQL Server 2008

How to achieve the desired output in SQL Server 2008?

Use of COALESCE not giving the desired result in SQL Server

How to insert the result in the desired row? SQL Server

How to get DISTINCT row from INNER JOIN Query in SQL Server

How to Compare information schema column with Table Column in Sql Server2008 using inner Join?

SQL Server 2008: Query performance using inner join

Split and inner join return string in row SQL Server 2008

Get DISTINCT records on INNER JOIN SQL Server

How to join tables in SQL Server to get the expected result

How to get the desired from the SQL Server Query

How to use inner join in sql server to join two tables and overwrite that in first table

Inner join gives undesired result with row number in sql server

How to Delete using INNER JOIN with SQL Server?

Inner joins in sql server 2008

how to use 'inner join' and 'order by' in sql

use Inner Join in SQL

Get opposite result of INNER JOIN

How to use DurationFormatUtils class to get the result in desired format?

PHP and mysql how to use joins to get the desired result

Problem with INNER JOIN I'm not getting the desired result

How to get the result in single row when using sub queries in SQL Server 2008?

MySql: how to get the desired result

how to get the desired result of inheritance?

SQL Server 2008 Join type to use for 4 tables

How to get sorted result within INNER JOIN on joined table