Having Clause in Select - Open SQL Enhancement ABAP on HANA - Part 12



*&---------------------------------------------------------------------*
*& Report zr_opensql_20_unioon_grp_by
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT zr_opensql_20_having_union.

SELECT
 FROM vbak AS so
 INNER JOIN kna1 AS cust
 ON so~kunnr = cust~kunnr
 FIELDS so~kunnr AS cust_id,
        cust~name1 AS customer,
        so~waerk AS curre,
        SUM( so~netwr ) AS tot_netprice,
        AVG( so~netwr ) AS net,
        MAX( so~netwr ) AS max,
        MIN( so~netwr  ) AS min,
        COUNT( so~netwr ) AS count,
        'Importet CUSSTOMER' AS cat_cust
 GROUP BY so~kunnr,so~waerk,cust~name1
 HAVING SUM( so~netwr ) > 10000
UNION
 SELECT
 FROM vbak AS so
 INNER JOIN kna1 AS cust
 ON so~kunnr = cust~kunnr
 FIELDS so~kunnr AS cust_id,
        cust~name1 AS customer,
        so~waerk AS curre,
        SUM( so~netwr ) AS tot_netprice,
        AVG( so~netwr ) AS net,
        MAX( so~netwr ) AS max,
        MIN( so~netwr  ) AS min,
        COUNT( so~netwr ) AS count,
        'Other CUSSTOMER' AS cat_cust
 GROUP BY so~kunnr,so~waerk,cust~name1
 HAVING SUM( so~netwr ) < 10000
 INTO TABLE @DATA(it_result).
IF sy-subrc IS INITIAL.
  cl_demo_output=>display( it_result ).
ENDIF.


*Select lists of the different selects must
*–have the same number of columns
*–contain compatible types
*Union are use ful where we use appending same data from table using appending table


*************************************************************














* UNION ALL
*
* SELECT
* FROM vbak AS so
* INNER JOIN kna1 AS cust
* ON so~kunnr = cust~kunnr
* FIELDS so~kunnr AS cust_id,
*        cust~name1 AS customer,
*        so~waerk AS curre,
*        SUM( so~netwr ) AS tot_netprice,
*        AVG( so~netwr ) AS net,
*        MAX( so~netwr ) AS max,
*        MIN( so~netwr  ) AS min,
*        COUNT( so~netwr ) AS count,
*        'less Importent' AS category
* GROUP BY so~kunnr,so~waerk,cust~name1
* HAVING  SUM( so~netwr ) < 10000


**********************************************************************
*********

Comments

Popular posts from this blog

AMDP ( ABAP Managed Database Procedure ) Part - 1

Backup all ADT Objects & Other queries RAP Part 10.1

SAP CDS Introduction Part 2 - ABAP on HANA Course