cancel
Showing results for 
Search instead for 
Did you mean: 

Calculating Percentage for OData Service

0 Kudos

Hi,

I am trying to fill a structure that basically only has an ID and a percentage for an OData Service. All values come out of tables that are already part of the OData service, however the percentage is missing.

The OData Service is for a Fiori-application.

When I am tryng to calculate the percentage a error message is shown (roughly translated) it says:
"The operators DIV and MOD cannot be used in an expression that contains real decimal numbers such as X." with X being one of the two numbers I need for the calculation.

Because of our old system I can't use CDS Views.

The code is basically like this:

SELECT table1~id, DIV( table2~X, table2~Y) AS percentage
FROM table1 JOIN table2 
ON table1~id EQ table2~id<br>INTO CORRESPONDING FIELDS OF TABLE @ET_ENTITYSET.

That's why I wanted to ask if someone got an idea how to solve it and can give me a hint.

Thanks in advance!

Philipp

Accepted Solutions (1)

Accepted Solutions (1)

0 Kudos

Hey, just wanted to say

I just wanted to say that I got a solution for the problem now:

LOOP AT et_entityset ASSIGNING FIELD-SYMBOL(<ls_entity>).
     IF ( <ls_entity>-y <> 0).
        <ls_entity>-percent = <ls_entity>-x / <ls_entity>-y.
     ELSE.
        <ls_entity>-prozent = 0.
ENDLOOP.

After the select statement.

Just in case someone gets similar problem.

Answers (1)

Answers (1)

ThorstenHoefer
Active Contributor
0 Kudos

Hi ptvoss,

dependend of the datatype (decimal places) you need to choose the function division instead of div.

https://help.sap.com/doc/abapdocu_752_index_htm/7.52/en-US/abensql_arith_func.htm

https://help.sap.com/doc/abapdocu_752_index_htm/7.52/en-US/abensql_expr_arith_abexa.htm

Alternative, you can try

 select ...
CAST( table2~X as FLTP) / CAST( table2~Y as FLTP) AS percentage
0 Kudos

Hi Thorsten,

sadly I can't use division because it doesn't seem to be useable in open SQL only in abap cds, which isn't usable for me ...

When I tried using this:

select ...
CAST( table2~X as FLTP) / CAST( table2~Y as FLTP) AS percentage

The error message:
"The database field or the result type of the aggregate function X and the component "percentage" of "ET_ENTITYSET" are not compatible." shows up.

I already tried changing the EDM-Coretype, which was of no use.

Could it may be possible to use "loop at" to calculate the percentage after the select?

ThorstenHoefer
Active Contributor
0 Kudos

Hi Philipp,

which ABAP field type do you have in ET_ENTITYSET?

0 Kudos

Hi Thorsten,

I don't know where to find that, but shouldn't it be the same as the one in the structure I am using for that entityset?

In that case it would be NUMC, could that be the problem? The structure was used for a dynpro application before and I am recycling it because I am using most of the data used to fill that structure.

ThorstenHoefer
Active Contributor
0 Kudos

Hi Philipp,

can you wrap a

cast( .... as numc(10) ) 

around it?

0 Kudos

Hi Thorsten,

there the message:
"The expression that begins with "x" cannot be cast on the ddic types "numc"." shows up.

The datatype of X and Y is DEC.

0 Kudos

Hi Thorsten,

thanks for taking the time to give help, I got a solution now.