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

SQL calculation

Former Member
0 Likes
883

Can i write a sql that will sum up all quantity with debit/credit indicator. For example

In table of mseg, i need to sum up the MENGE which related to specific material number, but i have to care about SHKZG, i can't use to the following SQL

select sum( MENGE ) FROM mseg group by matnr.

Can i write a simple sql which will sum up MENGE with credit/debit indicator. Thanks

3 REPLIES 3
Read only

Former Member
0 Likes
573

Hi ,

Don't write select query . Use <b>SUM</b> command

select menge from mseg into itab.

SUM.

Regards,

Hemant

Read only

Former Member
0 Likes
573

hI,

No, you cannot write sql like this at max u can write:


select sum(menge) from mseg group by matnr shkzg.  "This will reduce resultset records.


or simply do :

loop at itab.

if itab-shkzg = <+>.
toatal_menge = total_menge + itab-menge.
else.
toatal_menge = total_menge - itab-menge.

endif.

Jogdand M B

Read only

Former Member
0 Likes
573

There is no SQL to help you sum one field according to the values of another field.

You need to select the data into an internal table and sum.

SELECT menge shkzg INTO itab.

DATA: sum type menge.

LOOP at itab.

CASE itab-shkzg.

WHEN 'H'.

sum = sum + itab-menge.

WHEN 'S'.

sum = sum - itab-menge.

ENDCASE.

ENDLOOP.

The + and - will vary according to situation.

Hope this helps