Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Complex ABAP Query Understanding

0 Likes
1,495

Hi Experts,

I need to fix a specific bug in a select query in ABAP.

But i am unable to understand the logic written. I so would really appreciate if anyone could help me decode the logic in a simple manner.

ABAP Query:

CONSTANTS c_zero TYPE bseg-dmbtr VALUE '0.00'.

SELECT a~bukrs, a~belnr, a~gjahr,a~buzei AS buzei, a~dmbtr,a~menge,
a~meins,a~koart,
a~sgtxt, a~txgrp, a~bupla, a~gst_part, a~plc_sup, a~hsn_sac,
a~kunnr, a~werks, a~wrbtr, a~hkont, a~taxps, a~mwskz, a~shkzg,
a~umskz, a~ktosl, a~zuonr,a~ebeln, a~ebelp, a~qbshb,a~qsshb,
a~xref1, a~xref2,
a~rebzg, a~rebzj, a~matnr,a~prctr, a~kostl,
CASE a~koart
WHEN 'K' THEN a~lifnr WHEN 'D' THEN a~kunnr END AS lifnr,
( CASE a~shkzg WHEN 'H' THEN a~dmbtr ELSE @c_zero END ) AS
lc_cr,
( CASE a~shkzg WHEN 'S' THEN a~dmbtr ELSE @c_zero END ) AS
lc_db,
( CASE a~shkzg WHEN 'H' THEN a~wrbtr ELSE @c_zero END ) AS
dc_cr,
( CASE a~shkzg WHEN 'S' THEN a~wrbtr ELSE @c_zero END ) AS
dc_db,
( CASE a~menge WHEN @c_zero THEN a~menge ELSE division(
a~dmbtr, a~menge, 2 ) END ) AS kbetr,
concat( a~hkont,
concat( '-', ( CASE a~koart
WHEN 'K' THEN a~lifnr WHEN 'D' THEN a~kunnr END ) ) ) AS
account,
a~zfbdt, a~zbd1t, a~zbd2t, a~zbd3t,
b~txt50,
c~maktx,

CASE a~koart
WHEN 'K' THEN
( concat_with_space( b~txt50,
( concat_with_space( '-', d~name1 ,1 ) ),
1 ) )

WHEN 'D' THEN
( concat_with_space( b~txt50,
( concat_with_space( '-', e~name1 ,1 ) ),
1 ) )
ELSE b~txt50
END AS name1,
d~ktokk,
( concat_with_space( v1~name1, v1~name2 ,1 ) ) AS v1_name
FROM bseg AS a
INNER JOIN skat AS b
ON b~spras = @sy-langu
AND b~ktopl = '1000'
AND b~saknr = a~hkont
LEFT OUTER JOIN makt AS c
ON a~matnr = c~matnr
AND c~spras = @sy-langu
LEFT OUTER JOIN lfa1 AS d
ON a~lifnr = d~lifnr
LEFT OUTER JOIN kna1 AS e
ON a~kunnr = e~kunnr
LEFT OUTER JOIN bsec AS v1
ON a~bukrs = v1~bukrs
AND a~belnr = v1~belnr
AND a~gjahr = v1~gjahr
AND a~buzei = v1~buzei
WHERE a~bukrs = @im_bkpf-bukrs
AND a~belnr = @im_bkpf-belnr
AND a~gjahr = @im_bkpf-gjahr
ORDER BY buzei
INTO CORRESPONDING FIELDS OF
TABLE @gt_bseg.

1 ACCEPTED SOLUTION
Read only

Former Member
1,310

Hi,

You can take your time understanding big queries like these with extra replacement logic in it.

Just abstract them. First start with the inner and left outer joins and press f1 when you're having trouble understanding those terms.

Also understand the database tables from a functional perspective and their relationships.

Kind regards, Rob Dielemans

4 REPLIES 4
Read only

matt
Active Contributor
1,310

It'd be a lot easier to read and help if you use the "code" button in the editor and pasted your nicely formatted code - using right-click "paste as plain text".

Read only

Former Member
0 Likes
1,310

In which table field(data) you are getting the issue?

Read only

Former Member
1,311

Hi,

You can take your time understanding big queries like these with extra replacement logic in it.

Just abstract them. First start with the inner and left outer joins and press f1 when you're having trouble understanding those terms.

Also understand the database tables from a functional perspective and their relationships.

Kind regards, Rob Dielemans

Read only

DoanManhQuynh
Active Contributor
0 Likes
1,310

I could say this query is selecting information of partner ( could be customer or vendor or one-time partner thats why there are inner join with those master ). The CASE conditions just to separate into debit or credit column, get the partner name (maybe) etc...nothing special. Your query is specific with your logic so maybe you should ask about your issue instead.