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

Need help with sum function

Former Member
0 Likes
814

Greetings Abapers

I am trying to come up with a sum of the premium amounts for various components grouped by policy ID. My code is as follows but i need help with the SUM function. Any suggestions.

"Get Premium Amount

select * Sum(ZPREM_AMOUNT) from ZRLAC_DOCOBJ into LS_ZRLAC_DOCOBJ

where POST_YEAR = WA_ZRLAC_DOCSE-POST_YEAR

and ZPOLICY_ID = WA_ZRLAC_DOCSE-ZPOLICY_ID

and ZSI_SCODE = WA_ZRLAC_DOCSE-ZSI_SCODE

and ZCASE_NUMBER = WA_ZRLAC_DOCSE-ZCASE_NUMBER.

endselect.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
789

Hi Daniel,

Since you are using a select.. endselect

So the easiest way is to use the COLLECT statement.

Here is an example.

=====

DATA: BEGIN OF seats,

carrid TYPE sflight-carrid,

seatsocc TYPE sflight-seatsocc,

END OF seats.

DATA seats_tab LIKE HASHED TABLE OF seats

WITH UNIQUE KEY carrid.

SELECT carrid seatsocc

FROM sflight

INTO seats WHERE carrid = 'LH'.

COLLECT seats INTO seats_tab.

ENDSELECT.

=====

In the above program, for all entry in the sflight table where carrid = 'LH' you will get the sum of seatocc in seats_tab.

Regards,

Saurabh

7 REPLIES 7
Read only

Former Member
0 Likes
789

Hi Daniel,

Please check this code

http://help.sap.com/saphelp_nw70/helpdata/en/fc/eb39eb358411d1829f0000e829fbfe/content.htm

please check this code

SELECT  SUM( zprem_amount )
FROM zrlac_docobj
INTO ls_zrlac_docobj
WHERE post_year = wa_zrlac_docse-post_year
AND zpolicy_id = wa_zrlac_docse-zpolicy_id
AND zsi_scode = wa_zrlac_docse-zsi_scode
AND zcase_number = wa_zrlac_docse-zcase_number.
ENDSELECT.

Best regards,

raam

Read only

Former Member
0 Likes
789

hi check this example for the sum....

PARAMETERS: p_carrid TYPE sbook-carrid,

p_connid TYPE sbook-connid.

TYPES: BEGIN OF sbook_type,

fldate TYPE sbook-fldate,

smoker TYPE sbook-smoker,

smk_cnt TYPE i,

END OF sbook_type.

DATA sbook_tab TYPE TABLE OF sbook_type.

SELECT fldate smoker COUNT( * ) AS smk_cnt

FROM sbook

INTO CORRESPONDING FIELDS OF TABLE sbook_tab

WHERE connid = p_connid

GROUP BY carrid fldate smoker

HAVING carrid = p_carrid

ORDER BY fldate smoker.

Read only

Former Member
0 Likes
789

Hi Daniel,

try like this.

data:  w_prem_amount like ZPREM_AMOUNT.

select ZPREM_AMOUNT 
  from ZRLAC_DOCOBJ 
   into LS_ZRLAC_DOCOBJ
where POST_YEAR = WA_ZRLAC_DOCSE-POST_YEAR
   and ZPOLICY_ID = WA_ZRLAC_DOCSE-ZPOLICY_ID
   and ZSI_SCODE = WA_ZRLAC_DOCSE-ZSI_SCODE
   and ZCASE_NUMBER = WA_ZRLAC_DOCSE-ZCASE_NUMBER.

 w_prem_amount = w_prem_amount + LS_ZRLAC_DOCOBJ-ZPREM_AMOUNT.

endselect.

now the total premium amount will be in w_prem_amount.

reward if useful.

Read only

Former Member
0 Likes
790

Hi Daniel,

Since you are using a select.. endselect

So the easiest way is to use the COLLECT statement.

Here is an example.

=====

DATA: BEGIN OF seats,

carrid TYPE sflight-carrid,

seatsocc TYPE sflight-seatsocc,

END OF seats.

DATA seats_tab LIKE HASHED TABLE OF seats

WITH UNIQUE KEY carrid.

SELECT carrid seatsocc

FROM sflight

INTO seats WHERE carrid = 'LH'.

COLLECT seats INTO seats_tab.

ENDSELECT.

=====

In the above program, for all entry in the sflight table where carrid = 'LH' you will get the sum of seatocc in seats_tab.

Regards,

Saurabh

Read only

Former Member
0 Likes
789

Hi Daniel

Plz refer to following...........

select VRSIO

SPMON

SPART

MATNR

sum( ZNETSALE ) as S_ZNETSALE

sum( ZPTSNET ) as S_ZPTSNET

into corresponding fields

of table IT_S511

from S511

where VRSIO in ('000', 'IP0') and

SPMON in R_SPMON and

VTWEG in ('10', '20', '30', '35', '40') and

SPART in S_SPART

group by VRSIO SPMON SPART MATNR.

here group by is added for non-numeric keys.

reward pioints if found helpful

thnx

Prashant

Read only

Former Member
0 Likes
789

don't go for * rather u may try with this...

tables: sflight.
data:
t_sflight like table of sflight-seatsmax.
select sum( seatsmax )
  from sflight
  into table t_sflight.

or you can sum after populating the itab

like u can use at first inside a loop as

at first.

sum.

write: seatmax.

endat.

with luck,

pritam.

Read only

megha_h
Participant
0 Likes
789

Hi Daniel,

Select endselect is not the optimal way to do this.

try this for good performance

select ZPREM_AMOUNT

from ZRLAC_DOCOBJ

into table itab

where POST_YEAR = WA_ZRLAC_DOCSE-POST_YEAR

and ZPOLICY_ID = WA_ZRLAC_DOCSE-ZPOLICY_ID

and ZSI_SCODE = WA_ZRLAC_DOCSE-ZSI_SCODE

and ZCASE_NUMBER = WA_ZRLAC_DOCSE-ZCASE_NUMBER.

clear LS_ZRLAC_DOCOBJ.

loop at itab.

LS_ZRLAC_DOCOBJ = LS_ZRLAC_DOCOBJ + itab-ZPREM_AMOUNT.

endloop.

hope this helps.

Regards

Megha