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

max function in select statemnt

Former Member
0 Likes
1,533

Hi,

Can someone help me with what is wrong in the following query...How to use the max function

in ABAP program select statement??

Thanks..

LOOP AT IT_ANLA_ANLZ INTO WRK_ANLA_ANLZ.

SELECT KANSW

FROM ANLC INTO TABLE IT_ANLC WHERE GJAHR IN ( SELECT MAX(GJAHR) FROM ANLC ) and ANLN1 = WRK_ANLA_ANLZ-ANLN1

AND BUKRS = WRK_ANLA_ANLZ-BUKRS .

APPEND IT_ANLC.

ENDLOOP.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
866

Hi,

it should be like:

SELECT KANSW

FROM ANLC INTO TABLE IT_ANLC WHERE GJAHR IN ( SELECT MAX( GJAHR ) FROM ANLC ) and ANLN1 = WRK_ANLA_ANLZ-ANLN1

AND BUKRS = WRK_ANLA_ANLZ-BUKRS .

U should give space for GJAHR in brackets. U replace ur code by above..

Hope it works!!

Regards,

Pavan

4 REPLIES 4
Read only

Former Member
0 Likes
866

Hi,

You have write your select query once,

and then loop on the internal table into

which data will get populated for its display.



SELECT KANSW
FROM ANLC INTO TABLE IT_ANLC WHERE GJAHR IN 
( SELECT MAX(GJAHR) FROM ANLC ) and ANLN1 = WRK_ANLA_ANLZ-ANLN1
AND BUKRS = WRK_ANLA_ANLZ-BUKRS .

Then loop at internal table: 

LOOP AT IT_ANLA_ANLZ INTO WRK_ANLA_ANLZ.

write: / wrk_anla_anlz-fields.

Endloop.

Hope it helps

Regards

Mansi

Read only

I355602
Product and Topic Expert
Product and Topic Expert
0 Likes
866

Hi,

Refer:


DATA : V_GJAHR TYPE ANLC-GJAHR.

DATA : WA_ANLC LIKE LINE OF IT_ANLC.

SELECT MAX(GJAHR) FROM ANLC INTO V_GJAHR.

LOOP AT IT_ANLA_ANLZ INTO WRK_ANLA_ANLZ.
  SELECT KANSW
   FROM ANLC INTO WA_ANLC WHERE GJAHR = V_GJAHR
                            AND ANLN1 = WRK_ANLA_ANLZ-ANLN1
                            AND BUKRS = WRK_ANLA_ANLZ-BUKRS .

  APPEND WA_ANLC TO IT_ANLC.
  CLEAR WA_ANLC.
ENDLOOP.

Hope this helps you.

Regards,

Tarun

Read only

Former Member
0 Likes
866

u can store the data of table ANLC into internal table IT_ANLC and then,

SORT IT_ANLC by GJAHR descending.

Read table IT_ANLC index 1 into wa_ANLC.

now the field wa_ANLC-GJAHR has the value of max year. now u can continue with the same code and compare gjahr with wa_ANLC-gjahr instead of using max.

Read only

Former Member
0 Likes
867

Hi,

it should be like:

SELECT KANSW

FROM ANLC INTO TABLE IT_ANLC WHERE GJAHR IN ( SELECT MAX( GJAHR ) FROM ANLC ) and ANLN1 = WRK_ANLA_ANLZ-ANLN1

AND BUKRS = WRK_ANLA_ANLZ-BUKRS .

U should give space for GJAHR in brackets. U replace ur code by above..

Hope it works!!

Regards,

Pavan