Application Development 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: 

Select sum(field1, field2.... field12) to lv_sum

sankar1781
Participant
0 Kudos
406

Hi Experts,

Sorry if I have not searched the existing threads but posting it as duplicate or repeated discussion.

I tried but I didn't find the correct one as I expected.

My requirement is to query the FMBL table to fetch and aggregate or sum of the TVAL01 to TVAL12 to one variable by all the previous years based on Measure. 

For ex: if my input year is for 2024 and the Measure is 10-12345, then the query must fetch of all the data of Measure less than the year 2024 (we can set start year as 2015).

It will collect all the TVAL01 to TVAL12 of previous 10 years of records of the Measure and sum it to a single variable lv_sum. 

I tried with the EXEC SQL statement but it's going run-time error. Instead of approaching the application team as it takes time, I need your help, is there any other way to sum of all the amount fields to a single variable.

Please let me know. 

Thanks & Regards.

6 REPLIES 6

Sandra_Rossi
Active Contributor
0 Kudos
305

Why using EXEC SQL instead of just SELECT? Try SELECT and if there's an issue tell us the code you have tried and what issue you have.

267

Something like this might work. 

SELECT SUM(TVAL01 + TVAL02 + TVAL03 + TVAL04 + TVAL05 + TVAL06 + 

           TVAL07 + TVAL08 + TVAL09 + TVAL10 + TVAL11 + TVAL12) AS lv_sum

FROM FMBL

WHERE Measure = '10-12345'

AND Year < 2024

AND Year >= 2015;

0 Kudos
260

Hi Azeemquadri,

Thanks.

I did the same rather I tried is there any new way to declare it. 

I am closing this thread and thanks again for your code help. 

262

Hi Sandra_Rossi,

I thought using EXEC SQL helps to simplify the code, so posted a discussion here but resolved with SELECT AND ENDSELECT only.

Thanks for your response.

 

0 Kudos
246

I'm surprised that you have used ENDSELECT. It's to be used only by "experts" after performance issues have been reported. ENDSELECT in other situations should not be used. Probably you have used INTO @structure instead of INTO TABLE @itab.

sankar1781
Participant
0 Kudos
260

As Azeemquadri and Sandra Rossi replied. I approached the basic SELECT sum statement and it worked.