Displaying Queries 2 Display a query’s results in a specified order.

Slides:



Advertisements
Similar presentations
SQL – Lesson II Grade 12.
Advertisements

Sorting Rows. 2 home back first prev next last What Will I Learn? In this lesson, you will learn to: –Construct a query to sort a results set in ascending.
Performing Queries Using PROC SQL Chapter 1 1 ©Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.
Writing Basic SQL SELECT Statements. Capabilities of SQL SELECT Statements A SELECT statement retrieves information from the database. Using a SELECT.
11 Chapter 3: Displaying Query Results 3.1 Presenting Data 3.2 Summarizing Data.
Restricting and Sorting Data. Consider the table employee(employee_id,last_name,job_id, department_id ) assume that you want to display all the employees.
2 Copyright © 2004, Oracle. All rights reserved. Restricting and Sorting Data.
Sorting data and Other selection Techniques Ordering data results Allows us to view our data in a more meaningful way. Rather than just a list of raw.
Ceng 356-Lab2. Objectives After completing this lesson, you should be able to do the following: Limit the rows that are retrieved by a query Sort the.
1 Copyright © Oracle Corporation, All rights reserved. Writing Basic SQL SELECT Statements.
15 Structured Query Language (SQL). 2 Objectives After completing this section, you should be able to: Understand Structured Query Language (SQL) and.
11 Chapter 2: Basic Queries 2.1: Overview of the SQL Procedure 2.2: Specifying Columns 2.3: Specifying Rows.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
2 Copyright © 2004, Oracle. All rights reserved. Restricting and Sorting Data.
Copyright © 2004, Oracle. All rights reserved. Retrieving Data Using the SQL SELECT Statement Satrio Agung Wicaksono, S.Kom., M.Kom.
Copyright © 2004, Oracle. All rights reserved. Lecture 4: 1-Retrieving Data Using the SQL SELECT Statement 2-Restricting and Sorting Data Lecture 4: 1-Retrieving.
Structured Query Language
1 Querying a Single Table Structured Query Language (SQL) - Part II.
Queries SELECT [DISTINCT] FROM ( { }| ),... [WHERE ] [GROUP BY [HAVING ]] [ORDER BY [ ],...]
2 Copyright © 2004, Oracle. All rights reserved. Restricting and Sorting Data.
15 Copyright © Oracle Corporation, All rights reserved. Using SET Operators.
Copyright © 2004, Oracle. All rights reserved. Using the Set Operators.
Working with Columns, Characters, and Rows. 2 home back first prev next last What Will I Learn? In this lesson, you will learn to: –Apply the concatenation.
2 Copyright © 2009, Oracle. All rights reserved. Restricting and Sorting Data.
ORDER BY clause in SELECT command: Normally, the result of the query will not be in ordered format. If we want to get the result of the query in specific.
Simple Queries DBS301 – Week 1. Objectives Basic SELECT statement Computed columns Aliases Concatenation operator Use of DISTINCT to eliminate duplicates.
Writing Basic SQL SELECT Statements Lecture
ADVANCED SQL.  The SQL ORDER BY Keyword  The ORDER BY keyword is used to sort the result-set by one or more columns.  The ORDER BY keyword sorts the.
Limiting Selected Rows. 2-2 Objectives Sort row output using the ORDER BY clause. Sort row output using the ORDER BY clause. Enter search criteria using.
Oracle 10g Retrieving Data Using the SQL SELECT Statement.
Session 1 Retrieving Data From a Single Table
Restricting and Sorting Data
Retrieving Data Using the SQL SELECT Statement
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha
Chapter 6: Set Operators
Writing Basic SQL SELECT Statements
Displaying Query Results
Basic select statement
ATS Application Programming: Java Programming
Using the Set Operators
Basic Queries Specifying Columns
PROC SQL, Overview.
Restricting and Sorting Data
Creating Macro Variables in SQL (Review)
Writing Basic SQL SELECT Statements
Prof: Dr. Shu-Ching Chen TA: Yimin Yang
Noncorrelated subquery
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha
Chapter 3 The DATA DIVISION.
Restricting and Sorting Data
Subsetting Rows with the WHERE clause
Outer Joins Inner joins returned only matching rows. When you join tables, you might want to include nonmatching rows as well as matching rows.
Prof: Dr. Shu-Ching Chen TA: Haiman Tian
Grouping Summary Results
Summarizing Data with Summary Functions
Writing Basic SQL SELECT Statements
Restricting and Sorting Data
Writing Basic SQL SELECT Statements
The INTERSECT Operator
3 Specifying Rows.
Setting SQL Procedure Options
3 Views.
A new keyword -- calculated
Restricting and Sorting Data
UNION Operator keywords Displays all rows from both the tables
Remerging Summary Values
Using the Set Operators
‘ORDER BY’ Order by clause allows sorting of query results by one or more columns. Sorting can be done in ascending or descending. Default order is ascending.
Restricting and Sorting Data
Lab 2: Retrieving Data from the Database
Presentation transcript:

Displaying Queries 2 Display a query’s results in a specified order. Use SAS formats, labels, and titles to enhance the appearance and usability of a query’s output.

Ordering Data From the orion.Employee_payroll table, list the employee ID and salary of all employees hired prior to January 1, 1979 proc sql; select Employee_ID, Salary from orion.Employee_payroll where Employee_Hire_Date < '01JAN1979'd ; quit;

Ordering Data Use the ORDER BY clause to sort query results in a specific order. Ascending order (No keyword; this is the default.) Descending order (by following the column name with the DESC keyword)

Ordering Data In an ORDER BY clause, order the query results by specifying: Any column name from any table in the FROM clause, even if the column is not in the SELECT list A column name or a number representing the position of an item in the SELECT list An expression A combination of any of the above, with individual items separated by commas

From the orion.Employee_payroll table, list the employee ID and salary of all employees hired prior to January 1, 1979, in descending salary order. proc sql; select Employee_ID, Salary from orion.Employee_payroll where Employee_Hire_Date < '01JAN1979'd order by Salary desc; quit;

Producing an Ordered Report Sort output in descending order of amount and then by employee ID. proc sql; select Employee_ID, max(Qtr1,Qtr2,Qtr3,Qtr4) from orion.Employee_donations where Paid_By="Cash or Check" order by 2 desc, Employee_ID; quit;

Enhancing Query Output You can use SAS formats and labels to customize PROC SQL output. In the SELECT list, after the column name, but before the commas that separate the columns, you can include: Text in quotation marks (ANSI) or the LABEL= column modifier (SAS enhancement) to alter the column heading The FORMAT= column modifier to alter the appearance of the values in that column

Prepare a report that shows the employee ID of each Orion Star employee who makes charitable donations, lists the amount of the highest quarterly donation and the recipients. Rows should be sorted first in descending order of amount, and then by employee ID.

Enhancing Query Output You can enhance a report by displaying column labels instead of variable names, and formatting cash amounts with dollar signs and commas. proc sql; select Employee_ID label="Employee Identifier", sum(Qtr1,Qtr2,Qtr3,Qtr4) "Annual Donation" format=dollar7.2, Recipients from orion.Employee_donations where Paid_By="Cash or Check" order by 2 desc ; quit;

Produce a report of bonus values for all active employees Produce a report of bonus values for all active employees. Bonuses are 5% of salary. Employee_ID fixed text Salary*0.05

Enhancing Query Output You can also enhance the appearance of the query output by defining a new column containing the same constant character value for every row using SAS titles and footnotes Use a combination of these techniques to produce the Annual Bonuses for Active Employees report.

Enhancing Query Output proc sql; title 'Annual Bonuses for Active Employees'; select Employee_ID label='Employee Number', 'Bonus is:', Salary *.05 format=comma12.2 from orion.Employee_Payroll where Employee_Term_Date is missing order by Salary desc ; quit; title; s103d04