s q l chapter two

29
SQL Chapter Two

Upload: melody

Post on 25-Feb-2016

29 views

Category:

Documents


6 download

DESCRIPTION

S Q L Chapter Two. Overview. Basic Structure Verifying Statements Specifying Columns Specifying Rows  . Introduction SQL   is a modular language that uses statements and clauses.  . Basic structure of PROC SQL :. PROC SQL ; statement (select) - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: S Q L Chapter Two

SQL Chapter Two

 

Page 2: S Q L Chapter Two

Overview• Basic Structure• Verifying Statements • Specifying Columns• Specifying Rows 

Page 3: S Q L Chapter Two

Introduction 

SQL  is a modular language that uses statements and clauses.  

Page 4: S Q L Chapter Two

Basic structure of PROC SQL:

PROC SQL;           statement (select)           clauses (from, where, group by, having, order by);QUIT;     Note:  place semicolon at the end of the last clause only. 

Page 5: S Q L Chapter Two

Statements

select - specifies the columns to be selected Select statement has the following features:-selects data that meets certain conditions-groups data-specifies an order for the data-formats data-calculates new variables 

Page 6: S Q L Chapter Two

Clauses

from - specifies the tables to be queriedwhere - subsets the data based on a condition - optionalgroup by - classifies the data into groups - optionalhaving - subsets groups of data based on a group conditionorder by - sorts row by the values of specific columns Note:  the order of the clauses are significant. 

Page 7: S Q L Chapter Two

• Basic Structure• Verifying Statements • Specifying Columns• Specifying Rows 

Overview

Page 8: S Q L Chapter Two

Verifying Statements

Two functions that can be used to verify if your statement syntax are:  validate - used to check the select statement syntax noexec - checks for invalid syntax in all types of SQL statements

Page 9: S Q L Chapter Two

 Validate

proc sql;validateselect timemile, restpulse, maxpulsefrom project.fitnesswhere timemile gt 7; NOTE: PROC SQL statement has valid syntax.

proc sql;validateselect timemile, restpulse, maxpulse,from project.fitnesswhere timemile gt 7;

 Syntax error, expecting one of the following: a quoted string, !, !!, &...

Page 10: S Q L Chapter Two

NoExect

proc sql noexec;select  timemile, restpulse, maxpulsefrom  project.fitnesswhere  timemile gt 7; NOTE: Statement not executed due to NOEXEC option.

Page 11: S Q L Chapter Two

Contrasting

 Features of validate:-tests syntax of query without executing the query-checks the validity of column name-prints error messages for invalid queries-is only used for select statements 

Features of noexec:-Checks for invalid syntax in all types of SQL statements

Page 12: S Q L Chapter Two

Overview• Basic Structure• Verifying Statements • Specifying Columns• Specifying Rows 

Page 13: S Q L Chapter Two

Specifying Columns

Objectives-Displaying columns directly from a table -Displaying columns calculated from other columns -Calculating columns using a CASE expression

Page 14: S Q L Chapter Two

Displaying data from a table To print all of a table  columns in the order that they were stored, use an asterisk in the SELECT statement:

  PROC SQL;

SELECT * FROM VITALS; QUIT;

                            PATIENT     PULSE      TEMP       BPS       BPD                                                       101        72      98.5       130        88                              101        75      98.6       133        92                              101        74      98.5       136        90                              102        81        99       141        93                              102        77      98.7       144        97                              102        78      98.7       142        93                              103        77      98.3       137        79                              103        77      98.5       133        74                              103        78      98.6       140        80                              103        75      99.2       147        89                              104        72      98.8       128        83                              104        69      99.1       131        86                  

Page 15: S Q L Chapter Two

Printing Specify Columns If you do not want to print out all columns in a table in the order that they were stored, you can specify the columns to be printed in the order that you want them in the SELECT statement or CASE EXPRESSION in the select statement .

PROC SQL;CREATE TABLE TESTMED ASSELECT PATIENT,CASE ((PATIENT/2 = INT(PATIENT/2)) +(PATIENT = .))WHEN 1 THEN 'Med A'WHEN 0 THEN 'Med B'ELSE 'Error' END AS DOSEGRPLENGTH=5FROM VITALSORDER BY PATIENT;QUIT;

PATIENT    DOSEGRP   101       Med B   101       Med B      101       Med B   102       Med A   102       Med A   102       Med A   103       Med B   103       Med B   103       Med B   103       Med B   104       Med A   104       Med A   104       Med A  

Page 16: S Q L Chapter Two

Calculating ColumnsWe can calculate a new column by using data in an existing column and then naming the new column using the as function.

Calculate the proportion of Units form each country

    CODE:

OUTPUT:

Page 17: S Q L Chapter Two

Calculated columns using SAS Dates

  Recall from previous chapters in our SAS book that dates are stored in a different format when run through SAS.   We will then use these dates to calculate new columns.

Page 18: S Q L Chapter Two

Example: Calculate the range of dates in a Dailyprices dataset. 

CODE:

OUTPUT:

Page 19: S Q L Chapter Two

Creating new columnsThe use of CASE expression can be used to create a new column

CODE: OUTPUT:

                       

Page 20: S Q L Chapter Two

Creating a table To create and populate a table with the rows from an SQL query, use create table.  proc sql;create table states asselect state_code, state_namefrom d2data.state;quit;

 State_ Obs Code State_Name      99              UT       Utah               100             VT       Vermont            101              VA      Virginia            102             WA      Washington 103             WV      West Virginia       104             WI       Wisconsin  105             WY      Wyoming 106             N/A  

Page 21: S Q L Chapter Two

Overview• Basic Structure• Verifying Statements • Specifying Columns• Specifying Rows 

Page 22: S Q L Chapter Two

Specifying Rows in a table

Objectives -Selecting a subset of rows  -Removing duplicate rows -Subsetting using where clauses, escape clauses, and calculated values

Page 23: S Q L Chapter Two

Selecting a subset of rows

 proc sql;title 'large orders';   select Product_ID, total_retail_pricefrom d2data.order_item where total_retail_price > 1000; quit;

           Large orders                        Total Retail Price  Product ID       For This Product 240200100076      $1,796.00 240400200097      $1,250.40240100400043      $1,064.00 240200200013      $1,266.00 240300100032      $1,200.20240300300070      $1,514.40 230100700009      $1,687.50   230100700008      $1,542.60 240300300090      $1,561.80 230100700009      $1,136.20  230100200025      $1,103.60 240200100173      $1,937.20

Page 24: S Q L Chapter Two

Where clauseUse a where to specify a condition that data must fulfill before being selected.

CODE:

OUTPUT:

 Where clauses uses common comparisons (lt, gt, eq, etc) and logical operators (OR, Not, And, In, Is Null, ...). 

Page 25: S Q L Chapter Two

Removing duplicationsUse distinct keyword to eliminate duplications. CODE (without DISTINCT):                CODE (with DISTINCT):

OUTPUT: 

Page 26: S Q L Chapter Two

Escape ClauseThe escape clause allows you to designate a single character that will indicate how proc sql will interpret LIKE wildcards when SAS is searching within a character string.   

CODE:OUTPUT:

Example: Select observations from a string variable containing an underscore ('_').

Page 27: S Q L Chapter Two

Subsetting calculated values

Since the where clause is evaluated before the select, it's possible for an error to show up since the columns used in the where clause must exist in the table or be derived from an existing column.   There are two fixes for this, the first would be repeating the calculation in the where clause.  The alternative method would be using CALCULATED keyword to refer to an already calculated column in the select.

Page 28: S Q L Chapter Two

Subsetting calculated values

proc sql;title 'Lack of profit';   select Product_ID,          ((total_retail_price/quantity) - costprice_per_Unit) as profit      from d2data.order_item

   where calculated profit < 3;quit;title;

        Lack of profit            Product ID    profit               230100500045       0.7230100500068       0.9 240100100433      1.85240700200004         2 240200100021       1.5240100100031       2.4 240700200007       2.9240100100232       1.9230100500004      1.85230100500004      1.85240700100017     -1.41

Page 29: S Q L Chapter Two

SummaryBasic Structure • PROC SQL;

              statement (select)              clauses (from, where, group by, having, order by);    QUIT; Verifying Statements• validate - used to check the select statement syntax• noexec - checks for invalid syntax in all types of SQL statements

Specifying Columns• Displaying columns directly from a table•  Displaying columns calculated from other columns•  Calculating columns using a CASE expression

Specifying Rows • Selecting a subset of rows• Removing duplicate rows• Subsetting using where clauses, escape clauses, and calculated values