Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions Assignment2/assignment2.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
create index idx_batter_counts on baseball.batter_counts(game_id);
create index idx_game on baseball.game(game_id);

--note: commented out after code buddy reviewed this
---create table baseball.results (batter numeric, annual_batting_average double PRECISION, historic_batting_average double precision, rolling_batting_average double PRECISION);

Question 1

annual

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make sure to comment out these labels for the code because it won't run if you don't

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi tom, I changed the code a little bit and created the tables at the beginning of the queries, so I commented out this one since I no longer need it, can you check it one more time?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I commented out that table that I initially created, and stored all results in their own tables (baseball. annual_batting_average, baseball.total_batting_average, baseball.rolling_batting_average). this should work now.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also bit make sure to drop your temporary tables

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bita*

create table baseball.annual_batting_average (select batter, batting_average from(
select btc.batter, sum(btc.hit)/sum(btc.atBat) as batting_average, extract(YEAR from gm.local_date) as year
from baseball.batter_counts btc
left join baseball.game gm
on btc.game_id = gm.game_id
group by (btc.batter), year
having sum(btc.atBat) > 0
order by (btc.batter), year) as aa)


total
create table baseball.total_batting_average (select batter, sum(hit)/sum(atBat) as batting_average
from baseball.batter_counts
group by batter
having sum(atBat) >0);


Question 2

create table baseball.rolling_batting_average (with t1 as (select btc.batter, max(gm.local_date) as max from baseball.batter_counts btc
left join baseball.game gm
on btc.game_id = gm.game_id
group by btc.batter),
t2 as (select btc.batter, sum(btc.hit)/sum(btc.atBat) as batting_average, gm.local_date
from baseball.batter_counts btc
left join baseball.game gm
on btc.game_id = gm.game_id
group by btc.batter, btc.game_id
having sum(btc.atBat)>0)
select t2.batter , avg(t2.batting_average) from t2
right join t1 on t2.batter = t1.batter
where t2.local_date > date_add(t1.max, INTERVAL -100 DAY)
group by t1.batter);

--Explanation about temporary tables:
--I just created those since I thought we have to store the answers in a new table, but if not those tables are not needed and can simply be removed from the query

drop table baseball.annual_batting_average;
drop table baseball.total_batting_average;
drop table baseball.rolling_batting_average;