05 - set operators grouping and windows functions

Upload: chowdhury-golam-kibria

Post on 07-Aug-2018

224 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/20/2019 05 - SET Operators Grouping and Windows Functions

    1/30

    05 | SET Operators, Windows Functions,

    and Grouping

  • 8/20/2019 05 - SET Operators Grouping and Windows Functions

    2/30

    Querying Microsoft SQL Server 2012 Jump Start

    05 | SET Operators, Windows Functions, and Grouping

    SET operators, Windows functions, GROUPING sets PIVOT, UNPIVOT, CUBE, ROLLUP)

    06 | Modifying DataINSERT, UPDATE, and DELETE statements, use of defaults, constraints, and triggers, OUTPUT

    07 | Programming with T-SQLUsing T-SQL programming elements, implementing error handling, understanding and implementing transactions

    08 | Retrieving SQL Server Metadata and Improving Query PerformanceQuerying system catalogs and dynamic management views, creating and executing stored procedures, improving SQL

    Server query performance

  • 8/20/2019 05 - SET Operators Grouping and Windows Functions

    3/30

  • 8/20/2019 05 - SET Operators Grouping and Windows Functions

    4/30

  • 8/20/2019 05 - SET Operators Grouping and Windows Functions

    5/30

    [ORDER BY ]

  • 8/20/2019 05 - SET Operators Grouping and Windows Functions

    6/30

    -- only distinct rows from both queries are returnedSELECT ProductID, OrderQty, UnitPrice FROM Sales.SalesOrderDetailUNIONSELECT ProductID, OrderQty, UnitPrice FROM Purchasing.PurchaseOrderDetail

    Purchasing Sales

  • 8/20/2019 05 - SET Operators Grouping and Windows Functions

    7/30

    Purchasing Sales

    -- all rows from both queries are returnedSELECT ProductID, OrderQty, UnitPrice FROM Sales.SalesOrderDetailUNION ALLSELECT ProductID, OrderQty, UnitPrice FROM Purchasing.PurchaseOrderDetail

  • 8/20/2019 05 - SET Operators Grouping and Windows Functions

    8/30

    Using the INTERSECT operator

    Purchasing Sales

    INTERSECT

    -- only rows that exist in both queries are returnedSELECT ProductID, OrderQty, UnitPrice FROM Sales.SalesOrderDetailINTERSECTSELECT ProductID, OrderQty, UnitPrice FROM Purchasing.PurchaseOrderDetail

  • 8/20/2019 05 - SET Operators Grouping and Windows Functions

    9/30

    Using the EXCEPT operator

    Sales Purchasing

    -- only rows from Sales are returnedSELECT ProductID, OrderQty, UnitPrice FROM Sales.SalesOrderDetailEXCEPTSELECT ProductID, OrderQty, UnitPrice FROM Purchasing.PurchaseOrderDetail

  • 8/20/2019 05 - SET Operators Grouping and Windows Functions

    10/30

    1.

    2.

    SELECT FROM AS

    CROSS/OUTER APPLY AS

  • 8/20/2019 05 - SET Operators Grouping and Windows Functions

    11/30

  • 8/20/2019 05 - SET Operators Grouping and Windows Functions

    12/30

  • 8/20/2019 05 - SET Operators Grouping and Windows Functions

    13/30

  • 8/20/2019 05 - SET Operators Grouping and Windows Functions

    14/30

    SELECT CustomerID, OrderDate, TotalDue,SUM(TotalDue) OVER(PARTITION BY CustomerID)

    AS TotalDueByCustFROM Sales.SalesOrderHeader;

    CustomerID OrderDate TotalDue TotalDueByCust---------- -------------------------- -------- --------------11000 2007-08-01 00:00:00.000 3756.989 9115.1341

    11000 2007-10-01 00:00:00.000 2587.8769 9115.134111000 2006-09-01 00:00:00.000 2770.2682 9115.134111001 2007-08-01 00:00:00.000 2674.0227 7054.187511001 2006-11-01 00:00:00.000 3729.364 7054.187511001 2007-04-01 00:00:00.000 650.8008 7054.1875

  • 8/20/2019 05 - SET Operators Grouping and Windows Functions

    15/30

  • 8/20/2019 05 - SET Operators Grouping and Windows Functions

    16/30

    Function Description

    RANK Returns the rank of each row within the partition of a result

    set. May include ties and gaps.DENSE_RANK Returns the rank of each row within the partition of a result

    set. May include ties but will not include gaps.

    ROW_NUMBER Returns a unique sequential row number within partitionbased on current order.

    NTILE Distributes the rows in an ordered partition into a specified

    number of groups. Returns the number of the group to whichthe current row belongs.

  • 8/20/2019 05 - SET Operators Grouping and Windows Functions

    17/30

    Function Description

    LAG Returns an expression from a previous row that is a defined offset

    from the current row. Returns NULL if no row at specifiedposition.

    LEAD Returns an expression from a later row that is a defined offsetfrom the current row. Returns NULL if no row at specifiedposition.

    FIRST_VALUE Returns the first value in the current window frame. Requires

    window ordering to be meaningful.LAST_VALUE Returns the last value in the current window frame. Requires

    window ordering to be meaningful.

  • 8/20/2019 05 - SET Operators Grouping and Windows Functions

    18/30

  • 8/20/2019 05 - SET Operators Grouping and Windows Functions

    19/30

  • 8/20/2019 05 - SET Operators Grouping and Windows Functions

    20/30

    Pivoted data

  • 8/20/2019 05 - SET Operators Grouping and Windows Functions

    21/30

    1.

    2.

    3.

    SELECT Category, [2006],[2007],[2008]FROM ( SELECT Category, Qty, Orderyear

    FROM Sales.CategoryQtyYear) AS DPIVOT(SUM(QTY) FOR orderyearIN([2006],[2007],[2008])) AS pvt;

    Grouping

    Spreading

    Aggregation

    SELECT VendorID, [250] AS Emp1, [251] AS Emp2,[256] AS Emp3, [257] AS Emp4, [260] AS Emp5FROM(SELECT PurchaseOrderID, EmployeeID, VendorIDFROM Purchasing.PurchaseOrderHeader) pPIVOT(COUNT (PurchaseOrderID)

    FOR EmployeeID IN( [250], [251], [256], [257], [260] )) AS pvtORDER BY pvt.VendorID;

    VendorID Emp1 Emp2 Emp3 Emp4 Emp5-------- ----- ----- ----- ----- -----

    1492 2 5 4 4 4

    1494 2 5 4 5 4

    1496 2 4 4 5 5

  • 8/20/2019 05 - SET Operators Grouping and Windows Functions

    22/30

    CREATE TABLE pvt (VendorID int, Emp1 int, Emp2 int,Emp3 int, Emp4 int, Emp5 int);

    GOINSERT INTO pvt VALUES (1,4,3,5,4,4);INSERT INTO pvt VALUES (2,4,1,5,5,5);INSERT INTO pvt VALUES (3,4,3,5,4,4);GO

    SELECT VendorID, Employee, OrdersFROM

    (SELECT VendorID, Emp1, Emp2, Emp3, Emp4, Emp5FROM pvt) p

    UNPIVOT

    (Orders FOR Employee IN(Emp1, Emp2, Emp3, Emp4, Emp5)

    )AS unpvt;GO VendorID Employee Orders

    1 Emp1 4

    1 Emp2 3

    W iti i ith i t

  • 8/20/2019 05 - SET Operators Grouping and Windows Functions

    23/30

    Writing queries with grouping sets

    SELECT FROM GROUP BYGROUPING SETS(

    (),--one or more columns

    (),--one or more columns() -- empty parentheses if aggregating all rows

    );

    SELECT TerritoryID, CustomerID, SUM(TotalDue) ASTotalAmountDue

    FROM Sales.SalesOrderHeaderGROUP BYGROUPING SETS((TerritoryID),(CustomerID),());

    TerritoryID CustomerID TotalAmountDue

    --------------- ----------- --------------NULL 30116 211671.2674NULL 30117 919801.8188NULL 30118 313671.5352NULL NULL 123216786.1159

    3 NULL 8913299.24736 NULL 18398929.1889 NULL 11814376.09521 NULL 18061660.3717 NULL 8119749.346

    CUBE d ROLLUP

  • 8/20/2019 05 - SET Operators Grouping and Windows Functions

    24/30

    CUBE and ROLLUP

    SELECT TerritoryID, CustomerID, SUM(TotalDue) ASTotalAmountDueFROM Sales.SalesOrderHeaderGROUP BY CUBE(TerritoryID, CustomerID)

    ORDER BY TerritoryID, CustomerID;

    SELECT TerritoryID, CustomerID, SUM(TotalDue) ASTotalAmountDueFROM Sales.SalesOrderHeaderGROUP BY ROLLUP(TerritoryID, CustomerID)ORDER BY TerritoryID, CustomerID;

  • 8/20/2019 05 - SET Operators Grouping and Windows Functions

    25/30

  • 8/20/2019 05 - SET Operators Grouping and Windows Functions

    26/30

    [ORDER BY ]

  • 8/20/2019 05 - SET Operators Grouping and Windows Functions

    27/30

    -- only distinct rows from both queries are returnedSELECT ProductID, OrderQty, UnitPrice FROM Sales.SalesOrderDetailUNION

    SELECT ProductID, OrderQty, UnitPrice FROM Purchasing.PurchaseOrderDetail

    -- all rows from both queries are returnedSELECT ProductID, OrderQty, UnitPrice FROM Sales.SalesOrderDetailUNION ALLSELECT ProductID, OrderQty, UnitPrice FROM Purchasing.PurchaseOrderDetail

    -- only rows that exist in both queries are returnedSELECT ProductID, OrderQty, UnitPrice FROM Sales.SalesOrderDetailINTERSECTSELECT ProductID, OrderQty, UnitPrice FROM Purchasing.PurchaseOrderDetail

    -- only rows from Sales are returnedSELECT ProductID, OrderQty, UnitPrice FROM Sales.SalesOrderDetailEXCEPTSELECT ProductID, OrderQty, UnitPrice FROM Purchasing.PurchaseOrderDetail

  • 8/20/2019 05 - SET Operators Grouping and Windows Functions

    28/30

  • 8/20/2019 05 - SET Operators Grouping and Windows Functions

    29/30

    Pivoting data is rotating data from a rows-based orientation to acolumns-based orientation and DISTINCT values from a single

    column are displayed across as column headings - may includeaggregation

    The GROUPING SETS clause builds on the T-SQL GROUP BYclause by allowing multiple groups to be defined in the samequery

    A CUBE provides a shortcut for defining grouping sets given alist of columns therefore all possible combinations of GROUPINGSETS are created

    A ROLLUP provides a shortcut for defining grouping sets, bycreating combinations with the assumption the input columnsform a hierarchy

  • 8/20/2019 05 - SET Operators Grouping and Windows Functions

    30/30

    ©2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, Office, Azure, System Center, Dynamics and other product names are or may be registered trademarks and/or trademarks in

    the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because

    Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information

    provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, I MPLIED OR STATUTORY, AS TO THE I NFORMATION IN THIS PRESENTATION.