Essential summarizer: Search to Summarize, News to Summarize and Tweet to Summarize search13
In this session, you will learn to: Use functions to customize the result set Summarize and group...
-
Upload
jocelyn-hines -
Category
Documents
-
view
215 -
download
0
Transcript of In this session, you will learn to: Use functions to customize the result set Summarize and group...
In this session, you will learn to:Use functions to customize the result set
Summarize and group data
Objectives
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
Date functions:Can be used to manipulate datetime values, perform arithmetic operations, and perform date parsing
Let’s see how…
Using Date Functions
Mathematical functions:Can be used to manipulate numeric values in a result set
Let’s see how…
Using Mathematical Functions
Just a minute
Identify the utility of the datepart function.
Answer:The datepart function is used to extract different parts of a date value.
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
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
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
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
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.)
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
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.
Data is grouped to generate summarized reports.
Clauses used to group data are:GROUP BY
COMPUTE
COMPUTE BY
PIVOT
Grouping Data
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.)
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.)
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.)
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.)
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
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
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
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.)
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