Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "In this session, you will learn to: Use functions to customize the result set Summarize and group data Objectives."— Presentation transcript:

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

2 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

3 Date functions: Can be used to manipulate datetime values, perform arithmetic operations, and perform date parsing Let’s see how… Using Date Functions

4 Mathematical functions: Can be used to manipulate numeric values in a result set Let’s see how… Using Mathematical Functions

5 Just a minute Identify the utility of the datepart function. Answer: The datepart function is used to extract different parts of a date value.

6 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

7 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

8 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

9 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

10 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.)

11 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

12 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.

13 Data is grouped to generate summarized reports. Clauses used to group data are: GROUP BY COMPUTE COMPUTE BY PIVOT Grouping Data

14 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.)

15 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.)

16 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.)

17 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.)

18 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

19 Match the column A and with column B. Column AColumn B ALLUsed by aggregate functions PIVOTReturns zero or more values INUsed for modifying comparison operator >Relational Operator Column AColumn B ALLUsed for modifying comparison operator PIVOTRelational Operator INReturns zero or more values >Used by aggregate functions Answer: Just a minute

20 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

21 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.)

22 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


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

Similar presentations


Ads by Google