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

Select statement for summing values in column?

Former Member
0 Likes
3,013

How to write a select statement for sap standard table which contains data as given below

col1 col2

A 10

A 20

A 30

B 50

B 60

C 70

Need the output of select in internal table as given below

A 60

B 110

C 70

Regards,

Rachel

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
2,062

Hi,

You can use collect statement or u can use AT END OF field with the loop.

let me know if u have any issue in using these.

Regards,

Kusuma.

11 REPLIES 11
Read only

Former Member
0 Likes
2,063

Hi,

You can use collect statement or u can use AT END OF field with the loop.

let me know if u have any issue in using these.

Regards,

Kusuma.

Read only

Former Member
0 Likes
2,062

Hi Rachel,

You can use COLLECT statement( go through the key word documentation)

or while looping you can use Control break statements

AT NEW (( go through the key word documentation)

ENDAT.

Regards

Ramchander Rao.K

Read only

Former Member
0 Likes
2,062

If you want to directly get sum from the standard table;

See F1 help for SELECT SUM()

If you can do it after fetching data into internal table;

See F1 help for COLLECT.

Do search the forum before posting a question. You will get number of thread in COLLECT and SELECT.

Regards

Karthik D

Read only

Former Member
0 Likes
2,062

<copy&paste_removed_by_moderator>

Edited by: Julius Bussche on Nov 25, 2008 10:53 AM

Read only

0 Likes
2,062

@Jay

Nice copy & paste work from;

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

Read the [Rules of Engagement|https://www.sdn.sap.com/irj/sdn/wiki?path=/display/home/rulesofEngagement] before posting more.

Regards

Karthik D

Read only

Former Member
0 Likes
2,062

Within the loop COLLECT INTO a work area.

LOOP AT int_final INTO wa_final.

wa_final2-field1 = wa_final-field1.

wa_final2-field2 = wa_final-field2.

.

.

COLLECT wa_final2 INTO int_final2.

ENDLOOP.

Read only

rainer_hbenthal
Active Contributor
0 Likes
2,062

select col1, sum(col2) into <your_own_variables_here> from <tablename> group by col1.
Read only

Former Member
0 Likes
2,062

Hi

suppose i_tab1 contains the data. the sum data is in i_tab2. Just do

define i_tab2 like

data: i_tab2 type standard table of ty_tab2 with key column1.

   loop at i_tab1 into w_tab1.
      collect w_tab1 into i_tab2.
   endloop.

Read only

Former Member
0 Likes
2,062

Thanks for all the valuable input.

Below I have used sum in select statement(check case 2"),but i am not getting required o/p.

Kindly guide me.

case1 :

select aebeln aebelp bpackno bmenge

into corresponding fields of table itab

from ( eslh as a inner join esll as b

on bpackno = apackno )

where a~ebeln = 6000000100

and a~ebelp = 00010

and a~packno = '0000001025'

order by aebeln aebelp b~packno .

O/p for the above .

ebeln ebelp packno menge

6000000100 00010 0000001025 6.000

6000000100 00010 0000001025 5.000

6000000100 00010 0000001025 2.000

6000000100 00010 0000001025 1.000

6000000100 00010 0000001025 2.000

case2:

select aebeln aebelp bpackno sum( bmenge )

into corresponding fields of table itab

from ( eslh as a inner join esll as b

on bpackno = apackno )

where a~ebeln = 6000000100

and a~ebelp = 00010

and a~packno = '0000001025'

group by aebeln aebelp b~packno

order by aebeln aebelp b~packno .

6000000100 00010 0000001025 0.000

Edited by: Rachel on Nov 25, 2008 11:45 AM

Edited by: Rachel on Nov 25, 2008 11:46 AM

Read only

0 Likes
2,062

Just read my solution.

Read only

Former Member
0 Likes
2,062

Hi,

Use the collect statement.

COLLECT wa INTO itab

Thanks & Regards