‎2019 Oct 08 7:32 AM
Hello,
I have a requirement.
In internal table itab, i have following field
Document no. Date Amount
1800000170 20190701 100000.00
1800000219 20190802 55000.00
1400000232 20190829 315.00
1800000170 20190829 5000.00-
1800000170 20190903 10000.00-
0900000332 20190918 492.24
i want to sort it to get the required result
Document no. Date Amount
1800000170 20190701 100000.00
1800000170 20190903 10000.00-
1800000170 20190829 5000.00-
1800000219 20190802 55000.00
1400000232 20190829 315.00
0900000332 20190918 492.24
where document number having starting date ( 20190701 ) should come first and
same document number comes together also.
Please provide the accurate solution for this.
thanks
‎2019 Oct 08 8:23 AM
Hi,
If you use SORT itab by document, it should have all document numbers together.
If you want to SORT date also within that group. you can use SORT itab document DESCENDING date ASCENDING based on your requirement or change sort directions as per your need.
Hope this helps.
Regards
GK
‎2019 Oct 08 8:23 AM
Hi,
If you use SORT itab by document, it should have all document numbers together.
If you want to SORT date also within that group. you can use SORT itab document DESCENDING date ASCENDING based on your requirement or change sort directions as per your need.
Hope this helps.
Regards
GK
‎2019 Oct 08 8:25 AM
Hello kunal gursahani,
For SORTING two fields of a internal table you need to specify the order explicitly for each field as shown below, below Code is for Example. Use the concept below and apply accordingly.
SORT ITAB BY Document no.ASCENDING
Amount DESCENDING.
By the way i don't see any Order in your required results that you have given.
Regards
‎2019 Oct 09 11:30 AM
kunal_gursahani
Use the below code, it will help to sort like you said,
TYPES: BEGIN OF tt_final,
docno TYPE char1024,
date TYPE sy-datum,
amount TYPE integer,
END OF tt_final.
DATA: lt_final TYPE STANDARD TABLE OF tt_final,
lwa_final TYPE tt_final.
lwa_final-docno = '1800000170'.
lwa_final-date = '20190701'.
lwa_final-amount = '100000.00'.
APPEND lwa_final to lt_final.
CLEAR: lwa_final.
lwa_final-docno = '1800000219'.
lwa_final-date = '20190902'.
lwa_final-amount = '55000.00'.
APPEND lwa_final to lt_final.
CLEAR: lwa_final.
lwa_final-docno = '1400000232'.
lwa_final-date = '20190829'.
lwa_final-amount = '315.00'.
APPEND lwa_final to lt_final.
CLEAR: lwa_final.
SORT lt_final by date ASCENDING.
‎2019 Oct 10 10:14 PM
"Please provide the accurate solution" - it's not the purpose of SCN to do one's job for them. From the question text it's not clear what exactly is the technical challenge. That's why some other answers are essentially trying to explain SORT command syntax.
There is no singular sort order in this case and you'd need to manipulate the records to put them together in this way. Obviously, at first the records are sorted by date but then you seem to want to group the records with the same number, regardless of their dates.
Because there are two separate tasks (sort by date, then group by number), I believe you'd need to use a second, temporary table to arrange the records. How exactly to go about it depends on data volume, how it will be presented, etc. It doesn't seem like a very challenging thing to program though, so I'm a bit confused by the question. There is no advanced knowledge required, simply think of an algorithm. Write up on a piece of paper the first solution that comes to mind, then see if it could be improved.
‎2019 Oct 11 8:10 AM
1.SORT itabA BY Date.
2.itabB = itabA.
3.SORT itabB BY Document no Date.
4.DELETE ADJACENT DUPLICATES FROM itabB COMPARING Document no.
5.SORT itabB BY Date.
6.
LOOP AT itabB.
LOOP AT itabA WHERE Document no = itabB-Document no.
itabC = itabA.
APPEND itabC.
ENDLOOP.
ENDLOOP.
7.THE result = itabC