:
team_id total_points
100 9
200 6
300 6
400 6
500 3
,
.
SELECT
*
FROM
(
SELECT
team_id,
COUNT(match_id) AS total_matches,
SUM(goals_for) AS total_goals_for,
SUM(points) AS total_points
FROM match_teams
GROUP BY team_id
) q1 LEFT JOIN
(
SELECT
team_id as same_ranking_team_id,
SUM(points) AS total_points
FROM match_teams
GROUP BY team_id
)q2 on q1.total_points = q2.total_points and q1.team_id != q2.same_ranking_team_id
:
team_id total_points total_points same_ranking_team_id
100 9 null null
200 6 6 300
200 6 6 400
300 6 6 200
300 6 6 400
400 6 6 200
400 6 6 300
500 3 null null
,
SELECT
*
FROM
(
SELECT
team_id,
COUNT(match_id) AS total_matches,
SUM(goals_for) AS total_goals_for,
SUM(points) AS total_points
FROM match_teams
GROUP BY team_id
) q1 LEFT JOIN
(
SELECT
team_id as same_ranking_team_id,
SUM(points) AS total_points
FROM match_teams
GROUP BY team_id
)q2 on q1.total_points = q2.total_points and q1.team_id != q2.same_ranking_team_id
LEFT JOIN
(
select
match_id,
MIN(team_id) as first_team_id,
MAX(team_id) as second_team_id
from match_teams
group by match_id
) q3 on LEAST(q1.team_id, q2.same_ranking_team_id) = q3.first_team_id and GREATEST(q1.team_id, q2.same_ranking_team_id) = q3.second_team_id
:
team_id total_points total_points same_ranking_team_id match_id frs_team scd_team
100 9 null null null null null
200 6 6 300 62 200 300
200 6 6 400 63 200 400
300 6 6 200 62 200 300
300 6 6 400 70 300 400
400 6 6 200 63 200 400
400 6 6 300 70 300 400
500 3 null null null null null
now we need to add the points that team_id scored in this match, later you can also add goels
SELECT
*
FROM
(
SELECT
team_id,
COUNT(match_id) AS total_matches,
SUM(goals_for) AS total_goals_for,
SUM(points) AS total_points
FROM match_teams
GROUP BY team_id
) q1 LEFT JOIN
(
SELECT
team_id as same_ranking_team_id,
SUM(points) AS total_points
FROM match_teams
GROUP BY team_id
)q2 on q1.total_points = q2.total_points and q1.team_id != q2.same_ranking_team_id
LEFT JOIN
(
select
match_id,
MIN(team_id) as first_team_id,
MAX(team_id) as second_team_id
from match_teams
group by match_id
) q3 on LEAST(q1.team_id, q2.same_ranking_team_id) = q3.first_team_id and GREATEST(q1.team_id, q2.same_ranking_team_id) = q3.second_team_id
LEFT JOIN
(
select
match_id,
team_id ,
points
from match_teams
)q4 on q1.team_id = q4.team_id and q3.match_id = q4.match_id
it should give you this
team_id total_points total_points same_ranking_team_id match_id frs_team scd_team team_id match_id points
100 9 null null null null null null null null
200 6 6 300 62 200 300 200 62 3
200 6 6 400 63 200 400 200 63 3
300 6 6 200 62 200 300 300 62 0
300 6 6 400 70 300 400 300 70 3
400 6 6 200 63 200 400 400 63 0
400 6 6 300 70 300 400 400 70 0
500 3 null null null null null null null null
Now you have all the data to classify the teams, the final query is as follows:
SELECT
q1.team_id,
q1.total_points,
sum(COALESCE(q4.points, 0)) as teis_points
FROM
(
SELECT
team_id,
COUNT(match_id) AS total_matches,
SUM(goals_for) AS total_goals_for,
SUM(points) AS total_points
FROM match_teams
GROUP BY team_id
) q1 LEFT JOIN
(
SELECT
team_id as same_ranking_team_id,
SUM(points) AS total_points
FROM match_teams
GROUP BY team_id
)q2 on q1.total_points = q2.total_points and q1.team_id != q2.same_ranking_team_id
LEFT JOIN
(
select
match_id,
MIN(team_id) as first_team_id,
MAX(team_id) as second_team_id
from match_teams
group by match_id
) q3 on LEAST(q1.team_id, q2.same_ranking_team_id) = q3.first_team_id and GREATEST(q1.team_id, q2.same_ranking_team_id) = q3.second_team_id
LEFT JOIN
(
select
match_id,
team_id ,
points
from match_teams
)q4 on q1.team_id = q4.team_id and q3.match_id = q4.match_id
group by q1.team_id, q1.total_points
order by q1.total_points desc, teis_points desc
and it will give you
team_id total_points teis_points
100 9 0
200 6 6
300 6 3
400 6 0
500 3 0
Hope I haven’t missed anything here.
to check my answer:
drop table match_teams;
create table match_teams(
match_id int,
team_id int,
goals_for int,
points int
);
insert into match_teams values
(10,100,2,3),(10,200, 1, 0),(12,100, 3, 3),
(12,300, 0, 0),(15,100, 1, 0),(15,400, 2, 3),
(61,100, 2, 3),(61,500, 0, 0),(62,200, 5, 3),
(62,300, 1, 0),(63,200, 2 ,3),(63,400, 1 ,0),
(66,200, 0 ,0),(66,500, 3 ,3),(70,300, 4 ,3),
(70,400, 0 ,0),(73,300, 5 ,3),(73,500, 1 ,0),
(77,400, 2 ,3),(77,500, 1 ,0);