Application Development 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: 

How to select a max value in a database table for display

Former Member
0 Kudos

Dear Experts

I have a Z database table.

In that i have three fields.

Review Cycle Review step GUID of object

1 1 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

1 2 xxxxxxxxxxxxxxxxxxxxxxxxxxxxx

2 1 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

2 2 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

3 1

Here my requirwement for output dispay is that i need to select review_cycle 3 only.

Should i add counter in the loop when i loop at this internal table and display the counter value ?

Thanks in advance

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi Swarna,

See if this sample code helps you,

SELECT SINGLE MAX( etimes )

FROM zmailhist

INTO wa-etimes

WHERE vbeln EQ p_vbeln AND

kunnr EQ p_kunnr.

wa-etimes = wa-etimes + 1.

ELSE.

wa-etimes = 1.

ENDIF.

Revert back for further help

5 REPLIES 5

kesavadas_thekkillath
Active Contributor
0 Kudos

-How to select a max value in a database table for display-

if u wnat to select the max value in Review Cycle

then from database query use " select max ".

if its ins itab then sort based on that field descending and read it

Former Member
0 Kudos

Hi Swarna,

See if this sample code helps you,

SELECT SINGLE MAX( etimes )

FROM zmailhist

INTO wa-etimes

WHERE vbeln EQ p_vbeln AND

kunnr EQ p_kunnr.

wa-etimes = wa-etimes + 1.

ELSE.

wa-etimes = 1.

ENDIF.

Revert back for further help

0 Kudos

SELECT SINGLE MAX( BELNR ) FROM BKPF INTO V_LAST_BELNR.

this fetches you the record with maximum belnr. (the first maximum)

Edited by: Sumit Nene on Jun 8, 2009 12:09 PM

Former Member
0 Kudos

Hi Swarna,

select single max( review_cycle )

from db_tab

into work_area.

if review_cycle with value > 3 exists it gets u the greater value..

u shl use where condition..

select single review_cycle

from db_tab

into work_area

where review_cycle = 3.

chk sy-subrc.

Regards.

Former Member
0 Kudos

Hi Swarna

Try this code.

DATA:
 T_ITAB LIKE TABLE OF ZTABLE,
 FS_ITAB LIKE LINE OF T_ITAB,
 WA_ITAB TYPE ZTABLE-REVIEW_CYCLE .

SELECT * FROM ZTABLE INTO TABLE T_ITAB.
SORT T_ITAB DESCENDING BY REVIEW_CYCLE .

READ TABLE T_ITAB INTO FS_ITAB INDEX 1.
MOVE FS_ITAB-REVIEW_CYCLE TO WA_ITAB.

LOOP AT T_ITAB INTO FS_ITAB.
  IF FS_ITAB-REVIEW_CYCLE EQ WA_ITAB.
    WRITE:/
       FS_ITAB-REVIEW_CYCLE ,
       FS_ITAB-REVIEW_STEP,
       FS_ITAB-GUID.
  ENDIF.
ENDLOOP.

Regards

Hareesh Menon