2013 Jun 22 4:51 AM
Hi Guys,
I'm relatively new to ABAP would like to seek a pointer from gurus,
I'm stumbling on how to make a total,
my code as follows,
DATA: scustom_tab TYPE standard table of scustom,
scustom_wa TYPE scustom.
SELECT *
FROM scustom
INTO TABLE scustom_tab where CUSTTYPE = 'B' and
city = 'Frankfurt' and COUNTRY = 'DE'.
LOOP AT scustom_tab INTO scustom_wa.
AT NEW Discount.
SUM.
WRITE: / 'Sum',
scustom_wa-discount.
endAT.
AT LAST.
SUM.
WRITE: / 'Overall Sum',
scustom_wa-discount.
ENDAT.
ENDLOOP.
it generate the correct discount rate on each record but unable to do a overall sum.
any advice / solution would be very much appreciated.
Thank you,
Norman
2013 Jun 22 5:38 AM
Hi Norman,
The general pointers to be kept in mind while using control break statements are -
1. The Field on which you want to apply control break should be the left most field of the internal table. If this is not the case then control breaks will trigger even if your field does not change but the field towards the left of it changes.
2. All the fields to the right of the field on which you are applying control break will become 0 or *. Integer and dates become 0 while characters become *.
Advice - Before using Control breaks Sort Internal Table always.
Coming to your code - Do changes as boldened and it will work.
DATA: scustom_tab TYPE standard table of scustom,
scustom_wa TYPE scustom,
lv_dis_total type scustom-discount.
SELECT *
FROM scustom
INTO TABLE scustom_tab where CUSTTYPE = 'B' and
city = 'Frankfurt' and COUNTRY = 'DE'.
LOOP AT scustom_tab INTO scustom_wa.
*-- I assume 'Discount' is the left most field in scustom_tab internal table
lv_dis_total = lv_dis_total + scustom_wa-discount.
AT NEW Discount.
SUM.
WRITE: / 'Sum',
scustom_wa-discount.
endAT.
AT LAST.
* SUM.
WRITE: / 'Overall Sum',
* scustom_wa-discount.
lv_dis_total.
ENDAT.
ENDLOOP.
BR.
Hi Guys,
I'm relatively new to ABAP would like to seek a pointer from gurus,
I'm stumbling on how to make a total,
my code as follows,
DATA: scustom_tab TYPE standard table of scustom,
scustom_wa TYPE scustom.
SELECT *
FROM scustom
INTO TABLE scustom_tab where CUSTTYPE = 'B' and
city = 'Frankfurt' and COUNTRY = 'DE'.
LOOP AT scustom_tab INTO scustom_wa.
AT NEW Discount.
SUM.
WRITE: / 'Sum',
scustom_wa-discount.
endAT.
AT LAST.
SUM.
WRITE: / 'Overall Sum',
scustom_wa-discount.
ENDAT.
ENDLOOP.
it generate the correct discount rate on each record but unable to do a overall sum.
any advice / solution would be very much appreciated.
Thank you,
Norman
2013 Jun 22 5:02 AM
Hi Norman,
stop playing with AT NEW, AT LAST ... you will lost your mind (and your time).
if your fields look like that : FIELD1 FIELD2 FIELD3
1. AT NEW FIELD1 ==> You will not see the content of FIELD2 & FIELD3 (you will find stars)
2. AT NEW FIELD2, FIELD2 don't change, but FIELD1 change, you will stop ..
forget that statement
regards
Fred
2013 Jun 22 5:23 AM
Hi Ferederic,
I've tried using simplier method also no luck,
DATA : g type i,
h type i.
select * from scustom where CUSTTYPE = 'B' and city = 'Frankfurt' and COUNTRY = 'DE'.
if scustom-discount > '001' and scustom-discount < '009'.
g = g + 1.
h = scustom-discount.
write : / g, 20 h.
endif.
endselect.
uline.
write : 'TOTAL : ',20 h.
it gave the correct count and discount per record, but I'm still unable to sum it.
Thanks for the help.
2013 Jun 22 5:38 AM
Hi Norman,
The general pointers to be kept in mind while using control break statements are -
1. The Field on which you want to apply control break should be the left most field of the internal table. If this is not the case then control breaks will trigger even if your field does not change but the field towards the left of it changes.
2. All the fields to the right of the field on which you are applying control break will become 0 or *. Integer and dates become 0 while characters become *.
Advice - Before using Control breaks Sort Internal Table always.
Coming to your code - Do changes as boldened and it will work.
DATA: scustom_tab TYPE standard table of scustom,
scustom_wa TYPE scustom,
lv_dis_total type scustom-discount.
SELECT *
FROM scustom
INTO TABLE scustom_tab where CUSTTYPE = 'B' and
city = 'Frankfurt' and COUNTRY = 'DE'.
LOOP AT scustom_tab INTO scustom_wa.
*-- I assume 'Discount' is the left most field in scustom_tab internal table
lv_dis_total = lv_dis_total + scustom_wa-discount.
AT NEW Discount.
SUM.
WRITE: / 'Sum',
scustom_wa-discount.
endAT.
AT LAST.
* SUM.
WRITE: / 'Overall Sum',
* scustom_wa-discount.
lv_dis_total.
ENDAT.
ENDLOOP.
BR.
2013 Jun 22 5:41 AM
Hi Norman,
Try this way....
DATA: scustom_tab TYPE STANDARD TABLE OF scustom,
scustom_wa TYPE scustom.
data : discount1 type scustom-discount.
SELECT *
FROM scustom
INTO TABLE scustom_tab WHERE custtype = 'B' AND
city = 'Frankfurt' AND country = 'DE'.
LOOP AT scustom_tab INTO scustom_wa.
AT NEW discount.
SUM.
WRITE: / 'Sum',
scustom_wa-discount.
ENDAT.
discount1 = discount1 + scustom_wa-discount.
AT last.
WRITE: / 'Overall Sum',
discount1.
ENDAT.
ENDLOOP.
2013 Jun 22 5:55 AM
Hi guys,
thank you so much for the pointer,
I've manage to make a summary based on the help given.
assuming if I don't wish to use the internal table, instead I'm going to use normal select method, as such;
DATA : g type i,
h type i,
discountTotal1 type scustom-discount.
select * from scustom where CUSTTYPE = 'B' and city = 'Frankfurt' and COUNTRY = 'DE'.
if scustom-discount > '001' and scustom-discount < '009'.
g = g + 1.
h = scustom-discount.
write : / g, 20 h.
endif.
discountTotal1 = discountTotal1 + h.
endselect.
uline.
write : 'Discount TOTAL : ',20 discountTotal1.
I'm getting total 48 instead of 23.
not sure where I got thing wrong.
Thank you very much for the assistance guys.
2013 Jun 22 6:29 AM
Alright, everyone thank you for your help.
problem has been resolved.
many thanks!
2013 Jun 22 7:02 AM
Hi Norman,
I would not advice you to do any processing in Select.....Endselect Block because it is performance wise poor. All database hit statements are converted from Open SQL to native SQL and it takes some time. Always go for processing in code rather than during database statements.
BR.
| User | Count |
|---|---|
| 3 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 |