2014 Oct 29 2:19 PM
Sorting internal table by second highest value
How to sort table by second highest value
| ||||||||||||||||||||||||||||||||||
I want the DKVR ‘01’ at the first position. SELECT doknr dokar dokvr doktl aennr |
2014 Oct 29 2:25 PM
Hi,
it's not a SORT, it not link with the logical of Ascending or Descending.
You have to make a LOOP and a MODIFY to change the index of the line.
regards
Fred
2014 Oct 29 2:42 PM
I was going to propose a solution but then thought, what is supposed to happen to the record DKVR ‘02’.
2014 Oct 29 7:08 PM
Hi,
Can you elaborate further and give the output and condition if any?
2014 Oct 30 5:36 AM
Hi,
If you want the 2nd highest. You cant achiev using a normal sort. Instead Loop through the records. like below:
Loop at table.
lv_cnt = lv_cnt + 1.
at new DOKVR.
lv_cnt = lv_cnt + 1.
endat.
When the lv-cnt reaches 2, then move that records to a new table and renumber the rest.
Endloop.
hope this helps.
Thanks,
Naveen
2014 Oct 30 6:17 AM
Hi,
Assuming you want to use your own sort logic .
Add a new column that will be served as a sorting column.
Loop on yor table and change the value of this column to reflect its "importance"
Then sort using this column .
Sample code:
FORM test_06 .
TYPES: BEGIN OF tp_sflight_1 .
TYPES index TYPE syindex .
INCLUDE TYPE sflight .
TYPES: END OF tp_sflight_1 .
DATA: it_sflight_1 TYPE TABLE OF tp_sflight_1 .
SELECT * INTO CORRESPONDING FIELDS OF TABLE it_sflight_1
FROM sflight UP TO 100 ROWS .
FIELD-SYMBOLS: <st_sflight_1> LIKE LINE OF it_sflight_1 .
LOOP AT it_sflight_1 ASSIGNING <st_sflight_1> .
CASE <st_sflight_1>-planetype .
WHEN '747-400' .
<st_sflight_1>-index = 1 .
WHEN OTHERS .
<st_sflight_1>-index = 2 .
ENDCASE.
ENDLOOP.
SORT it_sflight_1 BY index planetype .
ENDFORM . "test_06
Regards.
2014 Oct 30 6:30 AM
This is easy.
1- Get the internal table and sort by "DOKVR: descending order. This will always bring the highest value on the top.
2- Read the index 1 of this table. THis will return you the highest value.
3- Copy the internal table into another temporary internal table of the same type as 1 above.
4- Delete from internal table 2 where "DOKVR" = value present in fist record.
5- The remaining result is all sorted by the second highest value.
Ashish
2014 Oct 30 6:43 AM
Hi,
Not really.
The solution that you gave is good for the example that was given .
The solution that I gave is more general .
This code:
WHEN '747-400' .
<st_sflight_1>-index = 1 .
WHEN OTHERS .
<st_sflight_1>-index = 2 .
ENDCASE.
Represent a "complex" logic to give the row its position in the list .
Regards.
2014 Oct 30 9:27 AM
hi SMP,
sort it_draw ASCENDING by <field>.*sorting*
DELETE it_draw where sy-tabx = '0'.*delete the first entry in the internal table*
hope that it solves ur query....
2014 Oct 30 9:30 AM
hi SMP,
sort it_draw ASCENDING by <field>.*sorting*
DELETE it_draw where sy-tabx = '0'.*delete the first entry in the internal table*
hope that it solves ur query....
2014 Oct 31 1:57 AM
Vishu,
You cannot use sy-tabix = 0, as it may have one or more records.
Ashish