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

internal table calculations

Former Member
0 Likes
1,001

HELLO FRIENDS,

I have a scenario where, i need to check the entries i a ztable for a field, i have to count how many entries in the field have value X, and how many entries have value Y,

Total number of X entries has to be multiplied by value *, and total number of entries of Y got to be multiplied by 4, and

total has tobe added.

If any one can give me little detailed code, along with declarations, it will be really greatfull to me.

thanks.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
978

Hi,

Please try this.


TABLES: ZTABLE.
 
DATA: COUNT_X TYPE I,
      COUNT_Y TYPE I,
      TOTALS  TYPE I.
      
RANGES: R_FIED1 FOR ZTABLE-FIELD1.

R_FIELD1-SIGN = 'I'.
R_FIELD1-OPTION = 'NP'.
R_FIELD1-LOW = '008%'.
APPEND R_FIELD1.

SELECT COUNT( DISTINCT FIELD1 )
INTO COUNT_X 
FROM ZTABLE
WHERE FIELD1 LIKE '008%'.

WRITE: / COUNT_X.
 
SELECT COUNT( DISTINCT FIELD1 )
INTO COUNT_Y 
FROM ZTABLE
WHERE FIELD1 IN R_FIELD1.

WRITE: / COUNT_Y.

OR 

SELECT FIELD1 COUNT( * )
INTO COUNT_X 
FROM ZTABLE
WHERE FIELD1 LIKE '008%'.
 
WRITE: / COUNT_X.

SELECT FIELD1 COUNT( * )
INTO COUNT_Y 
FROM ZTABLE
WHERE FIELD1 IN R_FIELD1.
 
WRITE: / COUNT_Y.

SKIP.
COUNT_X = COUNT_X * <...>.    <--- any number
COUNT_Y = COUNT_Y * 4.
TOTALS  = COUNT_X + COUNT_Y.

WRITE: / COUNT_X, COUNT_Y, TOTALS.

Regards,

Ferry Lianto

9 REPLIES 9
Read only

Former Member
0 Likes
978

sorry friends , i forgot toput the condition,

the fields with entries X are those char strings which tart with 008 in the start of the string out of 10 charecters. the field is a 10 charecter string.

thanks again.

Read only

0 Likes
978

the field with entries Y are those who donot have 008 at their starting of the 10 char string.

thanks

Read only

Former Member
0 Likes
978

Hi,

Please try something like this.


TABLES: ZTABLE.

DATA:  COUNT_X TYPE I.

SELECT COUNT( FIELD1 )
INTO COUNT_X 
FROM ZTABLE
WHERE FIELD1 LIKE '008%'.

WRITE: / COUNT_X.

Regards,

Ferry Lianto

Read only

0 Likes
978

hi ferry thanks for your reply,

but when i execute it says.

count( field1) is only valid in the extended form count( distinct field1).

can u plz look in to it.

thank you.

Read only

Former Member
0 Likes
978

Hi...

check this code ...

data :

w_count type i.

w_count1 type i.

w_sum type i.

select count( field1 )

from ztable

into w_count

where field1 like '008%'.

select count( field1 )

from ztable

into w_count1

where field1 not like '008%'.

w_sum = w_count + w_count1.

write : w_count , w_count1 , w_sum.

regards,

sai ramesh

Read only

Former Member
0 Likes
979

Hi,

Please try this.


TABLES: ZTABLE.
 
DATA: COUNT_X TYPE I,
      COUNT_Y TYPE I,
      TOTALS  TYPE I.
      
RANGES: R_FIED1 FOR ZTABLE-FIELD1.

R_FIELD1-SIGN = 'I'.
R_FIELD1-OPTION = 'NP'.
R_FIELD1-LOW = '008%'.
APPEND R_FIELD1.

SELECT COUNT( DISTINCT FIELD1 )
INTO COUNT_X 
FROM ZTABLE
WHERE FIELD1 LIKE '008%'.

WRITE: / COUNT_X.
 
SELECT COUNT( DISTINCT FIELD1 )
INTO COUNT_Y 
FROM ZTABLE
WHERE FIELD1 IN R_FIELD1.

WRITE: / COUNT_Y.

OR 

SELECT FIELD1 COUNT( * )
INTO COUNT_X 
FROM ZTABLE
WHERE FIELD1 LIKE '008%'.
 
WRITE: / COUNT_X.

SELECT FIELD1 COUNT( * )
INTO COUNT_Y 
FROM ZTABLE
WHERE FIELD1 IN R_FIELD1.
 
WRITE: / COUNT_Y.

SKIP.
COUNT_X = COUNT_X * <...>.    <--- any number
COUNT_Y = COUNT_Y * 4.
TOTALS  = COUNT_X + COUNT_Y.

WRITE: / COUNT_X, COUNT_Y, TOTALS.

Regards,

Ferry Lianto

Read only

0 Likes
978

thanks ferry,

the first one was only giving distinct values, there are duplicate values too, we nee to count them too. and the second option was giving error saying there should be a goup by cluse.

thanks.

can u once again plz look into it.

Read only

0 Likes
978

friends what i feel is ,

if we create an internal table and only copy those field1 values into that itab where

the field1 contains '008%' and we can use sy-dbcnt.

but i dont know how to code this logic,

can any one plz give me code for this logic thanks.

Read only

0 Likes
978

thanks ferry and ramesh,

i used both of ur code to write the code.

now its working.

thank you so much.

i have a last question, if i use this logic of bringing all fields from database and then checking the sy-dbcnt a good way.

please suggest me what to do.

thanks once again.