Previous Page | Next Page

The SQL Procedure

Example 9: Joining Three Tables


Procedure features:

FROM clause

joined-table component

WHERE clause

Tables: PROCLIB.STAFF2, PROCLIB.SCHEDULE2, PROCLIB.SUPERV2

This example joins three tables and produces a report that contains columns from each table.


Input Tables

                                 PROCLIB.STAFF2

  Id
  Num   Lname            Fname            City             State  Hphone
  ----------------------------------------------------------------------------
  1106  MARSHBURN        JASPER           STAMFORD         CT     203/781-1457
  1430  DABROWSKI        SANDRA           BRIDGEPORT       CT     203/675-1647
  1118  DENNIS           ROGER            NEW YORK         NY     718/383-1122
  1126  KIMANI           ANNE             NEW YORK         NY     212/586-1229
  1402  BLALOCK          RALPH            NEW YORK         NY     718/384-2849
  1882  TUCKER           ALAN             NEW YORK         NY     718/384-0216
  1479  BALLETTI         MARIE            NEW YORK         NY     718/384-8816
  1420  ROUSE            JEREMY           PATERSON         NJ     201/732-9834
  1403  BOWDEN           EARL             BRIDGEPORT       CT     203/675-3434
  1616  FUENTAS          CARLA            NEW YORK         NY     718/384-3329

                               PROCLIB.SCHEDULE2

                                                 Id
                          Flight     Date  Dest  Num
                          ---------------------------
                          132     01MAR94  BOS   1118
                          132     01MAR94  BOS   1402
                          219     02MAR94  PAR   1616
                          219     02MAR94  PAR   1478
                          622     03MAR94  LON   1430
                          622     03MAR94  LON   1882
                          271     04MAR94  NYC   1430
                          271     04MAR94  NYC   1118
                          579     05MAR94  RDU   1126
                          579     05MAR94  RDU   1106

                                PROCLIB.SUPERV2

                          Supervisor         Job
                          Id          State  Category
                          ---------------------------
                          1417        NJ     NA      
                          1352        NY     NA      
                          1106        CT     PT      
                          1442        NJ     PT      
                          1118        NY     PT      
                          1405        NJ     SC      
                          1564        NY     SC      
                          1639        CT     TA      
                          1126        NY     TA      
                          1882        NY     ME      

Program

 Note about code
libname proclib 'SAS-library';
 Note about code
options nodate pageno=1 linesize=80 pagesize=60;
 Note about code
   proc sql;
   title 'All Flights for Each Supervisor';
   select s.IdNum, Lname, City 'Hometown', Jobcat,
          Flight, Date
 Note about code
      from proclib.schedule2 s, proclib.staff2 t, proclib.superv2 v
 Note about code
      where s.idnum=t.idnum and t.idnum=v.supid;

Output: Listing

                        All Flights for Each Supervisor

       Id                                      Job
       Num   Lname            Hometown         Category  Flight     Date
       -----------------------------------------------------------------
       1106  MARSHBURN        STAMFORD         PT        579     05MAR94
       1118  DENNIS           NEW YORK         PT        132     01MAR94
       1118  DENNIS           NEW YORK         PT        271     04MAR94
       1126  KIMANI           NEW YORK         TA        579     05MAR94
       1882  TUCKER           NEW YORK         ME        622     03MAR94

Previous Page | Next Page | Top of Page