Mastering SQL Queries: Tips and Techniques for Effective Data Retrieval

displaying queries n.w
1 / 12
Embed
Share

Explore advanced SQL query techniques with examples to order, sort, and enhance query results in SAS. Learn how to display, order, and produce reports efficiently in SQL while leveraging SAS formats and labels for customized output.

  • SQL
  • Query Optimization
  • Data Retrieval
  • SAS Formats
  • Report Generation

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Displaying Queries 1

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

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

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

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

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

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

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

  9. 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 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 2 desc ; quit quit; sql; 9

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

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

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

Related


More Related Content