Você está na página 1de 35

04 | Grouping and Aggregating Data

Querying Microsoft SQL Server 2012 Jump Start


01 | Introducing SQL Server 2012
SQL Server types of statements; other SQL statement elements; basic SELECT statements

02 | Advanced SELECT Statements

DISTINCT, Aliases, scalar functions and CASE, using JOIN and MERGE; Filtering and sorting data, NULL
values

03 | SQL Server Data Types

Introduce data types, data type usage, converting data types, understanding SQL Server function types

04 | Grouping and Aggregating Data

Aggregate functions, GROUP BY and HAVING clauses, subqueries; self-contained, correlated, and EXISTS; Views, inline-table
valued functions, and derived tables

| Lunch Break

Eat, drink, and recharge for the afternoon session

Common

SUM
MIN
MAX
AVG
COUNT
COUNT_BIG

Statistical

STDEV
STDEVP
VAR
VARP

Other
CHECKSUM_AGG
GROUPING
GROUPING_ID

SELECT COUNT (DISTINCT SalesOrderID) AS


UniqueOrders,
AVG(UnitPrice) AS Avg_UnitPrice,
MIN(OrderQty)AS Min_OrderQty,
MAX(LineTotal) AS Max_LineTotal
FROM Sales.SalesOrderDetail;
UniqueOrders Avg_UnitPrice Min_OrderQty Max_LineTotal
------------- ------------ ------------ ------------31465
465.0934
1
27893.619000

SELECT SalesPersonID, YEAR(OrderDate) AS OrderYear,


COUNT(CustomerID) AS All_Custs,
COUNT(DISTINCT CustomerID) AS Unique_Custs
FROM Sales.SalesOrderHeader
GROUP BY SalesPersonID, YEAR(OrderDate);

SalesPersonID OrderYear All_Custs Unique_custs


--------------------- ----------- -----------289
2006
84
48
281
2008
52
27
285
2007
9
8
277
2006
140
57

SELECT <select_list>
FROM <table_source>
WHERE <search_condition>
GROUP BY <group_by_list>;

SELECT SalesPersonID, COUNT(*) AS Cnt


FROM Sales.SalesOrderHeader
GROUP BY SalesPersonID;

Logical Order

Phase

Comments

SELECT

FROM

WHERE

GROUP BY

Creates groups

HAVING

Operates on groups

ORDER BY

SELECT CustomerID, COUNT(*) AS cnt


FROM Sales.SalesOrderHeader
GROUP BY CustomerID;

SELECT productid, MAX(OrderQty) AS largest_order


FROM Sales.SalesOrderDetail
GROUP BY productid;

SELECT CustomerID, COUNT(*) AS


Count_Orders
FROM Sales.SalesOrderHeader
GROUP BY CustomerID
HAVING COUNT(*) > 10;

Using a COUNT(*) expression in HAVING clause is useful to solve

common business problems:

Show only customers that have placed more than one order:
SELECT Cust.Customerid, COUNT(*) AS cnt
FROM Sales.Customer AS Cust
JOIN Sales.SalesOrderHeader AS Ord ON Cust.CustomerID =
ORD.CustomerID
GROUP BY Cust.CustomerID
HAVING COUNT(*) > 1;

Show only products that appear on 10 or more orders:


SELECT Prod.ProductID, COUNT(*) AS cnt
FROM Production.Product AS Prod
JOIN Sales.SalesOrderDetail AS Ord ON Prod.ProductID =
Ord.ProductID
GROUP BY Prod.ProductID
HAVING COUNT(*) >= 10;

Working with subqueries

Writing scalar subqueries

SELECT SalesOrderID, ProductID, UnitPrice, OrderQty


FROM Sales.SalesOrderDetail
WHERE SalesOrderID =
(SELECT MAX(SalesOrderID) AS LastOrder
FROM Sales.SalesOrderHeader);

Writing multi-valued subqueries

SELECT CustomerID, SalesOrderId,TerritoryID


FROM Sales.SalesorderHeader
WHERE CustomerID IN (
SELECT CustomerID
FROM Sales.Customer
WHERE TerritoryID = 10);

Writing queries using EXISTS with subqueries

SELECT CustomerID, PersonID


FROM Sales.Customer AS Cust
WHERE EXISTS (
SELECT *
FROM Sales.SalesOrderHeader AS Ord
WHERE Cust.CustomerID = Ord.CustomerID);
SELECT CustomerID, PersonID
FROM Sales.Customer AS Cust
WHERE NOT EXISTS (
SELECT *
FROM Sales.SalesOrderHeader AS Ord
WHERE Cust.CustomerID = Ord.CustomerID);

CREATE VIEW HumanResources.EmployeeList


AS
SELECT BusinessEntityID, JobTitle, HireDate,
VacationHours
FROM HumanResources.Employee;
SELECT * FROM HumanResources.EmployeeList

CREATE FUNCTION Sales.fn_LineTotal (@SalesOrderID INT)


RETURNS TABLE
AS
RETURN
SELECT SalesOrderID,
CAST((OrderQty * UnitPrice * (1 - SpecialOfferID))
AS DECIMAL(8, 2)) AS LineTotal
FROM Sales.SalesOrderDetail
WHERE SalesOrderID = @SalesOrderID ;

Writing queries with derived tables

SELECT <column_list>
FROM (
<derived_table_definition>
) AS <derived_table_alias>;

Derived Tables Must

Have an alias
Have names for all
columns
Have unique names
for all columns
Not use an ORDER BY
clause (without TOP or
OFFSET/FETCH)
Not be referred to
multiple times in the
same query

Derived Tables May

Use internal or
external aliases for
columns
Refer to parameters
and/or variables
Be nested within other
derived tables

DECLARE @emp_id INT = 9;


SELECT orderyear, COUNT(DISTINCT custid) AS cust_count
FROM (
SELECT YEAR(orderdate) AS orderyear, custid
FROM Sales.Orders
WHERE empid=@emp_id
) AS derived_year
GROUP BY orderyear;

WITH CTE_year AS
(
SELECT YEAR(OrderDate) AS OrderYear, customerID
FROM Sales.SalesOrderHeader
)
SELECT orderyear, COUNT(DISTINCT CustomerID) AS CustCount
FROM CTE_year
GROUP BY OrderYear;

Common

SUM
MIN
MAX
AVG
COUNT
COUNT_BIG

Statistical

STDEV
STDEVP
VAR
VARP

Other
CHECKSUM_AGG
GROUPING
GROUPING_ID

Views are named tables expressions with definitions stored in a database


that can be referenced in a SELECT statement just like a table
Views are defined with a single SELECT statement and then saved in the
database as queries
Table-valued functions are created with the CREATE FUNCTION. They
contain a RETURN type of table

Derived tables allow you to write more modular queries


as named query expressions that are created within an outer SELECT
statement. They represent a virtual relational table so are not stored in
the database
CTEs are similar to derived tables in scope and naming requirements but
unlike derived tables, CTEs support multiple definitions, multiple
references, and recursion

Querying Microsoft SQL Server 2012 Jump Start


01 | Introducing SQL Server 2012
SQL Server types of statements; other SQL statement elements; basic SELECT statements

02 | Advanced SELECT Statements

DISTINCT, Aliases, scalar functions and CASE, using JOIN and MERGE; Filtering and sorting data, NULL
values

03 | SQL Server Data Types

Introduce data types, data type usage, converting data types, understanding SQL Server function types

04 | Grouping and Aggregating data

Aggregate functions, GROUP BY and HAVING clauses, subqueries; self-contained, correlated, and EXISTS; Views, inline-table
valued functions, and derived tables

| Lunch Break

Eat, drink, and recharge for the afternoon session

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, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

Você também pode gostar