I am using SQL Server. I created two views to get a few results and then made them into one select to use on the inner join. I’m sure there’s an easier way to do this. I couldn’t think of anything else at the moment. Please teach me!
x
create view VWSeasonsTBL
as
select playerID, count(YearID)totalyears, TeamID
from Managers as mgrSeasons
group by playerID, yearID, teamID
create view VWSeasonTotal
as
select playerID, teamID, sum(totalyears) totalSeasons
from VWSeasonsTBL
group by playerID, teamID
select
players.nameFirst, players.nameLast, teamID,
topMgr.totalS
from
(select playerID, teamID, Max(totalSeasons) totals
from VWSeasonTotal as topMgr
group by playerID, teamID) topMgr
inner join
players on topMgr.playerID = players.playerIDpk
order by
teamID
My results
What I’m looking for
Thank you for your time!
Advertisement
Answer
You can do it this way: It might give you many players per team if it happens that the max poinst per team has been reached by many players on the team
select players.nameFirst,players.nameLast, teamID, topMgr.totalS
from
(select teamID, Max(totalSeasons) totals from VWSeasonTotal as topMgr
group by teamID) topMgr
inner join VWSeasonTotal on topMgr.teamID = VWSeasonTotal.teamID
and teamID.totals = VWSeasonTotal.totalSeasons
inner join players on VWSeasonTotal.playerID = players.playerIDpk
order by teamID