Quantcast
Channel: SCN : All Content - SAP HANA Developer Center
Viewing all articles
Browse latest Browse all 6745

Self join in SAP HANA

$
0
0

Hi all.

 

There is sales order's table with columns:

doc_number - Sales document

s_ord_item - Sales document item

kondm - Material Pricing Group

uepos - Higher-level item in Sales document

 

I need choose rows which are not a sub-item of item with kondm = 15 or just not a sub-item.

 

Example:

source table
target table
doc_numbers_ord_itemkondmuepos
1000003011015
100000301201210
100000301110
10000030112012110
100000301130
10000030114012130
10000030115010
doc_numbers_ord_itemkondmuepos
1000003011015
100000301110
10000030112012110
100000301130
10000030114012130

Items 20 and 150 have higher-level item with kondm = 15, so they aren't in target table.

 

 

I tried do this using SQLscript.

At first I wrote SQLscript just with left join and without a filter.

 

/********* Begin Procedure Script ************/

BEGIN

 

ORDER_TAB = CE_COLUMN_TABLE("SAPBH1"."/BIC/AZSD_O0100",

                               ["DOC_NUMBER","S_ORD_ITEM","/BIC/ZKONDM","/BIC/ZUEPOS","SUBTOTAL_2"]);

 

UEPOS_TAB = CE_COLUMN_TABLE("SAPBH1"."/BIC/AZSD_O0100",

                               ["DOC_NUMBER","S_ORD_ITEM","/BIC/ZKONDM"]);

 

ORDER_PROJ = CE_PROJECTION(:ORDER_TAB,

                            ["DOC_NUMBER","S_ORD_ITEM","/BIC/ZKONDM" AS "KONDM","/BIC/ZUEPOS" AS "UEPOS","SUBTOTAL_2"],

                            '"DOC_NUMBER" = '':PRM_ORDER''');

 

UEPOS_PROJ = CE_PROJECTION(:UEPOS_TAB,

                            ["DOC_NUMBER","S_ORD_ITEM" AS "UEPOS","/BIC/ZKONDM" AS "UEPOS_KONDM"],

                            '"DOC_NUMBER" = '':PRM_ORDER''');

 

VAR_OUT = CE_LEFT_OUTER_JOIN(:ORDER_PROJ, :UEPOS_PROJ,

                               ["DOC_NUMBER","UEPOS"],

                               ["DOC_NUMBER","S_ORD_ITEM","KONDM","UEPOS","UEPOS_KONDM","SUBTOTAL_2"]);

 

END

/********* End Procedure Script ************/

 

First scrit's result is

doc_numbers_ord_itemkondmueposuepos_kondm
1000003011015?
10000030120121015
100000301110?
10000030112012110
100000301130?
10000030114012130
1000003011501015

 

Then I added projection to filter rows.

 

/********* Begin Procedure Script ************/

BEGIN

 

ORDER_TAB = CE_COLUMN_TABLE("SAPBH1"."/BIC/AZSD_O0100",

                               ["DOC_NUMBER","S_ORD_ITEM","/BIC/ZKONDM","/BIC/ZUEPOS","SUBTOTAL_2"]);

 

UEPOS_TAB = CE_COLUMN_TABLE("SAPBH1"."/BIC/AZSD_O0100",

                               ["DOC_NUMBER","S_ORD_ITEM","/BIC/ZKONDM"]);

 

ORDER_PROJ = CE_PROJECTION(:ORDER_TAB,

                            ["DOC_NUMBER","S_ORD_ITEM","/BIC/ZKONDM" AS "KONDM","/BIC/ZUEPOS" AS "UEPOS","SUBTOTAL_2"],

                            '"DOC_NUMBER" = '':PRM_ORDER''');

 

UEPOS_PROJ = CE_PROJECTION(:UEPOS_TAB,

                            ["DOC_NUMBER","S_ORD_ITEM" AS "UEPOS","/BIC/ZKONDM" AS "UEPOS_KONDM"],

                            '"DOC_NUMBER" = '':PRM_ORDER''');

 

JOIN_TAB = CE_LEFT_OUTER_JOIN(:ORDER_PROJ, :UEPOS_PROJ,

                               ["DOC_NUMBER","UEPOS"],

                               ["DOC_NUMBER","S_ORD_ITEM","KONDM","UEPOS","UEPOS_KONDM","SUBTOTAL_2"]);

 

VAR_OUT = CE_PROJECTION(:JOIN_TAB,

                            ["DOC_NUMBER","S_ORD_ITEM","KONDM","UEPOS","UEPOS_KONDM","SUBTOTAL_2"],

                            '"UEPOS_KONDM" != ''15''');

           

END

/********* End Procedure Script ************/

 

Second scrit's result surprised me - here it is:

doc_numbers_ord_itemkondmueposuepos_kondm
10000030112012110
10000030114012130

 

 

Then I tried filter uepos_kondm <> ?, it filtered all correct.

 

VAR_OUT = CE_PROJECTION(:JOIN_TAB,

                            ["DOC_NUMBER","S_ORD_ITEM","KONDM","UEPOS","UEPOS_KONDM","SUBTOTAL_2"],

                            '"UEPOS_KONDM" != ''?''');

 

Result is:

doc_numbers_ord_itemkondmueposuepos_kondm
10000030120121015
10000030112012110
10000030114012130
1000003011501015

 

 

Why did second script's projection filter out rows not only with uepos_kondm = 15, but also rows with blank and undefined uepos_kondm?

How fix it?


Viewing all articles
Browse latest Browse all 6745

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>