Skip to content

Instantly share code, notes, and snippets.

@wesrice
wesrice / sabermetrics_batting.sql
Last active August 22, 2022 18:40
SQL Views for Sabermetrics using Lahman's Baseball Database
CREATE OR REPLACE VIEW sabermetrics_batting AS
SELECT
batting.*,
-- PA - Plate Appearances
(batting.AB + batting.BB + batting.HBP + batting.SF + batting.SH) as PA,
-- BB% - Walk Percentage (http://www.fangraphs.com/library/offense/rate-stats/)
round((batting.BB / (batting.AB + batting.BB + batting.HBP + batting.SF + batting.SH)), 3) as BBpct,
<?php
/**
* Returns the total number of elements that match a given criteria.
*
* @param ElementCriteriaModel $criteria An element criteria model that defines the parameters for the elements
* we should be counting.
*
* @return int The total number of elements that match the criteria.
*/