Wednesday, July 31, 2019

Right Outer Join

This join always selects all records from the second table (right table) and then selects matching records from the first table (left table). If no records found then a blank line is created for the respected second table records. Hence it is similar to left outer logic with the right or second table. It is very much useful when we need to fetch second table data based on the first table.

PARAMETERSp_carrid TYPE spfli-carrid.
SELECT a~carrida~connida~cityfroma~cityto,
       a~fltimea~deptimea~arrtime,

       b~fldateb~priceb~currencyb~planetype

  FROM             sflight AS b
  RIGHT OUTER JOIN spfli   AS "Right join table
  ON b~carrid a~carrid   AND
     b~connid a~connid

  INTO TABLE @DATA(itab)
  WHERE a~carrid @p_carrid.

cl_demo_output=>displayitab ).



Now we need to fetch second table data based on the first table records. We don't need to fetch mismatching records into the output. Hence the second table must be under right outer join.

PARAMETERSp_carrid TYPE spfli-carrid.
SELECT a~carrida~connida~cityfroma~cityto,
       a~fltimea~deptimea~arrtime,

       b~fldateb~priceb~currencyb~planetype

  FROM             spfli   AS a
  RIGHT OUTER JOIN sflight AS b
  ON b~carrid a~carrid AND
     b~connid a~connid

  INTO TABLE @DATA(itab)
  WHERE a~carrid @p_carrid.

cl_demo_output=>displayitab ).


No comments: