Hello All,
I think I may have posted or added to another post before, but this is my first creating one.
I have been working through the Off Platform Project in the ‘Design Databases with PostgreSQL’ skill path.
Looking at a lot of other posts similar asking for help, I figured I would post what I have as I sat for almost 3 hours trying to crack just the first question.
The hardest part here was undoubtedly the fact that multiple team names are linked with the same id. Example: Philadelphia Phillies franchise. They have had 4 other names and because they were a continuous franchise, associated with one team id.
By using, lets say basic SQL (and I am no expert by any means) you can find the answer but have multiple team names listed, as seen in this screenshot.
After i got this far. I added the row_number and a partition to give me a rank. I guess I could have used rank here, but this seemed to work for me.
I was then left with old team names like the washington senators in 2016, instead of the washington nationals.
I used this code to ‘solve’ the problem. except for that naming issue.
Also, I imagine if you are not a baseball fan, this may have been where you stopped, maybe not knowing the names were wrong.
with avgweight as
(
select t.name, round(avg(p.weight),2) as avg_weight, b.yearid from batting b
join people p
on b.playerid = p.playerid
join teams t
on b.teamid = t.teamid
group by 1,3
order by 3 desc
)
select * from (select *,
row_number () over (
partition by yearid
order by avg_weight desc) as rank from avgweight)temp
where rank = 1
group by 3, 1, 2, 4
order by 4, 3 desc
After getting over the frustation, I combined the batting and teams tables to see if i can find something. that led me to this.
select batting.yearid, batting.teamid, teams.id, teams.teamid, teams.name from batting
join teams
on batting.teamid = teams.teamid
order by 5 desc
and right at the top of the query, there was the answer with the Worcester Ruby Legs (great name by the way). 3 years, and 3 different id numbers. not exclusive to the year by the way. Each year, 1880, 1881 and 1882 have 3 id numbers: 86, 94 and 108.
That is where I realized I joined the wrong columns. By joining the teamid columns, I locked it into a singular name.
And thus I was stuck with the first name based on the teamid column instead of the fluidity of the singular id column on the teams table.
After adjusting my join statement to look like this…
with avgweight as
(
select t.name, round(avg(p.weight),2) as avg_weight, b.yearid from batting b
join people p
on b.playerid = p.playerid
join teams t
on b.team_id = t.id
group by 1,3
order by 3 desc
)
select * from (select *,
row_number () over (
partition by yearid
order by avg_weight desc) as rank from avgweight)temp
where rank = 1
group by 3, 1, 2, 4
order by 4, 3 desc
I got this query result, and thus completing the first question… 4 more questions to go and one where i get to make it up.
Here we go!!!