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

Initial commit

parents
Loading
Loading
Loading
Loading
+7 −0
Changes for Querying Data with Transact-SQL - Exam 70-761 Certification Training - 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
+33 −0
Changes for Querying Data with Transact-SQL - Exam 70-761 Certification Training - Working Files/Chapter 10/Scalar User Defined Function Example.sql: 33 added lines, 0 removed lines.
Original line number Diff line number Diff line
-- Scalar UDF Example

-- We will create a function to provide a more 
-- descriptive result set for the EmailPromotion column
-- in the Person.EmailAddress table
SELECT * FROM Person.Person

-- Create a function named fnEmailPromo
-- It will accept EmailAddress as an input and
-- return a string value based on the int value
-- being passed in
-- We will use the function in a select statement
CREATE FUNCTION fnEmailPromo (@promo int)
RETURNS varchar(20)
AS
BEGIN
DECLARE @ret_promo varchar(20)
select @ret_promo=
	CASE 
		WHEN @promo=0 then 'None'
		WHEN @promo=1 then 'Local'
		WHEN @promo=2 THEN 'National'
		ELSE 'Not designated'
	END
	RETURN @ret_promo
END

-- Now, let's user our new fnEmailPromo function
-- Note that to call the new function we must include
-- dbo. in the name
SELECT FirstName + ' ' + Lastname Customer, dbo.fnEmailPromo(EmailPromotion) 'Promotion Type'
FROM Person.Person
+32 −0
Changes for Querying Data with Transact-SQL - Exam 70-761 Certification Training - Working Files/Chapter 10/TVF Example.sql: 32 added lines, 0 removed lines.
Original line number Diff line number Diff line
-- TVF Example

-- Here are the three tables we will be getting our data from
select top 1 * from person.person
select top 1 * from person.emailaddress
select top 1 * from humanresources.employee

-- Our TVF will return the names and email addresses of our
-- employees based on their gender
-- Let's create our TVF

CREATE FUNCTION dbo.fnGetEmail(@gender char(1))
RETURNS TABLE
AS
RETURN
	SELECT p.FirstName, p.Lastname,e.EmailAddress,em.Gender
	FROM person.person p
	JOIN person.EmailAddress e on p.BusinessEntityID=e.BusinessEntityID
	JOIN HumanResources.Employee em on e.BusinessEntityID=em.BusinessEntityID
	WHERE em.Gender=@gender

-- Let's use our TVF
SELECT * FROM dbo.fnGetEmail('m')


-- We can add a WHERE clause to get more creative with the results
SELECT * FROM dbo.fnGetEmail('m')
WHERE LastName LIKE 'D%'



+27 −0
Changes for Querying Data with Transact-SQL - Exam 70-761 Certification Training - Working Files/Chapter 11/Correlated Subquery Example.sql: 27 added lines, 0 removed lines.
Original line number Diff line number Diff line
--Correlated Subquery

--First let's look at a join query
SELECT DISTINCT p.BusinessEntityID, p.FirstName + ' ' + p.LastName Name
FROM
Person.Person p
JOIN
sales.SalesOrderHeader s
ON
p.BusinessEntityID=s.SalesPersonID
WHERE s.TotalDue>150000

--Now, let's write this as a correlated subquery
--NOTE that the inner query runs first and locates a matching record
--The outer query runs using the record found, then the inner query 
--runs again searching for another match...until all records are found
SELECT p.BusinessEntityID, FirstName + ' ' + LastName Salesperson FROM Person.Person p
WHERE
EXISTS
	(SELECT s.SalesPersonID FROM Sales.Salesorderheader s 
	 WHERE TotalDue>150000
	 and p.BusinessEntityID=s.SalesPersonID)




 
+34 −0
Changes for Querying Data with Transact-SQL - Exam 70-761 Certification Training - Working Files/Chapter 11/Multi-Valued Subquery Example.sql: 34 added lines, 0 removed lines.
Original line number Diff line number Diff line
-- Multi-vaued query examples

-- Let's start off with a simple multi-valued query
-- that returns a list of values
-- We need a list of employees who were born after 1972
-- Here' where we can get the data...
SELECT TOP 1 * from Person.Person
SELECT TOP 1 * from HumanResources.Employee

-- We can use a subquery to pull the list of BusinesEntityID's
-- for every BirthDate greater than 1972 in the Employee table
-- and use that list with the IN keyword to get the Employee names
-- from the Person table

SELECT Firstname + ' ' + Lastname Employee
FROM Person.Person
WHERE Person.BusinessEntityID IN
	(SELECT BusinessEntityID
	 FROM HumanResources.Employee
	 WHERE Year(BirthDate)>1972)
ORDER BY LastName, FirstName


-- Now, let's use a table-value subquery...also known as a table expression
-- to find the last SalesOrderID for each year
SELECT OrderYear, MAX(SalesOrderID) LastOrderID
FROM
	(SELECT SalesOrderID, Year(OrderDate) OrderYear -- Run this inner query separately 
	 FROM Sales.SalesOrderHeader)as D               -- to see the table being returned
GROUP BY OrderYear 
ORDER BY OrderYear


Loading