UNION Operator keywords Displays all rows from both the tables

Slides:



Advertisements
Similar presentations
Using the Set Operators
Advertisements

Haas MFE SAS Workshop Lecture 3:
SAS Programming:File Merging and Manipulation. Reading External Files (review) data barf; * create the dataset BARF; infile ’s:\mysas\Table7.1'; * open.
4 การใช้ SQL Functions. Copyright © 2007, Oracle. All rights reserved What Are Group Functions? Group functions operate on sets of rows to give.
1 Combining (with SQL) HRP223 – 2010 October 27, 2009 Copyright © Leland Stanford Junior University. All rights reserved. Warning: This presentation.
Introduction to SQL Session 2 Retrieving Data From Multiple Tables.
9 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Using the Set Operators Assist. Prof. Pongpisit Wuttidittachotti, Ph.D. Faculty.
PROC SQL – Select Codes To Master For Power Programming Codes and Examples from SAS.com Nethra Sambamoorthi, PhD Northwestern University Master of Science.
WRITING BASIC SQL SELECT STATEMENTS Lecture 7 1. Outlines  SQL SELECT statement  Capabilities of SELECT statements  Basic SELECT statement  Selecting.
11 Chapter 2: Basic Queries 2.1: Overview of the SQL Procedure 2.2: Specifying Columns 2.3: Specifying Rows.
Chapter 9 Joining Data from Multiple Tables
SQL Chapter Two. Overview Basic Structure Verifying Statements Specifying Columns Specifying Rows.
CS424 Relational Data Manipulation Relational Data Manipulation Relational tables are sets. Relational tables are sets. The rows of the tables can be considered.
SQL - Structured Query Language For database analyses.
Queries SELECT [DISTINCT] FROM ( { }| ),... [WHERE ] [GROUP BY [HAVING ]] [ORDER BY [ ],...]
15 Copyright © Oracle Corporation, All rights reserved. Using SET Operators.
SQL Select Statement IST359.
Copyright © 2004, Oracle. All rights reserved. Using the Set Operators.
Chapter 4: Combining Tables Vertically using PROC SQL 1 © Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.
Manipulating Data Lesson 3. Objectives Queries The SELECT query to retrieve or extract data from one table, how to retrieve or extract data by using.
Aggregating Data Using Group Functions. What Are Group Functions? Group functions operate on sets of rows to give one result per group.
11 Chapter 4: Subqueries 4.1: Noncorrelated Subqueries 4.2: Correlated Subqueries (Self-Study)
1 SQL Chapter 9 – 8 th edition With help from Chapter 2 – 10 th edition.
Writing Basic SQL SELECT Statements Lecture
Select Complex Queries Database Management Fundamentals LESSON 3.1b.
IFS180 Intro. to Data Management Chapter 10 - Unions.
A Guide to SQL, Seventh Edition
Chapter 6: Set Operators
Writing Basic SQL SELECT Statements
Displaying Query Results
Querying a Database Using the Select Query Window
Using the Set Operators
Basic Queries Specifying Columns
PROC SQL, Overview.
An Introduction to SQL.
SQL – Subqueries.
Writing Basic SQL SELECT Statements
Using the Set Operators
Match-Merge in the Data Step
Noncorrelated subquery
Correlated Subqueries
Aggregating Data Using Group Functions
A more complex example.
Integrity Constraints
Displaying Queries 2 Display a query’s results in a specified order.
Subsetting Rows with the WHERE clause
Combining (with SQL) HRP223 – 2013 October 30, 2013
Outer Joins Inner joins returned only matching rows. When you join tables, you might want to include nonmatching rows as well as matching rows.
Grouping Summary Results
Inner Joins.
Combining Data Sets in the DATA step.
Summarizing Data with Summary Functions
Writing Basic SQL SELECT Statements
5 The EXCEPT Operator Unique rows from the first result set that are not found in the second result set are selected.
Access/SQL Server Eliminate Duplicates with SELECT DISTINCT
Lab 3 and HRP259 Lab and Combining (with SQL)
Reporting Aggregated Data Using the Group Functions
The INTERSECT Operator
3 Specifying Rows.
Setting SQL Procedure Options
3 Views.
A new keyword -- calculated
Subqueries.
SQL set operators and modifiers.
Reporting Aggregated Data Using the Group Functions
Remerging Summary Values
Using the Set Operators
Reporting Aggregated Data Using the Group Functions
分组函数 Schedule: Timing Topic 35 minutes Lecture 40 minutes Practice
Manipulating Data Lesson 3.
Presentation transcript:

UNION Operator keywords Displays all rows from both the tables Removes duplicate records from the combined dataset keywords    ALL -- duplicate rows in the combined dataset.  CORR   -- PROC SQL matches the columns by name.

Remove nonmatching columns. UNION Operator UNION CORR ALL Remove duplicate rows. No Yes End Concatenate tables. Remove nonmatching columns.

data t1(drop=i) t2(drop=i rename=(z=w)); call streaminit(13453); do i= 1 to 3; x=int(rand("uniform")*5); z=int(rand("uniform")*5); output t1; output t2; end; do i= 4 to 6; run; data t1; set t1 end=done; output; if done then output; data t2; set t2 end=done; if _n_=1 then output; title "t1"; proc print data=t1 noobs;run; title "t2"; proc print data=t2 noobs;run; title;

/*union*/ proc sql; select * from t1 union from t2 ; quit;

/*reverse order of union*/ proc sql; select * from t2 union from t1 ; quit;

/*create a table from union*/ proc sql; create table tot1 as select * from t1 union from t2 ; quit; proc print data=tot1;run;

/*the corr option*/ proc sql; select * from t2 union corr from t1 ; quit;

/*the all option*/ proc sql; select * from t2 union all from t1 ; quit;

/*both corr and all*/ proc sql; select * from t2 union corr all from t1 ; quit;

Payroll Report for Level I, II, and III Employees Create a payroll report for the Level I, II, and III Orion Star employees. The orion.Staff table contains the job title and salary information for all Orion Star employees. Use the UNION set operator to combine the results from each query that calculates the total paid to all Level I, II, and III employees. Payroll Report for Level I, II, and III Employees _______________________________________ Total Paid to ALL Level I Staff 1,234,567 Total Paid to ALL Level II Staff 1,456,789 Total Paid to ALL Level III Staff 2,123,456

The UNION Operator proc sql; select 'Total Paid to ALL Level I Staff', sum(Salary) format=comma12. from orion.Staff where scan(Job_Title,-1, ' ')='I' union select 'Total Paid to ALL Level II Staff', where scan(Job_Title,-1,' ')='II' select 'Total Paid to ALL Level III Staff', where scan(Job_Title,-1,' ')='III'; quit; s106d11

Remove nonmatching columns. UNION Operator UNION CORR ALL Remove duplicate rows. No Yes End Concatenate tables. Remove nonmatching columns.