SAS SQL #2. Grouping Data Using GROUP BY Clause to group data by a column or columns

Similar documents
Chapter 9 Joining Data from Multiple Tables. Oracle 10g: SQL

Performing Queries Using PROC SQL (1)

Using SQL Queries to Insert, Update, Delete, and View Data: Joining Multiple Tables. Lesson C Objectives. Joining Multiple Tables

Introducing Microsoft SQL Server 2012 Getting Started with SQL Server Management Studio

Querying Microsoft SQL Server

Information Systems SQL. Nikolaj Popov

Effective Use of SQL in SAS Programming

SQL SELECT Query: Intermediate

Querying Microsoft SQL Server 2012

Relational Database: Additional Operations on Relations; SQL

Course 10774A: Querying Microsoft SQL Server 2012

ICAB4136B Use structured query language to create database structures and manipulate data

Course ID#: W 35 Hrs. Course Content

Course 10774A: Querying Microsoft SQL Server 2012 Length: 5 Days Published: May 25, 2012 Language(s): English Audience(s): IT Professionals

Querying Microsoft SQL Server 20461C; 5 days

MOC 20461C: Querying Microsoft SQL Server. Course Overview

Chapter 1 Overview of the SQL Procedure

Oracle SQL. Course Summary. Duration. Objectives

Querying Microsoft SQL Server Course M Day(s) 30:00 Hours

MOC QUERYING MICROSOFT SQL SERVER

Lab # 5. Retreiving Data from Multiple Tables. Eng. Alaa O Shama

Oracle Database 12c: Introduction to SQL Ed 1.1

Using SQL Queries in Crystal Reports

SQL Server for developers. murach's TRAINING & REFERENCE. Bryan Syverson. Mike Murach & Associates, Inc. Joel Murach

Duration Vendor Audience 5 Days Oracle End Users, Developers, Technical Consultants and Support Staff

Introduction to Microsoft Jet SQL

Join Example. Join Example Cart Prod Comprehensive Consulting Solutions, Inc.All rights reserved.

Oracle Database: SQL and PL/SQL Fundamentals

Displaying Data from Multiple Tables

Relational Division and SQL

Course 20461C: Querying Microsoft SQL Server Duration: 35 hours

Define terms Write single and multiple table SQL queries Write noncorrelated and correlated subqueries Define and use three types of joins

Advanced Query for Query Developers

Outline. SAS-seminar Proc SQL, the pass-through facility. What is SQL? What is a database? What is Proc SQL? What is SQL and what is a database

Oracle Database: SQL and PL/SQL Fundamentals NEW

Oracle Database 10g: Introduction to SQL

Querying Microsoft SQL Server 2012

Where? Originating Table Employees Departments

SUBQUERIES AND VIEWS. CS121: Introduction to Relational Database Systems Fall 2015 Lecture 6

Oracle Database: SQL and PL/SQL Fundamentals

Querying Microsoft SQL Server 2012

Introduction to Proc SQL Steven First, Systems Seminar Consultants, Madison, WI

Introduction to Querying & Reporting with SQL Server

SQL Programming. Student Workbook

Lecture 4: SQL Joins. Morgan C. Wang Department of Statistics University of Central Florida

Paper Merges and Joins Timothy J Harrington, Trilogy Consulting Corporation

Displaying Data from Multiple Tables. Copyright 2006, Oracle. All rights reserved.

Useful Business Analytics SQL operators and more Ajaykumar Gupte IBM

Introduction to SQL and SQL in R. LISA Short Courses Xinran Hu

Katie Minten Ronk, Steve First, David Beam Systems Seminar Consultants, Inc., Madison, WI

Advanced Subqueries In PROC SQL

Structured Query Language (SQL)

GET DATA FROM MULTIPLE TABLES QUESTIONS

Querying Microsoft SQL Server (20461) H8N61S

Querying Microsoft SQL Server Querying Microsoft SQL Server D. Course 10774A: Course Det ails. Co urse Outline

2874CD1EssentialSQL.qxd 6/25/01 3:06 PM Page 1 Essential SQL Copyright 2001 SYBEX, Inc., Alameda, CA

AV-005: Administering and Implementing a Data Warehouse with SQL Server 2014

WRITING EFFICIENT SQL. By Selene Bainum

Unit 3. Retrieving Data from Multiple Tables

Welcome to the topic on queries in SAP Business One.

Foundations & Fundamentals. A PROC SQL Primer. Matt Taylor, Carolina Analytical Consulting, LLC, Charlotte, NC

Five Little Known, But Highly Valuable, PROC SQL Programming Techniques. a presentation by Kirk Paul Lafler

More on SQL. Juliana Freire. Some slides adapted from J. Ullman, L. Delcambre, R. Ramakrishnan, G. Lindstrom and Silberschatz, Korth and Sudarshan

Paper TU_09. Proc SQL Tips and Techniques - How to get the most out of your queries

Displaying Data from Multiple Tables. Copyright 2004, Oracle. All rights reserved.

Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification

Advanced SQL - Subqueries and Complex Joins

SQL Server. 1. What is RDBMS?

Displaying Data from Multiple Tables. Copyright 2004, Oracle. All rights reserved.

Oracle Database 11g SQL

Netezza SQL Class Outline

CHAPTER 12. SQL Joins. Exam Objectives

SQL SUBQUERIES: Usage in Clinical Programming. Pavan Vemuri, PPD, Morrisville, NC

The join operation allows you to combine related rows of data found in two tables into a single result set.

MySQL for Beginners Ed 3

COMP 5138 Relational Database Management Systems. Week 5 : Basic SQL. Today s Agenda. Overview. Basic SQL Queries. Joins Queries

Programming with SQL

D61830GC30. MySQL for Developers. Summary. Introduction. Prerequisites. At Course completion After completing this course, students will be able to:

Introduction to Proc SQL Katie Minten Ronk, Systems Seminar Consultants, Madison, WI

A Closer Look at PROC SQL s FEEDBACK Option Kenneth W. Borowiak, PPD, Inc., Morrisville, NC

SQL Nested & Complex Queries. CS 377: Database Systems

RDBMS Using Oracle. Lecture Week 7 Introduction to Oracle 9i SQL Last Lecture. kamran.munir@gmail.com. Joining Tables

Alternatives to Merging SAS Data Sets But Be Careful

Handling Missing Values in the SQL Procedure

Information and Computer Science Department ICS 324 Database Systems Lab#11 SQL-Basic Query

Inquiry Formulas. student guide

Oracle 10g PL/SQL Training

Advanced SQL Queries for the EMF

Database CIS 340. lab#6. I.Arwa Najdi

Lecture Slides 4. SQL Summary. presented by Timothy Heron. Indexes. Creating an index. Mathematical functions, for single values and groups of values.

Relational Databases

Click to create a query in Design View. and click the Query Design button in the Queries group to create a new table in Design View.

Data Tool Platform SQL Development Tools

Relational Algebra. Query Languages Review. Operators. Select (σ), Project (π), Union ( ), Difference (-), Join: Natural (*) and Theta ( )

Microsoft Access 3: Understanding and Creating Queries

WORKING WITH SUBQUERY IN THE SQL PROCEDURE

Transcription:

SAS SQL #2 JC Wang Grouping Data Using GROUP BY Clause to group data by a column or columns GROUP BY input-column1 <,input-column2,...> usually used with an aggregate function in the SELECT clause or in a HAVING clause about how to summarize the data for each group PROC SQL calculates the aggregate function separately for each group when a GROUP BY clause is used without an aggregate function, PROC SQL treats the GROUP BY clause as if it were an ORDER BY clause and gives a warning message in the log that informs you that this has happened

Grouping by One Column Codes SELECT site, COUNT(*) Number of Subjects, SUM(visits) Total Visits FROM mylib.clinic GROUP BY site; Output Number of Total site Subjects Visits ------------------------ 01 61 127 02 66 130 03 46 90... more output lines... Grouping and Sorting Results Codes SELECT size Clinic Size, state State, COUNT(DISTINCT patient) Number Of Patients FROM mylib.master WHERE state IN ( MI, IN ) GROUP BY size, state ORDER BY size DESC, state; Output Number Clinic Of Size State Patients --------------------------- Small IN 105 Small MI 89 Medium IN 321 Medium MI 276 Large IN 1123 Large MI 756

Filtering Results by HAVING Clause similar to WHERE clause except that HAVING clause filters group return by the GROUP BY clause HAVING cond-involving-group by-variables usually follows GROUP BY to filter results from GROUP BY and use of aggregate function(s) if used without a GROUP BY clause, then it s treated as a WHERE clause and SAS issues a warning message is processed after the GROUP BY clause and any aggregate functions Filtering Results by HAVING Clause Codes SELECT site, COUNT(DISTINCT patient) AS npatients FROM mytable GROUP BY site HAVING npatients GT 14 ORDER BY npatients desc, site; Output site npatients --------------- 061 20 019 19 013 15 018 15 032 15... more output...

Validating a Query VALIDATE query; Upon submitting the query, PROC SQL print a message to SAS log indicating whether the syntax is correct. Creating a Table from a Query CREATE TABLE table AS query; Note that data set options are allowed in the table the query works on.

Joins A join is a query that select data from two or more tables/views, including self-join in which an alias can be considered as different table. Cartesian product Inner join Self join Left (outer) join Right (outer) join Full (outer) join Specialty joins: cross join (=cartesian product), union join, natural join Cartesian Product SELECT FROM table-1, table-2; resulted table size = product of input table sizes subset of the castesian product is usually desired ( two types: inner joins and outer joins) table_1 x y ------- x11 y11 x21 y21 x31 y31 table_2 x z ------- x12 z12 x22 z22 Output table x y x z ----------------- x11 y11 x12 z12 x21 y21 x12 z12 x31 y31 x12 z12 x11 y11 x22 z22 x21 y21 x22 z22 x31 y31 x22 z22

Syntax of Inner Join SELECT FROM table-1, table-2 WHERE <table-1.>col-1=<table-2.>col-2; or SELECT FROM table-1 INNER JOIN table-2 ON <table-1.>col-1=<table-2.>col-2; Note: the condition in the WHERE clause above is only an example. Using Table Alias FROM table-1 <AS> alias-1, table-2 <AS> alias-2 to avoid referencing a table with (one- or two- word) name all the time, a shorter (one-word) table alias can be used when referencing table using alias in any part of the query (SELECT, WHERE,...), use alias.col (when identical column name occurs from distinct tables)

Sorting Results from Joins SELECT FROM table-1, table-2 WHERE <table-1.>col-1=<table-2.>col-2 ORDER BY column(s); where column(s): comma-separated list of columns keyword DESC can follow a column if needed Self-Joins FROM table-1 <AS> alias-1, table-1 <AS> alias-2 A self-join is also called a reflexive join is used to show comparative relationships between values in a table

Types of Outer Joins left join right join full join Left Joins SELECT input-columns FROM table-1 LEFT JOIN table-2; ON matching-condition(s); list matched rows and non-matched rows from left table (table-1)

Right Joins SELECT input-columns FROM table-1 RIGHT JOIN table-2; ON matching-condition(s); list matched rows and non-matched rows from right table (table-2) Full Joins SELECT input-columns FROM table-1 FULL JOIN table-2; ON matching-condition(s); list matched rows and non-matched rows from both tables

Specialty Joins The specialty joins are special cases of the standard joins: CROSS JOIN = cartesian product UNION JOIN = queries (tables) combined with OUTER UNION operator, does not attempt to match rows NATURAL JOIN = cartesian product if no column with common name and type from two tables; NATURAL JOIN = inner join if at least one column with a common name and type from two tables (ON clause implied and not specified). a WHERE clause can be used to limit output Subqueries A subquery, also known as inner query (enclosed in parentheses) selects rows from one table based on values in another; is a query-expression that is nested as part of another query-expression; depending on the clause that contains it, can return a single value or multiple values; is most often used in the WHERE and the HAVING expressions.

Combining Queries with set operators query-1 set-operator <keyword> query-2; as a single statement; where set-operator is one of UNION (all unique rows) EXCEPT (rows from first query only) INTERSECT (common rows from both queries) OUTER UNION (concatenates query outputs) keyword is one of ALL (not suppressing duplicated rows, making one pass only) CORR CORRESPONDING (overlaying columns with common names) Schematic View of Set Operators

Set Symmetric Difference (query1 EXCEPT query2) UNION (query2 EXCEPT query1); as a single statement