22
In this session, you will learn to: Use functions to customize the result set Summarize and group data Objectives

In this session, you will learn to: Use functions to customize the result set Summarize and group data Objectives

Embed Size (px)

Citation preview

Page 1: In this session, you will learn to: Use functions to customize the result set Summarize and group data Objectives

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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