SQL Procedure User's Guide (Multiple Table)

Selecting Data from More than One Table by Using Joins

  Inner Joins: An Inner join returns only the subset of rows from the first table that matches rows from the second table You can specifies the columns that you want to be compared for matching values in a WHERE clause.

  Inner Joins... On: The Inner Join keywords can be used to join tables. The ON clause replaces the WHERE clause for specifying columns to join.

  The Effects of Null Values on Joins: PROC SQL treats null as missing values and as matches for joins.Any null will match with any other null of the same type in a join

  

Showing Relationships within a Single Table Using Self-Joins:

proc sql;
title "Cities' High Temps = Cities' Low Temps";
select High.City format $12., High.Country format $12.,
         High.AvgHigh, ' | ',
         Low.City format $12., Low.Country format $12.,
         Low.AvgLow
    from sql.worldtemps High, sql.worldtemps Low
    where High.AvgHigh = Low.AvgLow and
              High.city ne Low.city and
              High.country ne Low.country;

  Outer Joins: The Outer Joins are inner joins that are augmented with rows from one table that do not match any row from the other table in the join.The resulting output includes rows that match and rows that do not match from the join's source tables.Nonmatching rows have null values in the columns from the unmatched table.

  LEFT JOIN...ON...

  

  RIGHT JOIN...ON...

  FULL JOIN...ON...

 

  Specialty Joins: Three types of joins--> cross joins,union joins, and natural joins

    1. cross joins : Including All combinations of Rows with the cross join.

proc sql;
title 'Table One and Table Two';
select *
from one cross join two;

  2.Unoin Join: A unoin join combines two tables without attempting to match row. all columns and rows from both tables are include

  3.Natural Join : A natural join automatically selects columns from each table to use in determing matching rows. With a natural join,PROC SQL identifies columns in each table that have the same name and type,rows in which the values of these columns are equal are returned as matching rows.The ON clause is implied

 

Using Subqueries to Select Data

   1. Single-Value Subqueries:

proc sql;
title 'U.S. States with Population Greater than Belgium';
select Name 'State' , population format=comma10.
    from sql.unitedstates
        where population gt
            (select population from sql.countries
                     where name = "Belgium");        

  2.Multiple-Value Subqueries:

    It is used in a WHRER or HAVING expression that contains IN or a comparision operator that is modified by ANY or ALL.

libname sql 'SAS-library';
proc sql outobs=5;
title 'Populations of Major Oil Producing Countries';
select name 'Country', Population format=comma15.
    from sql.countries
        where Name in
            (select Country from sql.oilprod);    

  3. Correlated Subqueries:
    A correlated subquery requires a value or values to be passed to it by the outer query, After the subquery runs, it passes the results back to the outer query

proc sql;
title 'Oil Reserves of Countries in Africa';
select * from sql.oilrsrvs o
    where 'Africa' =
                (select Continent from sql.countries c
                        where c.Name = o.Country);

  4. Testing for the Existence of a Group of Values:

proc sql;
title 'Oil Reserves of Countries in Africa';
select * from sql.oilrsrvs o
    where exists
        (select Continent from sql.countries c
                    where o.Country = c.Name and
                    Continent = 'Africa');

Combining Queries with Set Operators:

  1. UNION : produces all unique rows from both queries.

  2. UNION ALL

  

  3 .EXCEPT:  produces rows that are part of the first query only

  4. INTERSECT: proudces rows that are common to both query results

  5. OUTER UNION: concatenates the query results.

  6.UNION Corr.

 

 

  

  

  

posted @ 2017-02-23 09:40  easy_wang  阅读(222)  评论(0编辑  收藏  举报