You are on page 1of 17

JOIN

Join
[Inner] Join

Table 1 Table 2
|----|----|----|----| |----|----|----|----|----|
| A | B | C | D | | D | E | F | G | H |
|----|----|----|----| |----|----|----|----|----|
| a1 | b1 | c1 | 1 | | 1 | e1 | f1 | g1 | h1 |
| a2 | b2 | c2 | 1 | | 3 | e2 | f2 | g2 | h2 |
| a3 | b3 | c3 | 2 | | 4 | e3 | f3 | g3 | h3 |
| a4 | b4 | c4 | 3 | |----|----|----|----|----|
|----|----|----|----|

Inner Join
|----|----|----|----|----|----|----|----|
| A | B | C | D | E | F | G | H |
|----|----|----|----|----|----|----|----|
| a1 | b1 | c1 | 1 | e1 | f1 | g1 | h1 |
| a2 | b2 | c2 | 1 | e1 | f1 | g1 | h1 |
| a4 | b4 | c4 | 3 | e2 | f2 | g2 | h2 |
|----|----|----|----|----|----|----|----|
[Inner] Join example

TYPES: BEGIN of ty_str,


connid TYPE connid,
carrname TYPE carrname,
END OF ty_str.
TABLES: spfli.
DATA: gt_connid TYPE TABLE OF ty_str,
gs_connid TYPE ty_str.
SELECT-OPTIONS s_connid FOR spfli-connid.

START-OF-SELECTION.
SELECT spfli~connid scarr~carrname
INTO TABLE gt_connid
FROM spfli
JOIN scarr ON spfli~carrid = scarr~carrid
WHERE spfli~connid IN s_connid.

LOOP AT gt_connid INTO gs_connid.


WRITE: / ‘Flight : ‘ , gs_connid-connid, ‘is operated by: ‘, gs_connid-carrname.
ENDLOOP.
Left [Outer] Join

Table 1 Table 2
|----|----|----|----| |----|----|----|----|----|
| A | B | C | D | | D | E | F | G | H |
|----|----|----|----| |----|----|----|----|----|
| a1 | b1 | c1 | 1 | | 1 | e1 | f1 | g1 | h1 |
| a2 | b2 | c2 | 1 | | 3 | e2 | f2 | g2 | h2 |
| a3 | b3 | c3 | 2 | | 4 | e3 | f3 | g3 | h3 |
| a4 | b4 | c4 | 3 | |----|----|----|----|----|
|----|----|----|----|

Left Outer Join


|----|----|----|----|----|----|----|----|----|
| A | B | C | D | D | E | F | G | H |
|----|----|----|----|----|----|----|----|----|
| a1 | b1 | c1 | 1 | 1 | e1 | f1 | g1 | h1 |
| a2 | b2 | c2 | 1 | 1 | e1 | f1 | g1 | h1 |
| a3 | b3 | c3 | 2 |NULL|NULL|NULL|NULL|NULL|
| a4 | b4 | c4 | 3 | 3 | e2 | f2 | g2 | h2 |
|----|----|----|----|----|----|----|----|----|
Left [Outer] Join example

TYPES: BEGIN of ty_str,


connid TYPE connid,
carrname TYPE carrname,
END OF ty_str.
TABLES: spfli.
DATA: gt_connid TYPE TABLE OF ty_str,
gs_connid TYPE ty_str.
SELECT-OPTIONS s_connid FOR spfli-connid.

START-OF-SELECTION.
SELECT spfli~connid scarr~carrname
INTO TABLE gt_connid
FROM spfli
LEFT JOIN scarr ON spfli~carrid = scarr~carrid
WHERE spfli~connid IN s_connid.

LOOP AT gt_connid INTO gs_connid.


WRITE: / ‘Flight : ‘ , gs_connid-connid, ‘is operated by: ‘, gs_connid-carrname.
ENDLOOP.
JOIN: Inner VS Left Outer

One thing to keep in mind when using INNER The OUTER JOIN different from the INNER
JOIN is that, only the rows that are common on JOIN, even if in secondary table doesn’t find a
both tables included in JOIN will be selected, so if row common to primary table, still will get the
in second table the JOIN doesn’t find a common row from the primary table, so in some cases is
row to primary table, than no data is selected even more useful to use OUTER JOIN than INNER
from the primary table. JOIN.
Exercise

1.Create a new program and define the selection-screen:


s_carrid - select-option, p_city – parameter
2.Display all the flights operated by carrier s_carrid starting from
p_city. Info about flights are stored in table SPFLI, info about
carriers are stored in table SCARR;
3.Display the following information for s_carrid :
- carrier_name (SCAR-CARRNAME)
- connection_id (SFLIGHT-CONNID)
- flight_date (SFLIGHT-FLDATE)
- seats_occ (SFLIGHT-SEATSOCC)
4.Select the first p_row flights ( table SFLIGHT ) and display
them
For All Entries
SELECT FOR ALL ENTRIES

SELECT <field list>


FROM <database table>
INTO TABLE <itab1>
FOR ALL ENTRIES IN <itab2>
WHERE <field1> = <itab2>-field1 AND/OR
<field2> = <itab2>-field2 [AND/OR
<field3> = <variable>/<value>…… ] .

The WHERE condition is performed for each line of the internal table.
For each line, the system selects the lines from the database table that satisfy the
condition.
The result set is the union of the individual selections for each line of the internal table.
Duplicate lines are automatically eliminated from the result set.
If itab is empty, the addition FOR ALL ENTRIES is disregarded, and all entries are read.

10 © Copyright 2017 Crystal System srl | Confidential


SELECT FOR ALL ENTRIES example

TYPES: BEGIN of ty_str,


connid TYPE connid,
carrname TYPE carrname,
END OF ty_str.

DATA: gt_spfli TYPE TABLE OF ty_str,


gt_scarr TYPE TABLE OF scarr.

PARAMETERS p_connid TYPE spfli-connid.

START-OF-SELECTION.

SELECT carrid connid


FROM spfli
INTO CORRESPONDING FIELDS OF TABLE gt_spfli
WHERE connid = p_connid.
IF gt_spfli IS NOT INITIAL.
SELECT carrid carrname
FROM scarr
INTO CORRESPONDING FIELDS OF TABLE gt_spfli
FOR ALL ENTRIES IN gt_spfli
WHERE carrid = gt_spfli-carrid.
ENDIF.
11 © Copyright 2017 Crystal System srl | Confidential
SELECT LOOPS ( UP TO n ROWS )

TYPES: BEGIN of ty_str,


connid TYPE connid,
carrname TYPE carrname,
END OF ty_str.

DATA: gt_spfli TYPE TABLE OF ty_str,

START-OF-SELECTION.

SELECT carrid connid


FROM spfli
INTO CORRESPONDING FIELDS OF TABLE gt_spfli
UP TO 10 ROWS
WHERE connid = p_connid.

12 © Copyright 2017 Crystal System srl | Confidential


SELECT - exercise

1.Create a new program and define the selection-screen: s_carrid


- select-option, p_city – parameter
2.Display all the flights operated by carrier s_carrid starting from
p_city. Info about flights are stored in table SPFLI, info about carriers
are stored in table SCARR; Carrier name is mandatory to be displayed;
3.Display the following information for s_carrid :
- carrier_name (SCAR-CARRNAME)
- connection_id (SFLIGHT-CONNID)
- flight_date (SFLIGHT-FLDATE)
- seats_occ (SFLIGHT-SEATSOCC)
If no connection is found( table SPFLI ), display only the carrier
name.
4.Select the first p_row flights ( table SFLIGHT ) and display them

13 © Copyright 2017 Crystal System srl | Confidential


SELECT - exercise

5. Display the carrier name, country from, country to for S_CARRID . Use the
FOR ALL ENTRIES option for selection.

6. Display the charter flights ( FLTYPE = ‘X’ ). Fields to be displayed: carrier


name, URL, city from, city to

7. Add the select-options S_CONNID , S_FLDATE on the selection screen.


For the select-options S_CONNID , S_FLDATE and S_CARRID , display the
following information about the aircraft type : carrid, connid, fldate, planetype,
speed, speed unit, producer.
Hint: use tables SFLIGHT and SAPLANE.

8. Display the price ( SFLIGHT-PRICE ) for the S_CONNID , S_FLDATE and


S_CARRID specified by the user on the selection screen. Decide which type
of select is better to use here.

14 © Copyright 2017 Crystal System srl | Confidential


Time to practice

1. Create a program ZXY_FLIGHTS1 with the following selection screen


Select name Data type TYPE Mandatory Default Description

s_carrid SPFLI-CARRID Select option X LH Airline code

s_connid SFLIGHT-CONNID Select option Flight Connection


Number
s_fldate SFLIGHT-FLDATE Select option Flight date

s_from SPFLI-CITYFROM Select option Departure city

s_to SPFLI-CITYTO Select option Arrival city


Time to practice
2. Select in an internal table GT_SFLIGHT all the flights using the following conditions:
SFLIGHT-CARRID in s_carrid
SFLIGHT-CONNID in s_connid
SFLIGHT-FLDATE in s_fldate
SPFLI-CITYFROM in s_from
SPFLI-CITYTO in s_to

3. The internal table should contain the following fields:


CARRID (table SFLIGHT)
CONNID (table SFLIGHT)
FLDATE (table SFLIGHT)
CITYFROM (table SPFLI)
CITYTO (table SPFLI)
DEPTIME (table SPFLI)
ARRTIME (table SPFLI)
PRICE (table SFLIGHT)
CURRENCY (table SFLIGHT)
PLANETYPE (table SFLIGHT)
SEATSMAX (table SFLIGHT)
SEATSOCC (table SFLIGHT)
Time to practice

3. From the flights already selected, display the flight with the most empty seats (SEATSMAX – SEATSOCC).
Please use an intermediate internal table GT_SFLIGHT_DET with the following structure
CARRID (table SFLIGHT)
CONNID (table SFLIGHT)
FLDATE (table SFLIGHT)
SEATSDIF (table SFLIGHTS)
DIF_FLAG (CHAR1)

4. In order to fill table GT_SFLIGHT_DET do the following steps:


4.1 Iterate on the initial internal table GT_SFLIGHT using the LOOP syntax
a. CLEAR the GS_SFLIGHT_DET structure
b. Copy the values from fields CARRID, CONNID, FLDATE from GS_SFLIGHT into GS_SFLIGHT_DET
c. Calculate the value of the field SEATSDIF (SEATSMAX – SEATSOCC)
d. Add the structure GS_SFLIGHT_DET using APPEND if SY-TABIX is an odd number and INSERT if SY-
TABIX is an even number
5. Sort the Itab GT_SFLIGHT_DET descending by field SEATSDIF
6. Read and Display the first entry of the table

You might also like