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

maximum value from table field.

Former Member
0 Likes
2,071

hi all,

I wrote below code to select the maximum value of field cust_id from table ZPND_CUST_TAB,

but its not working as i required its giving last entry of the table.

can any one tell me what should i change in below code to get maximum value from cust_id field of  ZPND_CUST_TAB table.

DATA temp_cust_id TYPE ZPND_CUST_TAB-CUST_ID.

SELECT MAX( CUST_ID FROM  ZPND_CUST_TAB

                        INTO temp_cust_id

                        WHERE CUST_ID > 0.

   WRITE  temp_cust_id.

15 REPLIES 15
Read only

Former Member
0 Likes
2,010

DATA it_temp_cust_id TYPE table of ZPND_CUST_TAB-CUST_ID.

data wa_temp_cust_id TYPEZPND_CUST_TAB-CUST_ID.

SELECT MAX( CUST_ID FROM  ZPND_CUST_TAB

                        INTO table it_temp_cust_id

                        WHERE CUST_ID > 0.


sort the internal table it_temp_cust_id in Descending order.

read table it_temp_cust_id into wa_temp_cust_id index 1.

if sy-subrc = 0.

   WRITE wa_temp_cust_id-cust_id.

endif.

Read only

0 Likes
2,010

hi raji ,

this code is also not working.



DATA it_temp_cust_id TYPE table of ZPND_CUST_TAB.

data wa_temp_cust_id TYPE ZPND_CUST_TAB-CUST_ID.

SELECT MAX( CUST_ID FROM  ZPND_CUST_TAB

                         INTO table it_temp_cust_id

                         WHERE CUST_ID > 0.

sort it_temp_cust_id Descending.

read table it_temp_cust_id into wa_temp_cust_id index 1.

if sy-subrc = 0.

    WRITE wa_temp_cust_id.

endif.

Read only

0 Likes
2,010

Hi,

once try this.

sort the internal table based on cust__id.


sort it_temp_cust_id Descending by cust_id.

Read only

Former Member
0 Likes
2,010

Hi,

what data type have u used for cust_id?

while creating in entries you must follow the sequence. Like crate imp id with no 1 as first then 2 and so on.

Regards -

Makarand

Read only

0 Likes
2,010

hi makarand,

field type is char

Read only

0 Likes
2,010

This message was moderated.

Read only

0 Likes
2,010

Hi santosh,

then you must create imp id in ascending order. Else it will return the last entry of the table.

You will find almost all the fields in standard table which holds the value from number range

like ebeln vbeln are of char type. But because of number range, system generates numbers in ascending order.

You can check by deleting the data of your ztable and create entries with increasing imp id number.

If the imp is sequence is not fixed.

then, use select * from table.

Get it sorted in descending order.

and read the table with index 1.

And you can get the max number.

Regards-

Makarand

Read only

0 Likes
2,009

Read only

nabheetscn
SAP Champion
SAP Champion
0 Likes
2,009

What is the issue ..? What is the data in the table...? and type of the field..?

Read only

Former Member
0 Likes
2,009

Hi,

in your code, please remove the WHERE clause in the SELECT statement.

Regards,

Klaus

Read only

nabheetscn
SAP Champion
SAP Champion
0 Likes
2,009

Rather than saying the code is not working kindly put up your table data and output which is coming as a screen shot.

Nabehet

Read only

matt
Active Contributor
0 Likes
2,009

Quite. My telepathic skills dwindle towards the end of the week.

Read only

Former Member
0 Likes
2,009

Here is my problem,

in image as shown ,

program-1 is giving the value of variable temp_cust_id(numc type) that is '202' but in program-2 when i assign that value to parameters variable cust_id it is not filling the cust_id parameters with 202.

if you get my problem please reply me with solution.

Read only

0 Likes
2,009

Santosh

First of all one is selection screen other is output both are different.

If you would have simply pressed F1 on default key word you would have come to know the reason. Move your code under the event LOAD-OF-PROGRAM.

SAP HELP

"When an executable program is started using SUBMIT, all the values val specified at program start using DEFAULT are passed between the events LOAD-OF-PROGRAM and INITIALIZATION to the associated parameters para. "

Nabheet

Read only

0 Likes
2,009

Totally confused.

If you are giving the cust_id in select option, they why are you coding select max ?.

that cust_id should be in where cluase of select query.