Public
Edited
Oct 26, 2023
Fork of Prep
Insert cell
Insert cell
batting = FileAttachment("Batting.csv").csv({typed:true})
Insert cell
people = FileAttachment("People.csv").csv({typed:true})
Insert cell
db = DuckDBClient.of({
batting:batting,
people:people
})
Insert cell
db
CREATE VIEW classData
(playerID,teamID,yearID,HR)
AS
SELECT
playerID,teamID,yearID,HR
FROM batting
Insert cell
db
CREATE VIEW classdata
(nameFirst,nameLast,playerID)
AS
SELECT nameFirst,nameLast,batting.playerID
FROM batting INNER JOIN people
ON batting.playerID = people.playerID
Insert cell
db
SELECT * FROM classData
Insert cell
db
SELECT playerID,teamID,yearID,HR,max(HR) OVER ()
FROM batting
Insert cell
db
SELECT playerID,sum(HR) AS careerHR,max(careerHR) OVER ()
FROM batting
GROUP BY playerID
Insert cell
db
SELECT playerID,yearID,teamID,HR,max(HR) OVER (partition by teamID)
FROM batting
WHERE yearID = 2022
ORDER BY teamID
Insert cell
db
SELECT playerID,yearID,teamID,HR,max(HR) OVER (partition by teamID,yearID)
FROM batting
WHERE yearID > 2000
ORDER BY teamID,yearID
Insert cell
db
SELECT playerID,yearID,teamID,HR,rank() OVER (order by HR DESC)
FROM batting
Insert cell
db
SELECT playerID,sum(HR) AS careerHR,rank() OVER (order by careerHR DESC)
from batting
GROUP BY playerID
Insert cell
db
SELECT playerID,teamID,yearID,HR,rank
FROM
(SELECT playerID, teamID,yearID,HR,rank() OVER (partition by teamID order by HR DESC) rank
FROM batting
WHERE yearID = 2022)
WHERE rank <= 10
ORDER BY teamID,rank
Insert cell
db
SELECT playerID,teamID,yearID,HR,100*HR/sum(HR) OVER (partition by teamID) AS percent
from batting
where yearID = 2022
ORDER BY teamID,percent DESC
Insert cell
db
SELECT playerID,teamID,yearID,HR,sum(HR) OVER(rows unbounded preceding)
from batting
WHERE playerID = 'ruthba01'
Insert cell
db
SELECT playerID,teamID,yearID,HR,avg(HR) OVER(rows between 1 preceding AND 1 following )
from batting
WHERE playerID = 'ruthba01'
Insert cell
db
SELECT playerID,teamID,yearID,HR,lag(HR,1) OVER ()
from batting
WHERE playerID = 'ruthba01'
Insert cell
db
SELECT playerID,teamID,yearID,HR,HR - lag(HR,1) OVER ()
from batting
WHERE playerID = 'ruthba01'
Insert cell
db
SELECT playerID,teamID,yearID,HR,(HR - lag(HR,1) OVER ())/lag(HR,1) OVER ()
from batting
WHERE playerID = 'ruthba01'
Insert cell
db
SELECT playerID,group_concat(DISTINCT teamID)
from batting
WHERE playerID = 'ruthba01'
GROUP BY playerID
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more