Unit 5 seminar
This presentation is the property of its rightful owner.
Sponsored Links
1 / 52

Unit 5 Seminar PowerPoint PPT Presentation


  • 44 Views
  • Uploaded on
  • Presentation posted in: General

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

Download Presentation

Unit 5 Seminar

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Unit 5 seminar

Unit 5 Seminar


Derived table

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.


Derived table1

Derived Table

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


Example

Example

  • Problem:

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


Derived table2

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


Problem with previous query

Problem with previous query

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


Derived table cont

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


Problem with is null version

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.


Derived table cont1

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


Derived table cont2

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.


Common table expressions cte

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.


Cte cont

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.


Cte cont1

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.


Cte cont2

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.


Cte syntax

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.


Cte syntax1

CTE Syntax

WITH emp (SELECT columnname

FROM tablenname AS column alias

INNER JOIN tablename as column alias

ON table.column = table.column)

You then can use the CTE in a query

SELECT columnname

FROM CTE AS column alias

Etc.


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 )


Query using cte

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;


Example1

Example

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


Example2

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;


Cte s and recursion

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).


Recursive cte

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.


Recursive cte1

Recursive CTE

  • Translation:

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


Recursive deconstructed

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. 


Recursive deconstructed1

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.


Cte s and hierarchies

CTE’s and hierarchies

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


Cte to examine hierarchies

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


Hierarchy example

Hierarchy Example

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


Steps

Steps

  • 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.

  • 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.

  • 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…


Unit 5 seminar

Code

WITH 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


Inline table valued functions tvfs

Inline Table-Valued Functions (TVFs)

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


Syntax

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


Inline table valued functions tvfs1

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.


Syntax1

Syntax

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


Example3

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)


Syntax info

Syntax info

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


Call the function

Call the Function

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


Results

Results


Example 2

Example 2

  • USE PUBSGOCREATE FUNCTION getAuthorsByState(@state CHAR(2) )RETURNS TABLEASRETURN(SELECT au_fname + ' ' + au_lname AS aNameFROM authorsWHERE state = @state)


Example 2 cont

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.


Step through

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 aNameFROM authorsWHERE state = @state


Step through cont

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


Step through cont1

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 PUBSGOSELECT * FROM getAuthorsByState('CA')


Test function

Test Function


Apply

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.


Apply cont

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.


Apply cont1

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. 


Apply cont2

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.


Apply cont3

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.


Comparison

Comparison

  • Cross Apply:

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


Comparison1

Comparison

  • Inner Join

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


Same results achieved

Same results achieved


  • Login