Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

SAP Common Table Expressions (CTE) in Open SQL

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

SAP Common Table Expressions (CTE) in Open SQL

After the release of ABAP 7.51, a feature called CTE is introduced. The Keyword for using this is WITH, this allows to
define one or more common table expressions. Each CTE is a subquery that produces tabular data set (like internal tables)
that can be used as a data source in the subsequent queries encapsulated within it.

Here we will see a very small example of its illustration. Let us develop a very simple report that accept Purchase order
numbers in input and shows the following result in ALV grid.

List Field
1.Material Number
2.Material Name
3.Purchase Order Number that the Material involved in
4.Material Amount in the corresponding purchase order
5.Purchase Order Type
6.Purchase Order Date
7.Total Number of Items in the corresponding purchase order
8.Total Amount of all items in the purchase order

Before CTEs, we either go for individual select queries and loops to construct the output, but using CTEs we can bring the
entire output in on statement that has embedded common table expressions.

Syntax:
WITH
+cte1[( name1, name2, ... )] AS ( SELECT subquery_clauses [UNION ...] ),
[associations][,
+cte2[( name1, name2, ... )] AS ( SELECT subquery_clauses [UNION ...] ),
[associations],
... ]
SELECT mainquery_clauses
[UNION ...]
INTO|APPENDING target
[UP TO ...] [OFFSET ...]
[abap_options].
...
[ENDWITH].

It is mandatory that every sub table expression should have the symbol + as prefix and each sub table expressions can be
used in all subsequent queries. Each CTE should be separated by a comma and final select query should be present to get the
data into internal table. ENDWITH is optional.

CTE 1: Get the Materials and its Purchase order details using MAKT, EKPO and EKKO into +material_items (CTE 1) as
per the input given in select options

CTE 2: Get all the purchase order items detail from EKPO using the selected Purchase orders in +material_items (CTE 1)
into +po_total_items (CTE 2)

CTE 3: Count and store the total number of purchase order items for each purchase order using the +po_total_items (CTE 2)
into +po_tot_count (CTE 3)

CTE 4: Sum and store the total number of purchase order items for each purchase order using the +po_total_items(CTE 2)
into +po_tot_amount(CTE 4)

Finally combine the fields present in +material_items(CTE 1) ,+po_tot_count (


CTE 3) and +po_tot_amount (CTE 4) into Output internal table LT_OUT

WITH
""->CTE 1.Getting the Material and PO item details
+material_items AS ( SELECT a~matnr,
a~maktx,
b~ebeln,
b~ebelp,
b~netwr,
b~meins,
c~bstyp,
c~aedat
FROM makt AS a
INNER JOIN ekpo AS b
ON b~matnr EQ a~matnr
AND b~loekz NE @abap_true

INNER JOIN ekko AS c


ON c~ebeln EQ b~ebeln
AND c~loekz NE @abap_true

WHERE a~matnr IN @so_mat


AND a~spras EQ 'E' ),
""->CTE 2.Getting the Purchase order and all Items for the Selected Purchase orders in Step 1
+po_total_items AS ( SELECT a~ebeln,
a~ebelp,
a~matnr,
a~netwr
FROM ekpo AS a
WHERE a~ebeln IN ( SELECT DISTINCT ebeln FROM +material_items )
AND a~loekz NE @abap_true ),
""->CTE 3.Counting Number of Total Items in one Purchase Order
+po_tot_count AS ( SELECT ebeln,
COUNT( ebelp ) AS total_items
FROM +po_total_items
GROUP BY ebeln ),
""->CTE 4.Counting Total Value in one purchase Order
+po_tot_amount AS ( SELECT ebeln,
SUM( netwr ) AS total_sum
FROM +po_total_items
GROUP BY ebeln )
""->Final Output 5.Combining the necessary fields to form the output
SELECT a~matnr,
a~maktx,
a~ebeln,
a~bstyp,
a~aedat,
a~netwr,
b~total_items,
c~total_sum

FROM +material_items AS a
LEFT OUTER JOIN +po_tot_count AS b
ON b~ebeln EQ a~ebeln

LEFT OUTER JOIN +po_tot_amount AS c


ON c~ebeln EQ a~ebeln

ORDER BY a~matnr,
a~ebeln
INTO TABLE @DATA(lt_out).
Input Screen: Materials Number 30002 and 85 is given.

Output:

Report Source Code:


*&---------------------------------------------------------------------*
*& Report ZCTE_TEST
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT zcte_test.
""->Types Declaration
TYPES : tt_fcat TYPE slis_fieldcat_alv.

""->Data Declaration
DATA : lv_matnr TYPE makt-matnr.

""->Internal Table Declaration


DATA :lt_fcat TYPE TABLE OF tt_fcat.
SELECTION-SCREEN BEGIN OF BLOCK b1.
SELECT-OPTIONS : so_mat FOR lv_matnr.
SELECTION-SCREEN END OF BLOCK b1.

START-OF-SELECTION.
""->Form to Fetch the Data from Database Table

WITH
""->1.Getting the Material and PO item details
+material_items AS ( SELECT a~matnr,
a~maktx,
b~ebeln,
b~ebelp,
b~netwr,
b~meins,
c~bstyp,
c~aedat

FROM makt AS a
INNER JOIN ekpo AS b
ON b~matnr EQ a~matnr
AND b~loekz NE @abap_true

INNER JOIN ekko AS c


ON c~ebeln EQ b~ebeln
AND c~loekz NE @abap_true

WHERE a~matnr IN @so_mat


AND a~spras EQ 'E' ),
""->2.Getting the Purchase order and all Items for the Selected Purchase orders in Step 1
+po_total_items AS ( SELECT a~ebeln,
a~ebelp,
a~matnr,
a~netwr
FROM ekpo AS a
WHERE a~ebeln IN ( SELECT DISTINCT ebeln FROM +material_items )
AND a~loekz NE @abap_true ),
""->3.Counting Number of Total Items in one Purchase Order
+po_tot_count AS ( SELECT ebeln,
COUNT( ebelp ) AS total_items
FROM +po_total_items
GROUP BY ebeln ),
""->4.Counting Total Value in one purchase Order
+po_tot_amount AS ( SELECT ebeln,
SUM( netwr ) AS total_sum
FROM +po_total_items
GROUP BY ebeln )
""->5.Combining the necessary fields to form the output
SELECT a~matnr,
a~maktx,
a~ebeln,
a~bstyp,
a~aedat,
a~netwr,
b~total_items,
c~total_sum

FROM +material_items AS a
LEFT OUTER JOIN +po_tot_count AS b
ON b~ebeln EQ a~ebeln

LEFT OUTER JOIN +po_tot_amount AS c


ON c~ebeln EQ a~ebeln

ORDER BY a~matnr,
a~ebeln
INTO TABLE @DATA(lt_out).

"Build Field Catelogue for displaying the output


PERFORM build_fcatalog USING:
'MATNR' 'LT_OUT' 'Material Number',
'MAKTX' 'LT_OUT' 'Material Name',
'EBELN' 'LT_OUT' 'Purchase Order No.',
'NETWR' 'LT_OUT' 'PO Material Amount',
'BSTYP' 'LT_OUT' 'Purchase Order Type',
'AEDAT' 'LT_OUT' 'Date',
'TOTAL_ITEMS' 'LT_OUT' 'No. Of Items in PO',
'TOTAL_SUM' 'LT_OUT' 'Total PO value'.

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'


EXPORTING
i_callback_program = sy-cprog
it_fieldcat = lt_fcat
is_layout = VALUE slis_layout_alv( colwidth_optimize = 'X'
zebra = 'X' )
TABLES
t_outtab = lt_out
EXCEPTIONS
program_error =1
OTHERS = 2.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.

""->Form to Build field catelogue

FORM build_fcatalog USING l_field l_tab l_text.

APPEND VALUE tt_fcat( fieldname = l_field


tabname = l_tab
seltext_m = l_text ) TO lt_fcat.

ENDFORM.

You might also like