Skip to content

Instantly share code, notes, and snippets.

View dnasca's full-sized avatar

Derrik dnasca

View GitHub Profile
@dnasca
dnasca / Reply.cs
Created April 11, 2015 08:16
Creating a Data layer - 1b.) Data Model Classes - Every Note can have a Reply
/*
The second step in creating the data model class will be modeling the Reply.
Each reply will have it's own Id, a body, created date, and a property that will point Entity to creating a foriegn key (TopicId)
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@dnasca
dnasca / NoteContext.cs
Last active August 29, 2015 14:18
Creating a Data layer - 2.) The Database Context - Inheriting from DbContext
/*
Entity framework expects a Context class. This class is used to communicate between the code and the dB. Since we built our model
classes by convention, we won't need to do much in the way of configuration of our context.
This is the pipeline to all queries, inserts, updates, deletes for any data operation is going to be passed through.
*The next class we'll build will be the NoteRepository class to wrap the context in.
*/
using System;
@dnasca
dnasca / INoteRepository.cs
Created April 11, 2015 09:10
Creating a Data layer - 3a.) Repository - Building an interface that will provide data access to the dB
/*
We'll use an interface because there are going to be places in the future when we want to be able to
write unit tests. For example, to test against things like controllers. The controller will need a repository to do their data access.
This allows us to split the methods that need to be implemented into a testing and live environment without having to re-write code.
The interface will provide individual methods that have the data operations we will need.
We will use the return type of IQueryable to return Topic objects. This is to allow users to page, sort, group, and filter.
If we did not want/need to do this, we could use IEnumerable instead. This is important because any operations that are requested
@dnasca
dnasca / NoteRepository.cs
Last active August 29, 2015 14:18
Creating a Data layer - 3b.) Repository - Building the implementation that will provide data access to the dB
/*
This is where all of the steps are tied together.
After we complete this class, we will simply supply the Repository (which wraps the Context) directly to the
Controllers constructor method as a parameter.
--For example. After we complete the class below, the Controller might look something like this:
public class HomeController : Controller
{
@dnasca
dnasca / lahman_fantasy_stats_1.sql
Last active August 29, 2015 14:19
This query asks the question with the following constraints: Who are all the players in 2014 that had more than 10 HRs, 50 runs scored, 50 runs batted in, 10 stolen bases, and had at least 300 AB's? Also, for every player in the initial result set who met the requirements of HR>10, R>50, RBI>50, AB>300-- calculate the BBPERC (walks percentage) a…
##Using the baseball statistics dB found at http://www.seanlahman.com/baseball-archive/statistics/
SELECT
b.playerID,
CONCAT(m.nameFirst, ' ', m.nameLast) AS NAME,
SUM(b.AB) AS AB,
SUM(b.HR) AS HR,
SUM(b.R) AS R,
SUM(b.RBI) AS RBI,
SUM(b.SB) AS SB,
ROUND(b.bb / (b.AB + b.BB + b.HBP + COALESCE(b.SF, 0)) * 100,
@dnasca
dnasca / lahman_historical_1995_braves_offensive_1.sql
Created April 18, 2015 07:48
This query will return some offensive stats on the 1995 ATL Braves players with >= 300 AB's during that season, sorted by AB's.
SELECT
b.playerID,
CONCAT(m.nameFirst, ' ', m.nameLast) AS NAME,
AB,
H,
2B,
3B,
HR,
R,
RBI,
@dnasca
dnasca / lahman_historical_career_sopercbbperc_1.sql
Created April 18, 2015 08:11
This query asks - Who are all the players between 1900 and 2014 with over 5000 at bats, include AB, BBPERC, SOPERC, and AVG, sort by SOPERC lowest->highest (Strike-out percentage)
##Using the baseball statistics dB found at http://www.seanlahman.com/baseball-archive/statistics/
SELECT
b.playerID,
CONCAT(m.nameFirst, ' ', m.nameLast) AS NAME,
SUM(b.AB) AS AB,
ROUND(b.bb / (b.AB + b.BB + b.HBP + COALESCE(b.SF, 0)) * 100, 2) AS BBPERC,
ROUND(b.so / (b.AB + b.BB + b.HBP + COALESCE(b.SF, 0)) * 100, 2) AS SOPERC,
ROUND(SUM(b.H) / SUM(b.AB), 3) AS AVG
FROM
Batting b,
@dnasca
dnasca / lahman_historical_strikeoutpercentage.sql
Last active August 29, 2015 14:19
This query will return all players from 1871 - 2014 who had at least 7000 AB's during their career. The list is sorted by the lowest->strikeout percentages (SOPERC)
#using the Lahman Baseball Database
SELECT
CONCAT(YEAR(m.debut), ' - ', YEAR(m.finalGame)) AS YEARSPLAYED,
CONCAT(m.nameFirst, ' ', m.nameLast) AS NAME,
SUM(b.AB) AS AB,
ROUND(SUM(b.so) / SUM(b.AB + b.BB + b.HBP + COALESCE(b.SF, 0)) * 100, 2) AS SOPERC
FROM
Batting b,
Master m
@dnasca
dnasca / career_b_singles.sql
Created April 21, 2015 03:44
Career singles
#using the Lahman Baseball Database
SELECT
m.birthYear AS BIRTH,
CONCAT(YEAR(m.debut), ' - ', YEAR(m.finalGame)) AS YEARSPLAYED,
CONCAT(m.nameFirst, ' ', m.nameLast) AS NAME,
SUM(b.AB) AS AB,
ROUND(SUM(b.so) / SUM(b.AB + b.BB + b.HBP + COALESCE(b.SF, 0)) * 100, 2) AS SOPERC,
SUM(b.h) AS H,
SUM(b.h - (2b + 3b + hr)) AS 1B
@dnasca
dnasca / 2014_b_BABIP.sql
Created April 21, 2015 04:50
Batting Average on Balls In Play (BABIP) - measures how often a ball in play goes for a hit. A ball is “in play” when the plate appearance ends in something other than a strikeout, walk, hit batter, catcher’s interference, sacrifice bunt, or home run. In other words, the batter put the ball in play and it didn’t clear the outfield fence.
#using the Lahman Baseball Database
SELECT
m.birthYear AS BIRTH,
CONCAT(m.nameFirst, ' ', m.nameLast) AS NAME,
SUM(b.AB) AS AB,
SUM((b.H - b.HR)/(b.AB - b.SO - b.HR + COALESCE(b.SF, 0))) AS BABIP #Batting Average on Balls In Play
FROM
Batting b,
Master m