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 Max value.

Former Member
0 Likes
7,172
P.O         Line        A-date      B-date
1234       00010    01.06.2009    02.06.2009
1234       00010    03.06.2009    09.06.2009
1234       00010    03.06.2009    02.06.2009
1234       00020    05.06.2009    06.06.2009
1234       00020    07.06.2009    02.06.2009
1234       00020    03.06.2009    09.06.2009

from abov itab I want max date of A-date and Max date of B-date .

e.g. max date for 10 of A-date is 03.06.2009 and max date of B-date is 09.06.2009 for line item 10.

How to get it.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
2,887

First write: sort itab by A-DATE.

then move hdate = itab-Adate.

then again sort itab by B-date.

H2date = itab-Bdate.

this will solve ...check it

Sas

20 REPLIES 20
Read only

Former Member
0 Likes
2,887

sort internal table by these dates fields in descending orders.

Regds,

Anil

Read only

former_member585060
Active Contributor
0 Likes
2,887

Hi,

Try with below syntax

SORT itab  ASCENDING BY a-date b-date  DESCENDING.

so for every new line item first record will be max date.

Regards

Bala Krishna

Read only

Former Member
0 Likes
2,888

First write: sort itab by A-DATE.

then move hdate = itab-Adate.

then again sort itab by B-date.

H2date = itab-Bdate.

this will solve ...check it

Sas

Read only

0 Likes
2,887

>

> First write: sort itab by A-DATE.

> then move hdate = itab-Adate.

>

> then again sort itab by B-date.

> H2date = itab-Bdate.

>

> this will solve ...check it

>

> Sas

No SaS.

How About sort the descending w.r.t. Line A-date B-date?Also,Stable Sorting?

Read only

0 Likes
2,887

Yes Amit you are correct.:)

Sort itab by a-date Descending to get the Max value...

and That is enough as it is on date-section no issues

Regards

sas

Read only

former_member212005
Active Contributor
0 Likes
2,887

Jim...what you can do is...

Concatenate the PO number and Line Item number into one field say POITEM....

SORT the internal table containing data in ascending order of both dates....

i.e.

SORT itab BY POITEM A_date ascending B_Date ascending.

then...loop this internal table..

LOOP AT itab INTO istab.

AT END OF POITEM.

" This record will contain the max date..

ENDAT.

ENDLOOP.

Hope this helps!

Please let me know , if it is not clear...

Your data in the new itab should contain...after sorting...

123410 01.06.2009 02.06.2009

123410 03.06.2009 02.06.2009

123410 03.06.2009 09.06.2009

123420 05.06.2009 06.06.2009

Read only

Former Member
0 Likes
2,887

use SORT itab by A-date B-date DESENDING,

read the first line in the table, u ill get the max values of A-date, B-date.

Rgds,

Pavan

Read only

Former Member
0 Likes
2,887

Hi,

Try

Sort itab by adat bdat descending.

this will resolve your problem for sure.

Regards,

Vijay

Read only

Former Member
0 Likes
2,887

Hi,

The requirement is not as simple as it sounds.

First please tell me will the internal table have records for only 1 order.

If not the coding becomes tougher.

It is tough as it is since you have to sort based on item as well.

You need to have a copy of your internal table.

T1 will have the entire data.

T2 will be used to keep the highest values

Sort T1 ascending by order and item.

Loop at T1.

Check if order and item are same as previous loop.

if not.

T2-Order = T1-order

T2-item = T1-item.

T2-Adate = T1-date

T2-Bdate = T1-Bdate.

append T2.

else.

read T2 for this order and Item.

if T2-Adate < T1-Adate.

T2-Adate = T1-Adate.

endif.

if T2-Bdate < T1-Bdate.

T2-Bdate = T1-Bdate.

endif.

modify T2.

Endif.

Endloop.

Now your T2 will have your Order Number Item number and higest dates for Adate and Bdate.

Hope this helps.

Please note that this may not be the best way of doing it and someone else might/might not give you the best possible way. This is how i perceived it and i am pasting this pseudo code.

Regards,

Pramod

Edited by: Pramod M on Jul 3, 2009 1:29 PM

Read only

Former Member
0 Likes
2,887

Hi,

You can have the logic this way..

SORT ITAB BY PO ITEM.
LOOP AT ITAB.
    IF L_ADATE LE ITAB-ADATE.
       L_ADATE = ITAB-ADATE.
    ENDIF.

    IF L_BDATE LE ITAB-BDATE.
       L_BDATE = ITAB-BDATE.
   ENDIF.

   AT END OF ITEM.
      " You Have the  Max dates in field L_ADATE &  L_BDATE .
      Clear : L_ADATE,  L_BDATE. 
  ENDIF.

ENDLOOP.

Read only

0 Likes
2,887

>

>

SORT ITAB BY PO ITEM.
> LOOP AT ITAB.
>     IF L_ADATE LE ITAB-ADATE.
>        L_ADATE = ITAB-ADATE.
>     ENDIF.
> 
>     IF L_BDATE LE ITAB-BDATE.
>        L_BDATE = ITAB-BDATE.
>    ENDIF.
> 
>    AT END OF ITEM.
>       " You Have the  Max dates in field L_ADATE &  L_BDATE .
>       Clear : L_ADATE,  L_BDATE. 
>   ENDIF.
> 
> ENDLOOP.

On the First iteration of Loop, There is no value in either L_Adate or L_Bdate.So if the max value of date is present at first position then your Logic will lead inconsistent data.

Read only

0 Likes
2,887

HI Amit,

Yes You are right...just i was thinking as if it works with interger and char value...i have added the AT NEW Event to get the value.

SORT ITAB BY PO ITEM.
LOOP AT ITAB.
   AT NEW OF ITEM.
       L_ADATE = ITAB-ADATE.
       L_BDATE = ITAB-BDATE.
  ENDIF.

    IF L_ADATE LE ITAB-ADATE.
       L_ADATE = ITAB-ADATE.
    ENDIF.
 
    IF L_BDATE LE ITAB-BDATE.
       L_BDATE = ITAB-BDATE.
   ENDIF.
 
   AT END OF ITEM.
      " You Have the  Max dates in field L_ADATE &  L_BDATE .
      Clear : L_ADATE,  L_BDATE. 
  ENDIF.
 
ENDLOOP.

Read only

0 Likes
2,887

@ Amit,

Sorry amit, but i do not think it will lead to inconsistency.

On the first iteration the value in l_adate will be 000000 which is less than whatever date the internal table wil have.

So the first if statement will be executed successfully.

Now l_date will have date value of first record and from second iteration it continues its normal execution.

But this will work only if data for 1 PO is present in the table. For multiple POs this logic will fail.

Regards,

Pramod

Read only

Former Member
0 Likes
2,887
Hi all, 
Thanks for quick reply.


1234       00010    05.06.2009     02.06.2009
1234       00010    03.06.2009     09.06.2009
1234       00010    03.06.2009     02.06.2009



out of this 3 line i want only one record.

of Max date.
like this  
1234       00010    05.06.2009     09.06.2009


below is code for it.

data : BEGIN OF itab occurs 0,
       ebeln type ebeln,
       ebelp type ebelp,
       end of itab .
data: BEGIN OF  itab2 OCCURS 0,
       ebeln type ebeln,
       ebelp TYPE ebelp,
       hrdat type sy-datum,
       padat type sy-datum,
      END OF itab2.

     itab-ebeln = 1234.
     itab-ebelp = 10.
     APPEND itab.

     itab-ebeln = 1234.
     itab-ebelp = 20.
     APPEND itab.



     itab2-ebeln = 1234.
     itab2-ebelp = 10.
     itab2-hrdat = '20090605'.
     itab2-padat = '20090602'.
     APPEND itab2.


     itab2-ebeln = 1234.
     itab2-ebelp = 10.
     itab2-hrdat = '20090603'.
     itab2-padat = '20090609'.
     APPEND itab2.

     itab2-ebeln = 1234.
     itab2-ebelp = 10.
     itab2-hrdat = '20090603'.
     itab2-padat = '20090602'.
     APPEND itab2.

 loop at itab.
       loop at itab2 where ebeln = itab-ebeln and ebelp = itab-ebelp.
               write :/ itab-ebeln, itab-ebelp ,itab2-hrdat,itab2-padat.
        endloop.
       endloop.
Read only

0 Likes
2,887

Hi,

I just think this code will print all the 3 entries in that table.

Is that what you wanted.

Regards,

Pramod

Read only

0 Likes
2,887

Jim...since you are using nested LOOP's all you have to do..is compare for the largest...inside the LOOP..


loop at itab.
       loop at itab2 where ebeln = itab-ebeln and ebelp = itab-ebelp.
            IF itab2-hrdat GT gv_date.
                    gv_date = itab2-hrdat.
            ENDIF.

            AT LAST.
"               Now you will have the largest hrdate in gv_date.
            ENDAT.
" Do similar coding for padat as well                 
        endloop.
       endloop.

Hope it helps!

Read only

Former Member
0 Likes
2,887

Max date of A-date and B-date. output like this

A-date B-date

1234 00010 05.06.2009 09.06.2009

Read only

Former Member
0 Likes
2,887

Max date of A-date and B-date. output like this

A-date                    B-date
1234 00010    05.06.2009            09.06.2009

Read only

0 Likes
2,887

hi

Try to use DELETE ADJACENT DUPLICATES FROMcommand in final internal table...

Read only

Former Member
0 Likes
2,887

Vin ans is match.

Thanks to all,