52
Unit 5 Seminar

Unit 5 Seminar

  • Upload
    zasha

  • View
    34

  • Download
    0

Embed Size (px)

DESCRIPTION

Unit 5 Seminar. Derived Table. A derived table is a virtual table that’s calculated on the fly from a select statement. - PowerPoint PPT Presentation

Citation preview

Page 1: Unit 5 Seminar

Unit 5 Seminar

Page 2: Unit 5 Seminar

Derived Table

• A derived table is a virtual table that’s calculated on the fly from a select statement.

• The biggest benefit of using derived tables over using temporary tables is that they require fewer steps, and everything happens in memory instead of a combination of memory and disk.

Page 3: Unit 5 Seminar

Derived Table

• The fewer the steps involved, along with less I/O, the faster the performance.

Page 4: Unit 5 Seminar

Example

• Problem:

Generate a report that shows off the total number of orders each customer placed in 1996

Page 5: Unit 5 Seminar

Derived Table

• SELECT C.CustomerID, C.CompanyName, COUNT(O.OrderID) AS TotalOrdersFROM Customers C LEFT OUTER JOIN Orders O ONC.CustomerID = O.CustomerIDWHERE YEAR(O.OrderDate) = 1996GROUP BY C.CustomerID, C.CompanyName

Page 6: Unit 5 Seminar

Problem with previous query

• But there’s something missing. Customers that didn’t place an order in 1996 aren’t showing up.

Page 7: Unit 5 Seminar

Derived Table (cont)

• SELECT C.CustomerID, C.CompanyName,COUNT(O.OrderID) AS TotalOrdersFROM Customers C LEFT OUTER JOIN Orders O ONC.CustomerID = O.CustomerIDWHERE (YEAR(O.OrderDate) = 1996 OR O.OrderDate IS NULL)GROUP BY C.CustomerID, C.CompanyName

Page 8: Unit 5 Seminar

Problem with is null version

• If a customer has placed an order, but just not in the year 1996 they won’t show up. This is because the “is null” check finds customers that have never placed an order—it still doesn’t do anything to add customers who’ve placed an order, but just not in 1996.

Page 9: Unit 5 Seminar

Derived Table (cont)

• SELECT C.CustomerID, C.CompanyName,COUNT(dOrders.OrderID) AS TotalOrdersFROM Customers C LEFT OUTER JOIN/* start our derived table */(SELECT * FROM Orders WHERE YEAR(Orders.OrderDate) = 1996) AS dOrders/* end our derived table */ONC.CustomerID = dOrders.CustomerIDGROUP BY C.CustomerID, C.CompanyName

Page 10: Unit 5 Seminar

Derived Table (cont)

• You should now see a row returned for each Customer and the total number or orders placed in the year 1996—including the customers that didn’t place an order.

• The reason this works is because the LEFT JOIN will include all matches or null values. If the LEFT JOIN has matches (like in our first query,) but non that match the criteria those rows are excluded.

Page 11: Unit 5 Seminar

Common Table Expressions (CTE)

• A common table expression (CTE) can be thought of as a temporary result set that is defined within the execution scope of a single SELECT, INSERT, UPDATE, DELETE, or CREATE VIEW statement.

Page 12: Unit 5 Seminar

CTE (cont)

• Common Table Expressions offer the same functionality as a view, but are ideal for one-off usages where you don't necessarily need a view defined for the system.

Page 13: Unit 5 Seminar

CTE (cont)

• A CTE is similar to a derived table in that it is not stored as an object and lasts only for the duration of the query.

• A CTE can be self-referencing and can be referenced multiple times in the same query.

Page 14: Unit 5 Seminar

CTE (cont)

• CTEs allow you to separate part of your T-SQL logic from your main query instead of using a view, correlated subquery, or temp table.

Page 15: Unit 5 Seminar

CTE Syntax

• Start with the word WITH, followed by a name for your CTE. Follow the name of the CTE with the word AS and a set of parentheses. Inside the parentheses, type in a valid SELECT query. You can then use the CTE in your main query just as if the CTE was a table or view.

Page 16: Unit 5 Seminar

CTE Syntax

WITH emp (SELECT columnnameFROM tablenname AS column aliasINNER JOIN tablename as column aliasON table.column = table.column)

You then can use the CTE in a querySELECT columnnameFROM CTE AS column aliasEtc.

Page 17: Unit 5 Seminar

CTE

• WITH emp AS (SELECT EmployeeID, FirstName, LastName, E.Title, ManagerIDFROM HumanResources.Employee AS E INNER JOIN Person.Contact AS C ON E.ContactID = C.ContactID )

Page 18: Unit 5 Seminar

Query using CTE

• SELECT A.EmployeeID, A.FirstName, A.LastName, A.Title,A.ManagerID, B.FirstName AS MgrFirstName, B.LastName AS MgrLastName, B.Title AS MgrTitleFROM emp AS A INNER JOIN emp AS B ON A.ManagerID = B.EmployeeID;

Page 19: Unit 5 Seminar

Example

• The query returns a list of the customers along with the products ordered on the most recent order.

Page 20: Unit 5 Seminar

Example

• WITH maxDate AS (SELECT MAX(OrderDate) AS MaxOrderDate, CustomerIDFROM Sales.SalesOrderHeaderGROUP BY CustomerID),orders AS (SELECT SalesOrderID, soh.CustomerID, OrderDate FROM Sales.SalesOrderHeader AS soh INNER JOIN maxDate ON soh.CustomerID = maxDate.CustomerID AND soh.OrderDate = maxDate.MaxOrderDate)SELECT CustomerID,ProductID, sod.SalesOrderID,OrderDate FROM orders INNER JOIN Sales.SalesOrderDetail AS sod ON sod.SalesOrderID = orders.salesOrderID;

Page 21: Unit 5 Seminar

CTE’s and Recursion

• Recursion is the process of defining a solution to a problem in terms of itself.

• For example, a teacher needs to sort a stack of tests alphabetically by the students' names. She could process the tests one at a time and, for each test, insert it into the appropriate spot to the left (called insertion sort).

Page 22: Unit 5 Seminar

Recursive CTE

• 2 pieces:

• The base case - what to do when you're done recursing. After dividing the tests into separate piles of say, eight elements per pile, the base case is to sort these piles via insertion sort.

• The recursive step - the action to perform that involves plugging the input "back into" the system. For merge sort, the recursive step is the division of one pile into two. Then into four. Then into eight, and so on, until the base case is reached.

Page 23: Unit 5 Seminar

Recursive CTE

• Translation:

This translates into two SQL queries - one that gets the "initial" data UNIONed with one that performs the recursion.

Page 24: Unit 5 Seminar

Recursive Deconstructed

• The WITH clause is the definition of the CTE and it precedes the outer query, which refers back to the CTE. 

• Within the WITH clause, the anchor member is a SELECT statement that acts as the seed for recursion. 

Page 25: Unit 5 Seminar

Recursive Deconstructed

• It is merged using the UNION ALL operator to the recursive member, which is a SELECT statement that refers back to the CTE; hence it is recursive.

Page 26: Unit 5 Seminar

CTE’s and hierarchies

• CTEs can also be used to recursively enumerate hierarchical data.

Page 27: Unit 5 Seminar

CTE to examine Hierarchies

• ;WITH cte_name (<column list>) AS• (• <query that defines the anchor member>• UNION ALL• <query that is the recursive member,

referencing cte_name>• )• query_statement_using_cte_name

Page 28: Unit 5 Seminar

Hierarchy Example

• Show the levels that directly report to the Product Development Manager

Page 29: Unit 5 Seminar

Steps1. We create the anchor member as the record which is for the

Product Development Manager. As part of this query, we create two pseudo columns. One for indicating the level (called OrgLevel) and for sorting the records in the right fashion (called SortKey). The sort key for us is the primary key of the table converted to a binary column.

2. After the anchor query, we now use this as the input and form the recursive query. Note that the recursive query increments the OrgLevel column and also builds the SortKey column.

3. Since we want only the people who directly report to the product development manager, we specify the condition OrgLevel < 1. What happens if we omit this condition? That is the next sample…

Page 30: Unit 5 Seminar

CodeWITH SampleOrgChart (Level, Position, ReportingLevel, OrgLevel, SortKey)AS ( -- Create the anchor query. This establishes the starting – point SELECT a.LevelID, a.Position, a.ReportingLevelID, 0, CAST (a.LevelID AS

VARBINARY(900)) FROM dbo.SampleOrg a WHERE a.Position = 'Product Development Manager' UNION ALL -- Create the recursive query. This query will be executed -- until it

returns no more rows SELECT a.LevelID, a.Position, a.ReportingLevelID, b.OrgLevel+1, CAST (b.SortKey + CAST (a.LevelID AS BINARY(4)) AS VARBINARY(900)) FROM dbo.SampleOrg a INNER JOIN SampleOrgChart b ON a.ReportingLevelID = b.Level WHERE b.OrgLevel < 1 ) SELECT * FROM SampleOrgChart ORDER BY SortKey

Page 31: Unit 5 Seminar

Inline Table-Valued Functions (TVFs)

• An inline table-valued function can be viewed as a select statement with parameters.

Page 32: Unit 5 Seminar

Syntax

• To create an inline table-valued function, you need to use the "RETURNS TABLE" clause in the "CREATE FUNCTION" statement. There should be no function body, except for a RETURN statement with a SELECT subquery

Page 33: Unit 5 Seminar

Inline Table-Valued Functions (TVFs)

• Inline Table-Valued Functions return a resultset, as opposed to a single scalar value. A table valued function specifies the TABLE keyword in its RETURN clause.

Page 34: Unit 5 Seminar

Syntax

• CREATE FUNCTION [ owner_name. ] fn_name ( [ { @parameter [ AS ] type } [ ,...n ] ]) RETURNS TABLE [ AS ] RETURN [ ( ] select-statement [ ) ]

Page 35: Unit 5 Seminar

Example

• An Inline Table-Valued Function created by this command:

CREATE FUNCTION datesales (@deadline as datetime)

RETURNS TABLE AS RETURN ( SELECT * FROM sales WHERE ord_date > @deadline)

Page 36: Unit 5 Seminar

Syntax info

• A variable has a name that begins with the @ symbol and always must be given a data type.

Page 37: Unit 5 Seminar

Call the Function

• USE PUBS GO select * from datesales('09/13/1994')

Page 38: Unit 5 Seminar

Results

Page 39: Unit 5 Seminar

Example 2• USE PUBS

GO

CREATE FUNCTION getAuthorsByState

(

@state CHAR(2)

)

RETURNS TABLE

AS

RETURN

(

SELECT au_fname + ' ' + au_lname AS aName

FROM authors

WHERE state = @state

)

Page 40: Unit 5 Seminar

Example 2 (cont)

• UDF named "getAuthorsByState"

• Objective: return a list of names of authors whose state field matches the value passed in through the input parameter, @state.

Page 41: Unit 5 Seminar

Step-Through

1st: a basic SQL query to concatenate the values of the au_fname and au_lname fields with a space.

SELECT au_fname + ' ' + au_lname AS aName

FROM authors

WHERE state = @state

Page 42: Unit 5 Seminar

Step-Through (cont)

2nd: In the where clause, we tell SQL Server to only return authors whose state matches the value of our @state input parameter.

WHERE state = @state

Page 43: Unit 5 Seminar

Step-Through (cont)

3rd: To test our new inline-table valued UDF, clear the code window in query analyzer and enter and execute the following code:

USE PUBS

GO

SELECT * FROM getAuthorsByState('CA')

Page 44: Unit 5 Seminar

Test Function

Page 45: Unit 5 Seminar

Apply

• SQL Server 2005 introduced the APPLY operator, which is very much like a join clause and which allows joining between two table expressions i.e. joining a left/outer table expression with a right/inner table expression.

Page 46: Unit 5 Seminar

Apply (cont)

• The APPLY operator allows you to join two table expressions; the right table expression is processed every time for each row from the left table expression.

Page 47: Unit 5 Seminar

Apply (cont)

• The APPLY operator comes in two variants, the CROSS APPLY and the OUTER APPLY.

• The CROSS APPLY operator returns only those rows from left table expression (in its final output) if it matches with right table expression. In other words, the right table expression returns rows for left table expression match only. 

Page 48: Unit 5 Seminar

Apply (cont)

• the OUTER APPLY operator returns all the rows from left table expression irrespective of its match with the right table expression.  For those rows for which there are no corresponding matches in right table expression, it contains NULL values in columns of right table expression.

Page 49: Unit 5 Seminar

Apply (cont)

• the CROSS APPLY is semantically equivalent to INNER JOIN (or to be more precise its like a CROSS JOIN with a correlated sub-query) with a implicit join condition of 1=1 whereas OUTER APPLY is semantically equivalent to LEFT OUTER JOIN.

Page 50: Unit 5 Seminar

Comparison

• Cross Apply:

SELECT * FROM Department D CROSS APPLY    (    SELECT * FROM Employee E    WHERE E.DepartmentID = D.DepartmentID    ) A GO

Page 51: Unit 5 Seminar

Comparison

• Inner Join

SELECT * FROM Department D INNER JOIN Employee E ON D.DepartmentID = E.DepartmentID GO

Page 52: Unit 5 Seminar

Same results achieved