Commit 00f2d4c6 authored by O'Reilly Media, Inc.'s avatar O'Reilly Media, Inc.
Browse files

Initial commit

parents
Loading
Loading
Loading
Loading

README.md

0 → 100644
+15 −0
Changes for README.md: 15 added lines, 0 removed lines.
Original line number Diff line number Diff line
## Example files for the title:  
  
# Up and Running with Microsoft SQL Server, by Wes Higbee
  
[![Up and Running with Microsoft SQL Server, by Wes Higbee](http://akamaicovers.oreilly.com/images/9781771375825/cat.gif)](https://www.safaribooksonline.com/library/view/title/9781771375825//)
  
The following applies to example files from material published by O’Reilly Media, Inc. Content from other publishers may include different rules of usage. Please refer to any additional usage rights explained in the actual example files or refer to the publisher’s website.
  
O'Reilly books are here to help you get your job done. In general, you may use the code in O'Reilly books in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from our books does not require permission. Answering a question by citing our books and quoting example code does not require permission. On the other hand, selling or distributing a CD-ROM of examples from O'Reilly books does require permission. Incorporating a significant amount of example code from our books into your product's documentation does require permission.
  
We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN.
  
If you think your use of code examples falls outside fair use or the permission given here, feel free to contact us at <permissions@oreilly.com>.
  
Please note that the examples are not production code and have not been carefully testing. They are provided "as-is" and come with no warranty of any kind.
+7 −0
Changes for Up and Running with Microsoft SQL Server - Working Files/Chapter 1/No Work Files.rtf: 7 added lines, 0 removed lines.
Original line number Diff line number Diff line
{\rtf1\ansi\ansicpg1252\cocoartf1038\cocoasubrtf350
{\fonttbl\f0\fswiss\fcharset0 Helvetica;}
{\colortbl;\red255\green255\blue255;}
\margl1440\margr1440\vieww9000\viewh8400\viewkind0
\pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\ql\qnatural\pardirnatural

\f0\fs24 \cf0 No work files for this lesson...}
 No newline at end of file
+7 −0
Changes for Up and Running with Microsoft SQL Server - Working Files/Chapter 2/No Work Files.rtf: 7 added lines, 0 removed lines.
Original line number Diff line number Diff line
{\rtf1\ansi\ansicpg1252\cocoartf1038\cocoasubrtf350
{\fonttbl\f0\fswiss\fcharset0 Helvetica;}
{\colortbl;\red255\green255\blue255;}
\margl1440\margr1440\vieww9000\viewh8400\viewkind0
\pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\ql\qnatural\pardirnatural

\f0\fs24 \cf0 No work files for this lesson...}
 No newline at end of file
+50 −0
Changes for Up and Running with Microsoft SQL Server - Working Files/Chapter 3/GenderPayInequality.sql: 50 added lines, 0 removed lines.
Original line number Diff line number Diff line
use AdventureWorks2014;

--Find employees with at least one pay raise
select BusinessEntityID, count(*) from HumanResources.EmployeePayHistory
	group by BusinessEntityID
	having count(*) > 1

-- Average of all pay rate history (including past salaries)
select E.Gender, avg(EPH.Rate)
	from HumanResources.Employee as E
	inner join HumanResources.EmployeePayHistory EPH
		on E.BusinessEntityID = EPH.BusinessEntityID
	group by E.Gender

-- Average of only current pay rates
select E.Gender, avg(EPH.Rate) AverageCurrentPayRate
	from HumanResources.EmployeePayHistory EPH
	inner join HumanResources.Employee E on E.BusinessEntityID = EPH.BusinessEntityID
  inner join
	 (select BusinessEntityID, max(RateChangeDate) as CurrentRateChangeDate
		 from HumanResources.EmployeePayHistory
		group by BusinessEntityID) CurrentPayChangeDate
		on CurrentPayChangeDate.BusinessEntityID = EPH.BusinessEntityID
			and CurrentPayChangeDate.CurrentRateChangeDate = EPH.RateChangeDate
	group by Gender

-- Query with variables (uncomment where clauses below)
declare @employeeId int
set @employeeId = 16

-- Query pay history to validate data
select BusinessEntityID, Rate, RateChangeDate from HumanResources.EmployeePayHistory 
--where BusinessEntityID = @employeeId
order by BusinessEntityID, RateChangeDate desc

-- Query current pay history records only
select EPH.BusinessEntityID, EPH.Rate CurrentPayRate
	from HumanResources.EmployeePayHistory EPH
  inner join
	 (select BusinessEntityID, max(RateChangeDate) as CurrentRateChangeDate
		 from HumanResources.EmployeePayHistory
		--where BusinessEntityID = @employeeId
		group by BusinessEntityID) CurrentPayChangeDate
		on CurrentPayChangeDate.BusinessEntityID = EPH.BusinessEntityID
			and CurrentPayChangeDate.CurrentRateChangeDate = EPH.RateChangeDate
	order by BusinessEntityID, RateChangeDate desc

-- Query of column descriptions for EmployeePayHistory table
SELECT *
FROM fn_listextendedproperty (NULL, 'schema', 'HumanResources', 'table', 'EmployeePayHistory', 'column', default);
+96 −0
Changes for Up and Running with Microsoft SQL Server - Working Files/Chapter 4/InequalityOrganizationLevels.sql: 96 added lines, 0 removed lines.
Original line number Diff line number Diff line
-- REFACTOR TO COMMON TABLE EXPRESSION
With CurrentEmployeePayHistory AS (
	select EPH.* from HumanResources.EmployeePayHistory EPH
		inner join (select BusinessEntityID, max(RateChangeDate) as CurrentRateChangeDate
						from HumanResources.EmployeePayHistory
						group by BusinessEntityID) CPCD
			on CPCD.BusinessEntityID = EPH.BusinessEntityID and CPCD.CurrentRateChangeDate = EPH.RateChangeDate
)
-- Average of only current pay rates
select Gender, E.OrganizationLevel, avg(CEPH.Rate) AverageCurrentPayRate, count(*)
	from CurrentEmployeePayHistory CEPH
	inner join HumanResources.Employee E on E.BusinessEntityID = CEPH.BusinessEntityID
	group by Gender, OrganizationLevel
	order by OrganizationLevel, Gender







-- COMPUTE GAP IN PAY
With CurrentEmployeePayHistory AS (
	select EPH.* from HumanResources.EmployeePayHistory EPH
		inner join (select BusinessEntityID, max(RateChangeDate) as CurrentRateChangeDate
						from HumanResources.EmployeePayHistory
						group by BusinessEntityID) CPCD
			on CPCD.BusinessEntityID = EPH.BusinessEntityID and CPCD.CurrentRateChangeDate = EPH.RateChangeDate
)
-- Average of only current pay rates
select E.OrganizationLevel, avg(CEPH.Rate) AverageCurrentPayRate, count(*),
	
	-- Male Average Pay Rate 
	--SUM(Rate) as TotalRate,
	--SUM(CASE WHEN Gender='M' THEN Rate END) as MaleTotalRate,
	--SUM(CASE WHEN Gender='M' THEN 1 END) as MaleTotalCount,
	SUM(CASE WHEN Gender='M' THEN Rate END) / SUM(CASE WHEN Gender='M' THEN 1 END) as MaleAvgPayRate,

	-- Female Average Pay Rate
	--SUM(CASE WHEN Gender='F' THEN Rate END) as FemaleTotalRate,
	--SUM(CASE WHEN Gender='F' THEN 1 END) as FemaleTotalCount,
	SUM(CASE WHEN Gender='F' THEN Rate END) / SUM(CASE WHEN Gender='F' THEN 1 END) as FemaleAvgPayRate,
	
	-- Gap In Pay ( Male Average Pay Rate - Female Average Pay Rate)

	SUM(CASE WHEN Gender='M' THEN Rate END) / SUM(CASE WHEN Gender='M' THEN 1 END) -
		SUM(CASE WHEN Gender='F' THEN Rate END) / SUM(CASE WHEN Gender='F' THEN 1 END) as GapMaleToFemale

	from CurrentEmployeePayHistory CEPH
	inner join HumanResources.Employee E on E.BusinessEntityID = CEPH.BusinessEntityID
	group by OrganizationLevel
	order by OrganizationLevel











--RANK FUNCTIONS EXAMPLE
With CurrentEmployeePayHistory AS (

	select * from 
		(select EPH.*,
			ROW_NUMBER() OVER (PARTITION BY BusinessEntityId ORDER BY RateChangeDate desc) as RowNumber
			from HumanResources.EmployeePayHistory EPH) CEPH
		where RowNumber = 1

)
-- Average of only current pay rates
select E.OrganizationLevel, avg(CEPH.Rate) AverageCurrentPayRate, count(*),
	
	-- Male Average Pay Rate 
	--SUM(Rate) as TotalRate,
	--SUM(CASE WHEN Gender='M' THEN Rate END) as MaleTotalRate,
	--SUM(CASE WHEN Gender='M' THEN 1 END) as MaleTotalCount,
	SUM(CASE WHEN Gender='M' THEN Rate END) / SUM(CASE WHEN Gender='M' THEN 1 END) as MaleAvgPayRate,

	-- Female Average Pay Rate
	--SUM(CASE WHEN Gender='F' THEN Rate END) as FemaleTotalRate,
	--SUM(CASE WHEN Gender='F' THEN 1 END) as FemaleTotalCount,
	SUM(CASE WHEN Gender='F' THEN Rate END) / SUM(CASE WHEN Gender='F' THEN 1 END) as FemaleAvgPayRate,
	
	-- Gap In Pay ( Male Average Pay Rate - Female Average Pay Rate)

	SUM(CASE WHEN Gender='M' THEN Rate END) / SUM(CASE WHEN Gender='M' THEN 1 END) -
		SUM(CASE WHEN Gender='F' THEN Rate END) / SUM(CASE WHEN Gender='F' THEN 1 END) as GapMaleToFemale

	from CurrentEmployeePayHistory CEPH
	inner join HumanResources.Employee E on E.BusinessEntityID = CEPH.BusinessEntityID
	group by OrganizationLevel
	order by OrganizationLevel
 No newline at end of file
Loading