简体   繁体   中英

SUM the NUMC field in SELECT

I need to group a table by the sum of a NUMC-column, which unfortunately seems not to be possible with ABAP / OpenSQL.

My code looks like that:

    SELECT z~anln1
    FROM zzanla AS z
    INTO TABLE gt_
    GROUP BY z~anln1 z~anln2
    HAVING SUM( z~percent ) <> 100  " percent unfortunately is a NUMC -> summing up not possible

What would be the best / easiest practices here as I cannot alter the table itself?

Unfortunately the NUMC type is described as numerical text, so at the end it lands in the database as VARCHAR and that is why the functions like SUM or AVG cannot be used.

It all depends on how big your table is. If it is rather small you could get the group fields and the values for sum into an internal table and then sum it using COLLECT statement and eventually remove the rows for which the sum is equal 100%.

One solution is to define the field in the table using a more appropriate type.

NUMC is often used for key fields - like document numbers, which there would never be a reason to add together.

I didn't find a smooth solution. What I did, was to copy everything in an internal table, looped over it converting the NUMC values to DEC values. Grouping and summing up worked at that point. At the end, I converted the DEC values back to NUMC values.

It's been awhile. I came back to this post, because someone voted up my original answer. I was thinking about editing my old answer but I decided to post a new one. As this question was asked in 2017, there were some restictions but now it can be done by using CAST function in new OpenSQL.

SELECT z~anln1
FROM zzanla AS z
INTO TABLE @gt_
GROUP BY z~anln1, z~anln2
HAVING SUM( CAST( z~percent AS INT4 ) ) <> 100

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM