Documente Academic
Documente Profesional
Documente Cultură
The select list of a SELECT statement A COMPUTE or COMPUTE BY clause A HAVING clause
USE AdventureWorks USE AdventureWorks SELECT AVG(VacationHours)AS MAX(TaxRate) 'AverageVacationHours', SUM SELECT COUNT(*) (SickLeaveHours) AS 'TotalSickLeaveHours Sales.SalesTaxRate FROM Sales.SalesPerson FROM HumanResources.Employee GROUP SalesQuota WHERE BY TaxType;> 25000; WHERE Title LIKE 'Vice President%'
NULL values may produce unexpected or incorrect results Use the ISNULL function to correct this issue USE AdventureWorks SELECT AVG(ISNULL(Weight,0)) AS AvgWeight FROM Production.Product
The COUNT(*) function is an exception and returns the total number of records in a table
Deploy custom developed CLR assemblies within the SQL Server Process Leverage the .NET 2.0 API
Can be created using any .NET language such as C# and VB CREATE ASSEMBLY StringUtilities FROM PathToAssembly\StringUtilities.dll' WITH PERMISSION_SET=SAFE; CREATE AGGREGATE Concatenate(@input nvarchar(4000)) RETURNS nvarchar(4000) EXTERNAL NAME [StringUtilities].[Concatenate]
common aggregate functions in SQL Server 2008. The Human Resources (HR) department of Adventure Works requires information about employees. This would be an excellent situation to show the usage of several aggregate functions.
BY
Examining How the ROLLUP and CUBE Operators Work Using the ROLLUP and CUBE Operators Using the COMPUTE and COMPUTE BY Clauses Building a Query for Summarizing Grouped Data -
COMPUTE
1
2 3 4
23761
45791 75909 19900
Source Table
Group 1
Group 2
FROM Sales.SalesOrderDetail
GROUP BY SalesOrderID HAVING SUM(LineTotal) > 100000.00
SalesOrderID 43875
SubTotal 121761.939600
43884
44518 44528 44530 44795 46066 ...
115696.331324
126198.336168 108783.587200 104958.806836 104111.515642 100378.907800
ORDER BY SalesOrderID
Bellevue
Berlin Bordeaux Bothell Calgary Cambridge ...
35
1 1 22 1 2
Year
2003
2004
54307615.0868
32196912.4165
query
for all combination of values in selected columns SELECT a, b, c, SUM (<expression>) FROM T GROUP BY CUBE (a,b,c)
4 ProductID
1
A Shelf
A A A A A B B B B B
900 QtySum
761 791 909 900 3361 324 318 443 442 1507
SELECT ProductID,Shelf,SUM(Quantity) AS QtySum FROM Production.ProductInventory WHERE ProductID<6 GROUP BY CUBE(ProductID, Shelf)
2 3 4 NULL 1 2 3 4 NULL
CUBE operator extensions of the GROUP BY SELECT clause. You have been asked to summarize some information in the AdventureWorks database. This would be an excellent usage of these new operators.
relational format
same query
sum
185
Sum
1635
COMPUTE BY
SELECT SalesPersonID, CustomerID, OrderDate, SubTotal, TotalDue FROM Sales.SalesOrderHeader ORDER BY SalesPersonID, OrderDate COMPUTE SUM(SubTotal), SUM(TotalDue) BY SalesPersonID
Region
Country
Store
NULL
NULL NULL
NULL
NULL NULL
NULL
NULL NULL
NULL
NULL NULL NULL Europe
NULL
NULL DE FR NULL
Functionality
What s Ranking?
Ranking computes a value for each row in user-specified
set of rows.
Ranking functions include: RANK DENSE_RANK NTILE ROW_NUMBER Can be used for: Arbitrary row numbering Product Popularity Best Customers
159.00
9.99 8.99
Bike Stands
Bottles and Cages Bottles and Cages
1
1 2
Product HL Mountain Handlebars HL Road Handlebars HL Touring Handlebars ML Mountain Handlebars HL Headset ML Headset LL Headset
PriceRank 1 1 2 3 1 2 3
Product Patch Kit/8 Patches Road Tire Tube Road-750 Black, 44 Road-750 Black, 44
PriceBand Category
Product
ListPrice
1
1 2 2 3 3 1 1 2
Accessories Patch
Accessories Road Accessories LL Accessories ML
Kit/8 Patches
Tire Tube Road Tire Road Tire
2.29
3.99 21.49 24.99 34.99 35.00 539.99 782.99 782.99
Accessories Sport-100 Helmet, Blue Accessories HL Bikes Bikes Bikes ... Mountain Tire Road-750 Black, 44 Road-650 Red, 48 Road-650 Red, 52
Feature
RANK
DENSE_RANK
NTILE
ROW_NUMBER
2008
Bike Chain 8 3 2 7
Lisa
Chain
Lisa
Chain
Mike
Lisa Lisa
Bike
Bike Chain
5
3 3
Mike Lisa
8 3
2 7
Lisa
Chain
Lisa
Lisa
Bike
Chain
3
7
ROLLUP Operator
CUBE Operator
GROUPING_ID Function
Functions
Exercise 2: Summarizing Grouped Data Exercise 3: Ranking Grouped Data Exercise 4: Creating Crosstab Queries
Logon information
Lab Scenario
You are the database administrator of Adventure Works.
The HR department wants you to prepare a report on the number of vacation hours of the vice presidents of the company, and another report on the number of employees with incomplete addresses. The warehouse manager requires three reports on the average number of hours taken to manufacture a product, the order quantity and the price list of each product, and the safety stock level for red, blue, and black helmets. Besides these requests, the marketing manager wants you to prepare a report on the year-to-date sales of salespersons and rank their names based on their performance.
Lab Review
When you execute queries with aggregate functions you
may see a warning message Null value is eliminated by an aggregate or other SET operation. Why do you get this?
xyz is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause. What did you forget to do? on their year to date sales, were the amount of results in each category even? Why or why not?