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

loop issue

Former Member
0 Likes
1,145

hi,

in my report program i need to calculate the total amount based on the Customer number and Invoice number. But problem is that some time one customer having three invoice. if i give in condition for customer or invoice its taking only one Invoice or one customer. My requirement is based on the customer i need both Invoice Amount calculation. For that what logic i want to write in my program. If any one having any idea plz inform me.

Thanks

bab

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,089

Hi,

Do something like this.

1. Create an internal table with the customer number as the first field, invoice number as the second and invoice item as the third and rest of the fields of the invoice ( like amount etc ).

2. Populate this table and sort it by customer number invoice number and invoice item.

3. Loop at this sorted internal table

4. use control break statements ( at new, at end of etc ) on customer number and invoice number sum invoice amount in a varaible lets call them as cust_amount , invoice_amount.

5 End loop

Hope this will give you an idea of how to proceed, please make your attempt if you have further difficulties , don't hesitate to ask.

KR,

Advait

10 REPLIES 10
Read only

Former Member
0 Likes
1,089

Hi,

Please post the code snippet you are trying for so that we can suggest some changes in it as per your requirement

Regards,

Vik

Read only

0 Likes
1,089

hi,

my select query for mat is

SELECT CNO CDATE MINVNO SUM( REAMOUNT1 )

INTO (L_CNO2, L_CDATE2, L_MINVNO2, L_AMOUNT2)

FROM ZPAYMENT

WHERE ( CDATE = WA_PAY-CDATE AND CNO = WA_PAY-CNO AND

MINVNO = WA_PAY-MINVNO AND VCODE = WA_PAY-VCODE

AND QTY > 0 AND REAMOUNT1 > 0 )

in these query its fetching what are the invoice available in that corresponding customer number that much invoice amount will take and give total amount.

if i give condition like

SELECT CNO CDATE MINVNO SUM( REAMOUNT1 )

INTO (L_CNO2, L_CDATE2, L_MINVNO2, L_AMOUNT2)

FROM ZPAYMENT

WHERE ( CDATE = WA_PAY-CDATE AND CNO = WA_PAY-CNO AND

MINVNO = WA_PAY-MINVNO AND VCODE = WA_PAY-VCODE AND CUSTMAT = T_CUSTMAT

AND QTY > 0 AND REAMOUNT1 > 0 )

in this condition it takes only one invoice based on the customer number.

thanks

bab

Read only

0 Likes
1,089

hi vikred ,

have you see the select query.... for these what i want logic i want to write.

bab

Read only

0 Likes
1,089

Hi,

The first select query you have mentioned, youi will get the total ammount for each customer number based on the number of invoices present for the customer.

In the second select query you have added the condition CUSTMAT = T_CUSTMAT. What does CUSTMAT stand for and what is the requirement now?

Regards,

Vik

Read only

0 Likes
1,089

To me your query looks fine, however, it is obvious that the additional criteria ( CUSTMAT = T_CUSTMAT) is affecting your data selection and that is where you need to see in se16 if there are more than 1 records in the table for that condition.

KR,

Advait

Read only

0 Likes
1,089

hi,

forget about the second query... in first query what logic i want to apply for getting base on one customer (CUSTMAT) in need to get the corresponding invoice total....

bab

Read only

0 Likes
1,089

Hi,

Then try like this,


SELECT CNO CDATE MINVNO  REAMOUNT1 
INTO  itab
FROM ZPAYMENT
WHERE ( CDATE = WA_PAY-CDATE AND CNO = WA_PAY-CNO AND 
MINVNO = WA_PAY-MINVNO AND VCODE = WA_PAY-VCODE AND CUSTMAT = T_CUSTMAT
AND QTY > 0 AND REAMOUNT1 > 0 ).

loop at itab.
v_ind = sy-tabix+1.
if sy-tabix = 1.
read table itab into wa_tab index v_ind.
if itab-CUSTMAT = wa_tab- CUSTMAT and itab-MINVNO = wa_tab-MINVNO.
total = itab-REAMOUNT1 + wa_tab-REAMOUNT1.
clear wa_tab.
endif.
else.
read table itab into wa_tab index v_ind.
if itab-CUSTMAT = wa_tab- CUSTMAT and itab-MINVNO = wa_tab-MINVNO.
total = total + wa_tab-REAMOUNT1.
clear wa_tab.
endif.

itab2-CUSTMAT= itab-CUSTMAT.
itab2-reamount1 = total.
append itab2.
endloop.

Now itab2 will hold the customer and the total amount calculated

Regards,

Vik

Read only

Former Member
0 Likes
1,089

Hi Babu,

declare another internal table and clear all fields except customer, invoice , amt field.

Then in loop collect .

e.g.

itab1 and itab2 shuld be same.

Loop at itab1.

clear all fields except customer, invoice , amt field.

collect itab1 into itab2.

Endloop.

Regards,

Vijay

Read only

Former Member
0 Likes
1,089

Create a temp internal table with customer no, invoice no and amt field. first fill only cust no and invoice no..

loop at customet no.. and check if looped record EQ previous record.. if yes add the sum to a variable.. do this until the cust no is not equal to the previous record. if the condition is not satisfied then move the value in variable to teh thir field in the interna table. so by the end of the loop processing u have customer no, inv no and sum of all teh amounts for a particular customer.

Method 2 : Read the customer no. loop at the internal table from sy-tabix.

Sum up all the amounts for the same cust no..

Read only

Former Member
0 Likes
1,090

Hi,

Do something like this.

1. Create an internal table with the customer number as the first field, invoice number as the second and invoice item as the third and rest of the fields of the invoice ( like amount etc ).

2. Populate this table and sort it by customer number invoice number and invoice item.

3. Loop at this sorted internal table

4. use control break statements ( at new, at end of etc ) on customer number and invoice number sum invoice amount in a varaible lets call them as cust_amount , invoice_amount.

5 End loop

Hope this will give you an idea of how to proceed, please make your attempt if you have further difficulties , don't hesitate to ask.

KR,

Advait