‎2009 Aug 04 10:33 AM
Hi all ,
I have a problem to find the sum of a dynamic column while retrieving the data from data base using Select Sum aggregate.
The problem is i am unable to pass the dynamic column name to the SUM aggreagte function, i.e suppose i am retrieving data from COSR . based on the period entered in the selection screen i need to sum the values of a field in COSR.
Is it possible to use SUM aggregate function in this case? Please help me?
Thanks,
Aravind
‎2009 Aug 04 10:45 AM
Hi ,
Try the following code,
Select SUM(sme001)
from COSR
into v_total
where gjahr in s_gjahr.
If the column to sum is dynamic, pass it to a field symbol and use it in the select statement.
Regards,
Vik
‎2009 Aug 04 10:55 AM
Hi,
Thanks for the replies.
Yes the coloumn is dynamic . I already tried by passing the field symbol. But it is giving syntax error as unknown field list.
Can you help me on passing filed symbol to SUM function?
Thanks,
Aravind
‎2009 Aug 04 10:51 AM
Hi Aravind,
Are you trying to sum fields SME001 to SME016? Try using Field Symbols:
field-symbols <lfs_field> type cosr-sme001.
data: l_variable(2) type c,
l_field type string.
p_prefix = 'cosr-sme0'.
while l_variable le 16.
concatenate p_prefix l_variable into l_field.
assign (l_field) to <lfs_field>.
if sy-subrc = 0.
p_amount = p_amount + <lfs_field>.
endif.
l_variable = l_variable + 1.
endwhile.
hope this helps.
Benedict
Edited by: benedict choa on Aug 4, 2009 5:51 PM
‎2009 Aug 04 11:03 AM
use dynamic select:
e.g. if you can retrieve the field name, for example field1 and you will be retrieving from table1, then just do it like this:
lv_field1 = 'FIELD1'.
lv_table1 = 'TABLE1'.
concatenate 'SUM(' lv_field1 ')' 'from ' lv_table1 'into' 'lv_sum' into lv_string.
select (lv_string).
if you want to add where condition, just add it in the string.
‎2009 Aug 05 2:25 AM
Hi Chris,
Thanks for the reply. But if i use the same procedure syntax error is occuring as select statment ended unexpectedly?
Please help.
Thanks,
Aravind.
‎2009 Aug 05 8:47 AM
Hi all,
Thanks for the replies.
I tried in all the ways mentioned. I think it is not possible to use SUM with a dynamic coloumn. So i used another syntax and closing the thread.
Thanks,
Aravind.
‎2009 Aug 04 11:09 AM
try something like this im not sure whether it matches ur req
append ur dynamic field to internal table.
parameters:pa_lgort type mard-lgort.
data:field type string.
data:begin of it OCCURS 0,
text(25) type c,
end of it.
data:begin of it1 OCCURS 0,
labst type mard-labst,
end of it1.
concatenate 'sum(' 'labst' ')' INTO field SEPARATED BY space. "<--pass ur dynamic field here.
append field to it.
select (it) from mard into table it1 where lgort = pa_lgort.
break-point.
Edited by: Keshu Thekkillam on Aug 4, 2009 3:39 PM
Edited by: Keshu Thekkillam on Aug 4, 2009 3:40 PM