1 chapter 4: creating simple queries 4.1 introduction to the query task 4.2 selecting columns and...

Download 1 Chapter 4: Creating Simple Queries 4.1 Introduction to the Query Task 4.2 Selecting Columns and Filtering Rows 4.3 Creating New Columns with an Expression

If you can't read please download the document

Upload: marylou-moore

Post on 13-Dec-2015

216 views

Category:

Documents


1 download

TRANSCRIPT

  • Slide 1

1 Chapter 4: Creating Simple Queries 4.1 Introduction to the Query Task 4.2 Selecting Columns and Filtering Rows 4.3 Creating New Columns with an Expression 4.4 Creating New Columns by Recoding Values 4.5 Joining Tables 4.6 Joining Tables Including Non-Matching Rows (Self-Study) Slide 2 2 Chapter 4: Creating Simple Queries 4.1 Introduction to the Query Task 4.2 Selecting Columns and Filtering Rows 4.3 Creating New Columns with an Expression 4.4 Creating New Columns by Recoding Values 4.5 Joining Tables 4.6 Joining Tables Including Non-Matching Rows (Self-Study) Slide 3 3 Objectives State the function of the query task. Name the areas within the Query Builder dialog. State the function of the areas within the Query Builder dialog. Slide 4 4 Query Builder The Query Builder is used to extract data from one or more tables according to the criteria specified by the user. Slide 5 5 Query Builder: Select Data The Select Data tab is used to select which columns are included and what their roles are in the query. Slide 6 6 Query Builder: Filter Data The Filter Data tab is used to retrieve rows that meet certain criteria, based on existing or calculated columns. Slide 7 7 Query Builder: Computed Columns The Computed Columns button is used to add new columns, either by building an expression or recoding an existing column. Slide 8 8 Query Builder: Tables and Joins The Tables and Joins window is used to join multiple tables based on a relationship between columns. Slide 9 9 Query Builder: Parameters The Parameters button enables you to define a query that returns different results based on values you provide at runtime. Slide 10 10 Chapter 4: Creating Simple Queries 4.1 Introduction to the Query Task 4.2 Selecting Columns and Filtering Rows 4.3 Creating New Columns with an Expression 4.4 Creating New Columns by Recoding Values 4.5 Joining Tables 4.6 Joining Tables Including Non-Matching Rows (Self-Study) Slide 11 11 Objectives Apply a filter in a query. Exclude columns in a query. Reorder columns in a query. Modify a columns properties. Slide 12 12 Selecting Both Rows and Columns Slide 13 13 Selecting Columns By default, a query starts out empty. You must add any columns to be included in the query to the Select Data tab. Drag and drop or right- click to add columns to the Select Data tab. Modify a columns properties. Slide 14 14 Filtering Rows The process of specifying which rows to retrieve in a query is called setting a filter and is done in the Filter Data tab. 2) Drop the column in here. 1) Drag a column from here. Slide 15 15 Setting a Filter When a column is dropped on the Filter Data tab, the Edit Filter dialog opens. Type the value(s) or expression. Choose the type of comparison. Slide 16 16 Setting an Advanced Filter Select New Advanced Filter... to open the Advanced Expression Editor and create a new filter. Create an advanced filter to build your own condition. Slide 17 17 Advanced Expression Editor: Functions 1) Select the type of function. 2) Select a specific function. Slide 18 18 Advanced Expression Editor: Data 1) Select a column to view distinct values. 2) Select values to include in the expression. Slide 19 19 Combining Filters By default, multiple filters are combined with AND. The Filter Combination window can be used to further customize the way multiple filters are evaluated. Slide 20 20 Final Results: Selecting Rows and Columns Slide 21 21 Using Query Results in Tasks Tables generated from queries can serve as the active data source for follow-up tasks. Slide 22 22 Scenario LLB wants to determine how many non-U.S. companies supply products to LLB. The report should reflect the following: the total number of suppliers in each country, excluding the United States countries with the most suppliers listed first Slide 23 23 Scenario Partial Output Slide 24 24 This demonstration illustrates how to set a filter and exclude columns in the Query Builder task. Selecting Columns and Filtering Rows Slide 25 25 Chapter 4: Creating Simple Queries 4.1 Introduction to the Query Task 4.2 Selecting Columns and Filtering Rows 4.3 Creating New Columns with an Expression 4.4 Creating New Columns by Recoding Values 4.5 Joining Tables 4.6 Joining Tables Including Non-Matching Rows (Self-Study) Slide 26 26 Objectives Define a new column of data in a query by building an expression. Slide 27 27 Computing a New Column Revenue = Quantity * SellPrice Slide 28 28 Computed Columns Create new columns. Slide 29 29 Advanced Expression Editor 1) Choose a method for computing the new column. 2) Use the Advanced Expression Editor to build the expression. Slide 30 30 Computed columns appear in the left pane and can be used in a filter on the Filter Data tab or as an input to another computed column. New Computed Columns Slide 31 31 Scenario LLB wants to know the projected inventory of each product at any given moment. The products table is constantly changing with daily updates for the number of units in stock and the number of units on order. ProjectedInventory = UnitsInStock + UnitsOnOrder Slide 32 32 This demonstration illustrates how to use the Advanced Expression Editor to define a column of data in the Query Builder task. Creating a Column with an Expression Slide 33 33 Chapter 4: Creating Simple Queries 4.1 Introduction to the Query Task 4.2 Selecting Columns and Filtering Rows 4.3 Creating New Columns with an Expression 4.4 Creating New Columns by Recoding Values 4.5 Joining Tables 4.6 Joining Tables Including Non-Matching Rows (Self-Study) Slide 34 34 Objectives Recode individual values or a range of values in a column. Slide 35 35 Recoded Columns New columns can also be calculated by recoding values from an existing column. Slide 36 36 Recoded Values Recoding a column enables you to assign a value to a new column based on the value of an existing column. When State='CA' Then Region = 'West' TRUE Then Region = 'Central' TRUE Then Region = 'East' TRUE When State='NY' FALSE When State='MI' FALSE Slide 37 37 Recode a Column When you recode a column, a prompt will appear to select the existing column to be used to generate the new column. Slide 38 38 Specify a Replacement 2) Use Replace Values or Replace a Range to map the recoded values to the original data. 1) Select Add to assign the new recoded values. Slide 39 39 Recoding a Column Assign values for all other cases. Select the appropriate type for the new column. Slide 40 40 Using the New Recoded Column The new recoded column is automatically included in the Select Data tab. The new column can also be used in the Filter Data or Sort Data tabs as well. To sort by a column, drag the column into the Sort Data tab and select the Sort Direction. Slide 41 41 Scenario LLB wants to know how the unit costs for the products are distributed within each food category. Rank each product into three unique groups based on the unit cost: $0 to $14.99, $15 to $29.99, and $30 and Above. Slide 42 42 Partial Table Analysis Results Slide 43 43 This demonstration illustrates the use of the Replace Values tab in a query to create a new column based conditionally upon an existing column. Creating a New Column by Recoding Values Slide 44 44 Chapter 4: Creating Simple Queries 4.1 Introduction to the Query Task 4.2 Selecting Columns and Filtering Rows 4.3 Creating New Columns with an Expression 4.4 Creating New Columns by Recoding Values 4.5 Joining Tables 4.6 Joining Tables Including Non-Matching Rows (Self-Study) Slide 45 45 Objectives Join multiple tables by common columns. Include only matching rows. Slide 46 46 Joining Tables Joining tables enables you to extract and process data from more than one table at a time. Slide 47 47 Joining Tables Matching rows only Slide 48 48 Joining Tables Matching rows only Slide 49 49 Adding Tables Select Add Tables to include additional data sources in the query. Slide 50 50 Tables and Joins Window 1) Select Join to access the Tables and Joins window. 2) Use the Tables and Joins window to specify how multiple tables relate. Slide 51 51 Modify Join Window The join criteria can be modified to include non-matching rows from either or both tables. Slide 52 52 Query Builder: Selecting a Server Use the Result Options window to specify the SAS server that should be used to execute the query. Slide 53 53 Join Results From USCustomerDatabaseLong From ItemsOrdered Slide 54 54 Scenario LLB wants to compute the total profits generated from each order. The profit that LLB earns from each order can be calculated as the difference between the selling price and the unit cost multiplied by the quantity of items ordered. LLB stores this quarter's information about its suppliers, products, and orders in three separate tables. Slide 55 55 Scenario: Separate Tables Slide 56 56 Scenario: Joined Table with New Column Profit = (SellPrice UnitCost) * Quantity Slide 57 57 This demonstration illustrates how to join multiple tables and store the result in a data table. Joining Tables Slide 58 58 Complete the Chapter 4 exercises starting on page 4-68. Detailed solutions are provided starting on page 4-73. Exercises Slide 59 59 Chapter 4: Creating Simple Queries 4.1 Introduction to the Query Task 4.2 Selecting Columns and Filtering Rows 4.3 Creating New Columns with an Expression 4.4 Creating New Columns by Recoding Values 4.5 Joining Tables 4.6 Joining Tables Including Non-Matching Rows (Self-Study) Slide 60 60 Objectives Perform different join types. Slide 61 61 Joining Tables Types of Joins: Matching Rows Only (SAS Enterprise Guide default) produces results where only the rows from one table that have a corresponding match in every other table are returned. All Rows from one or both tables produces results where all of the matched rows from both tables and the unmatched rows from at least one table are returned. All Rows from AAll Rows from A and BAll Rows from B A B Slide 62 62 Review: Matching Rows Only Slide 63 63 Including Non-Matching Rows All rows from CustomerDatabase and ItemsOrdered Slide 64 64 Including Non-Matching Rows All rows from CustomerDatabase Slide 65 65 Including Non-Matching Rows All rows from ItemsOrdered Slide 66 66 Review: Modify Join Window The join criteria can be modified to include non-matching rows from either or both tables. Slide 67 67 Modifying a Join: Results Results include all orders that are in the ItemsOrdered table, whether customer information in the CustomerDatabase table exists or not. Slide 68 68 Isolating Non-Matching Rows The query can also include a filter to isolate the non-matching rows from one or both tables. Orders placed by customers who do not exist in the CustomerDatabase table Slide 69 69 Scenario When products are discontinued, they are removed from the products table. LLB wants to generate a query on a regular basis to identify orders placed on discontinued products. Slide 70 70 This demonstration illustrates how to change the join type to include non-matching rows in a query. Joining Tables Including Non-Matching Rows Slide 71 71 Complete Exercise 6 starting on page 4-71. Detailed solutions are provided starting on page 4-82. Exercises