Colin Brazier
asked on
Limit results to the most recent instance of the parent table
Hi experts, I've been out of practice with SQL lately. That's my excuse and I'm sticking to it.
This query tells me the players selected for fixtures where players have been selected (for some fixtures, no players are selected).
I need to get the info from just the latest such fixture, i.e. the most recent where players have been selected. How would I best do this?
fixtures_results
id
fr_team_code
fr_match_date
...etc
fixtures_players
fp_fixture_id
fp_player_id
fp_player_surname
...etc
SELECT FR.fr_match_date, FP.fp_player_id
FROM fixtures_results FR INNER JOIN fixtures_players FP
ON FR.id = FP.fp_fixture_id
WHERE FP.fp_fixture_id IS NOT NULL
AND FR.fr_team_code = 'W'
AND FR.fr_season_start = 2017
ORDER BY FR.fr_match_date DESC, FP.fp_player_id
This query tells me the players selected for fixtures where players have been selected (for some fixtures, no players are selected).
I need to get the info from just the latest such fixture, i.e. the most recent where players have been selected. How would I best do this?
fixtures_results
id
fr_team_code
fr_match_date
...etc
fixtures_players
fp_fixture_id
fp_player_id
fp_player_surname
...etc
SELECT FR.fr_match_date, FP.fp_player_id
FROM fixtures_results FR INNER JOIN fixtures_players FP
ON FR.id = FP.fp_fixture_id
WHERE FP.fp_fixture_id IS NOT NULL
AND FR.fr_team_code = 'W'
AND FR.fr_season_start = 2017
ORDER BY FR.fr_match_date DESC, FP.fp_player_id
ASKER
Fair enough. Here are two data samples.
I just need info from the most recent fixture where there are players selected, in this instance, fixture 6717 and its players only.
fixtures.xlsx
players.xlsx
I just need info from the most recent fixture where there are players selected, in this instance, fixture 6717 and its players only.
fixtures.xlsx
players.xlsx
ASKER
This gives the results I want...can I simplify it?
SELECT FR.fr_match_date, FP.fp_fixture_id, FP.fp_player_id
FROM fixtures_results FR INNER JOIN fixtures_players FP
ON FR.id = FP.fp_fixture_id
WHERE FR.fr_team_code = 'W'
AND FR.fr_season_start = 2017
AND FR.fr_match_date = (
SELECT max(fr_match_date)
FROM fixtures_results FR INNER JOIN fixtures_players FP
ON FR.id = FP.fp_fixture_id
WHERE FP.fp_fixture_id IS NOT NULL
AND FR.fr_team_code = 'W'
AND FR.fr_season_start = 2017
)
I think does what you are looking for.
»bp
SELECT
MAX(FR.fr_match_date) AS fr_match_date,
FR.fr_season_start,
FP.fp_player_id,
FP.fp_player_surname
FROM fixtures_results FR
INNER JOIN fixtures_players FP
ON FR.id = FP.fp_fixture_id
WHERE FP.fp_fixture_id IS NOT NULL
AND FR.fr_team_code = 'W'
AND FR.fr_season_start = 2017
GROUP BY FR.fr_season_start,
FP.fp_player_id,
FP.fp_player_surname
ORDER BY fr_match_date,
FR.fr_season_start,
FP.fp_player_id,
FP.fp_player_surname;
»bp
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I have amended it slightly to remove surname and include th fixture ID, and to sort descending.
It brings back all games that meet the criteria, not just the one on the latest date.
SELECT
MAX(FR.fr_match_date) AS fr_match_date,
FR.id,
FR.fr_season_start,
FP.fp_player_id
FROM fixtures_results FR
INNER JOIN fixtures_players FP
ON FR.id = FP.fp_fixture_id
WHERE FP.fp_fixture_id IS NOT NULL
AND FR.fr_team_code = 'W'
AND FR.fr_season_start = 2017
GROUP BY FR.id,
FR.fr_season_start,
FP.fp_player_id
ORDER BY fr_match_date DESC,
FR.id,
FR.fr_season_start,
FP.fp_player_id;
It brings back all games that meet the criteria, not just the one on the latest date.
ASKER
Ignore my last contribution, we crossed!
ASKER
Thanks Bill, will check back later.
Col
Col
Not sure it simplifies much, but I find CTE's can sometimes make things a bit more modular, so here's an approach with that technique.
»bp
WITH FR_CTE (fr_match_date, id) AS
(
SELECT MAX(fr_match_date),
MAX(id)
FROM fixtures_results
INNER JOIN fixtures_players
ON id = fp_fixture_id
WHERE fr_team_code = 'W'
AND fr_season_start = 2017
)
SELECT fr_match_date,
fp_fixture_id,
fp_player_id
FROM FR_CTE
INNER JOIN fixtures_players
ON id = fp_fixture_id
ORDER BY fr_match_date DESC,
fp_player_id;
»bp
ASKER
I think you have it here, except we do need the criteria in both inner and outer, otherwise it includes other team_codes.
Thanks.
Thanks.
»bp