In this session, you will learn to: Use functions to customize the result set Summarize and group...
-
Upload
jocelyn-hines -
Category
Documents
-
view
214 -
download
0
Transcript of In this session, you will learn to: Use functions to customize the result set Summarize and group...
![Page 1: In this session, you will learn to: Use functions to customize the result set Summarize and group data Objectives.](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649f345503460f94c51ca6/html5/thumbnails/1.jpg)
In this session, you will learn to:Use functions to customize the result set
Summarize and group data
Objectives
![Page 2: In this session, you will learn to: Use functions to customize the result set Summarize and group data Objectives.](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649f345503460f94c51ca6/html5/thumbnails/2.jpg)
String functions:Can be used to manipulate string values in the result set
Can only be used with char and varchar data types
Syntax:
SELECT function_name (parameters)
Let’s see how…
Using String Functions
![Page 3: In this session, you will learn to: Use functions to customize the result set Summarize and group data Objectives.](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649f345503460f94c51ca6/html5/thumbnails/3.jpg)
Date functions:Can be used to manipulate datetime values, perform arithmetic operations, and perform date parsing
Let’s see how…
Using Date Functions
![Page 4: In this session, you will learn to: Use functions to customize the result set Summarize and group data Objectives.](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649f345503460f94c51ca6/html5/thumbnails/4.jpg)
Mathematical functions:Can be used to manipulate numeric values in a result set
Let’s see how…
Using Mathematical Functions
![Page 5: In this session, you will learn to: Use functions to customize the result set Summarize and group data Objectives.](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649f345503460f94c51ca6/html5/thumbnails/5.jpg)
Just a minute
Identify the utility of the datepart function.
Answer:The datepart function is used to extract different parts of a date value.
![Page 6: In this session, you will learn to: Use functions to customize the result set Summarize and group data Objectives.](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649f345503460f94c51ca6/html5/thumbnails/6.jpg)
Just a minute
The management of AdventureWorks wants to increase the shift time from 8 hours to 10 hours. Calculate the end time of the shifts based on their start time.
Answer:SELECT ShiftID, StartTime, 'EndTime' = dateadd(hh, 10, StartTime) FROM HumanResources.Shift
![Page 7: In this session, you will learn to: Use functions to customize the result set Summarize and group data Objectives.](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649f345503460f94c51ca6/html5/thumbnails/7.jpg)
Ranking functions:Can be used to generate sequential numbers for each row or to give a rank based on specific criteria
Supported by SQL Server are:row_number
rank
dense_rank
Let’s see how…
Using Ranking Functions
![Page 8: In this session, you will learn to: Use functions to customize the result set Summarize and group data Objectives.](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649f345503460f94c51ca6/html5/thumbnails/8.jpg)
System functions:Can be used to query system tables
Commonly used in SQL Server are:host_id
host_name
suser_id
Let’s see how…
Using System Functions
![Page 9: In this session, you will learn to: Use functions to customize the result set Summarize and group data Objectives.](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649f345503460f94c51ca6/html5/thumbnails/9.jpg)
Problem Statement:The management at AdventureWorks, Inc. wants to view a report that displays the employee ID, designation, and age of the employees who are working as a marketing manager or a marketing specialist. The data should be displayed in uppercase.
The employee details are stored in the Employee table in the AdventureWorks database. How will you display the required data?
Demo: Customizing the Result Set
![Page 10: In this session, you will learn to: Use functions to customize the result set Summarize and group data Objectives.](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649f345503460f94c51ca6/html5/thumbnails/10.jpg)
Solution:To solve the preceding problem, you need to perform the following tasks:
1. Create a query.
2. Execute the query to display data.
Demo: Customizing the Result Set (Contd.)
![Page 11: In this session, you will learn to: Use functions to customize the result set Summarize and group data Objectives.](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649f345503460f94c51ca6/html5/thumbnails/11.jpg)
Aggregate functions:Can be used to summarize values of a column based on a set of rows
Supported by SQL Server are:avg
count
min
max
sum
Let’s see how…
Summarizing Data by Using Aggregate Functions
![Page 12: In this session, you will learn to: Use functions to customize the result set Summarize and group data Objectives.](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649f345503460f94c51ca6/html5/thumbnails/12.jpg)
Just a minute
What would be the output of the following query?
SELECT 'Maximum Rate' = max (UnitPrice) FROM Sales.SalesOrderDetail
Answer:The query displays the maximum unit price from the SalesOrderDetail table.
![Page 13: In this session, you will learn to: Use functions to customize the result set Summarize and group data Objectives.](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649f345503460f94c51ca6/html5/thumbnails/13.jpg)
Data is grouped to generate summarized reports.
Clauses used to group data are:GROUP BY
COMPUTE
COMPUTE BY
PIVOT
Grouping Data
![Page 14: In this session, you will learn to: Use functions to customize the result set Summarize and group data Objectives.](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649f345503460f94c51ca6/html5/thumbnails/14.jpg)
GROUP BY:Summarizes the result set into groups as defined in the query by using aggregate functions
Uses the HAVING clause to eliminate all those groups that do not match the condition
Syntax:
SELECT column_list
FROM table_name
WHERE condition
[GROUP BY [ALL] expression [, expression]
[HAVING search_condition]
Let’s see how…
Grouping Data (Contd.)
![Page 15: In this session, you will learn to: Use functions to customize the result set Summarize and group data Objectives.](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649f345503460f94c51ca6/html5/thumbnails/15.jpg)
COMPUTE:Can be used to generate summary rows by using aggregate functions
COMPUTE BY:Can be used to calculate summary values of the result set on a group of data
The column on which the data is to be grouped is mentioned after the BY keyword.
Grouping Data (Contd.)
![Page 16: In this session, you will learn to: Use functions to customize the result set Summarize and group data Objectives.](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649f345503460f94c51ca6/html5/thumbnails/16.jpg)
Syntax:
SELECT column_list
FROM table_name
ORDER BY column_name
COMPUTE aggregate_function (column_name)
[, aggregate_function (column_name)...] [BY column_name [, column_name]...]
Let’s see how…
Grouping Data (Contd.)
![Page 17: In this session, you will learn to: Use functions to customize the result set Summarize and group data Objectives.](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649f345503460f94c51ca6/html5/thumbnails/17.jpg)
PIVOT:Is used to transform a set of columns into values
Syntax:
SELECT * FROM table_name
PIVOT (aggregation_function (value_column)
FOR pivot_column
IN (column_list)
) table_alias
Let’s see how…
Grouping Data (Contd.)
![Page 18: In this session, you will learn to: Use functions to customize the result set Summarize and group data Objectives.](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649f345503460f94c51ca6/html5/thumbnails/18.jpg)
Just a minute
When grouping data, which of the following clauses helps eliminate the groups that do not match the condition specified?1. NOT IN
2. HAVING
3. WHERE
4. COMPUTE
Answer:2. HAVING
![Page 19: In this session, you will learn to: Use functions to customize the result set Summarize and group data Objectives.](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649f345503460f94c51ca6/html5/thumbnails/19.jpg)
Match the column A and with column B.
Column A Column B
ALL Used by aggregate functions
PIVOT Returns zero or more values
IN Used for modifying comparison operator
> Relational Operator
Column A Column B
ALL Used for modifying comparison operator
PIVOT Relational Operator
IN Returns zero or more values
> Used by aggregate functions
Answer:
Just a minute
![Page 20: In this session, you will learn to: Use functions to customize the result set Summarize and group data Objectives.](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649f345503460f94c51ca6/html5/thumbnails/20.jpg)
Problem Statement:You are a database developer of AdventureWorks, Inc. The management wants to view the average quantity ordered for each product group. The data should be displayed in the descending order of ProductID.
The sales details are stored in the SalesOrderHeader and SalesOrderDetails tables in the AdventureWorks database. How will you generate this report?
Demo: Summarizing and Grouping Data
![Page 21: In this session, you will learn to: Use functions to customize the result set Summarize and group data Objectives.](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649f345503460f94c51ca6/html5/thumbnails/21.jpg)
Solution:To solve the preceding problem, you need to perform the following tasks:
1. Create a query.
2. Execute the query to verify the result.
Demo: Summarizing and Grouping Data (Contd.)
![Page 22: In this session, you will learn to: Use functions to customize the result set Summarize and group data Objectives.](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649f345503460f94c51ca6/html5/thumbnails/22.jpg)
In this session, you learned that:The string functions are used to format data in the result set.
The date functions are used to manipulate date values.
The mathematical functions are used to perform numerical operations.
The ranking functions are used to generate sequential numbers for each row or to give a rank based on specific criteria.
The system functions are used to query system tables.
The aggregate functions, such as avg, count, min, max, and sum are used to retrieve summarized data.
The GROUP BY and PIVOT clauses are used to group the result set.
The COMPUTE and COMPUTE BY clauses are used to calculate summarized values in a grouped result set.
Summary