‎2008 Nov 20 8:34 AM
Hello ppl,
I have a select-option field on my selection screen.
I am selecting data from a database table based on the entries from this select-option.
The problem is that in the select-option, the user can enter the data in any order.
For eg.
9000 to 9500,
8452,
8245,
8375, etc.
The SELECT statement selects entries in the internal table in ascending order ALWAYS.
eg.
8254,
8375,
8452,
9000
to
9500, etc.
Please help me find a solution to sort this internal table according to the order of entries on the selection screen.
Thanks.
‎2008 Nov 20 8:38 AM
Hello David,
Can i ask u sth. WHY DO U NEED TO DO THIS ?????
BR,
Suhas
‎2008 Nov 20 8:42 AM
Hello Suhas,
Sorry, I can't disclose the actual requirement.
But, I hope you have understood the requirement.
Please let me know if you have any solution to achieve this.
Thanks.
‎2008 Nov 20 8:57 AM
i have a BIG doubt???
Do u have more than one SELECT-OPTION for an SINGLE FIELD?
‎2008 Nov 20 8:54 AM
have an internal table with two fields.
one for the entries in select option and the next field for storing the order.
eg
value order
8024 1
450 2
500 3
once the select query fills the internal table , refer the previous internal table for order .
copy the order field to ur new internal table for respective values
sort the table based on the newly added field.
‎2008 Nov 20 8:58 AM
Hi.. You cannot do this within the select statement itself. But you can try this.
select field1 field2....
from database_table
into internal_table_1
where field = select-option.
loop at select-option.
read table internal_Table_1 into work_area with key field = select-option-low.
if sy-subrc = 0.
append work_area to internal_Table_2.
endif.
endloop.
The internal_Table_2 should be of same structure as of internal_Table_1.
The contents of internal_table_2 will have the entries as you asked.....
Regards.
‎2008 Nov 20 9:09 AM
Hello ZeroABAPer,
Tell me how will you handle the case for the SELECT-OPTION having LOW & HIGH values using yor bit of code?
BR,
Suhas
‎2008 Nov 20 9:18 AM
Hello ZeroAbaper,
This code can't handle situations where both LOW and HIGH values are entered.
Thanks.
‎2008 Nov 20 9:22 AM
David,
You do not require any code at all, incase, if both HIGH and LOW values are entered. Your selection will automatically fetch the necessary values.
You can modify the code, which i have given by introducing an IF condition.
if Select-option-high is initial.
...code here.
endif.
"Proceed from here as per your requirement.
‎2008 Nov 20 9:24 AM
Suppose you are having SY-TABIX as select-option.
You enter the values as 15 to 50.
You need not modify anything in this case.
If you enter single entries like 15,20,25 and 50.
In this case, execute the code which i gave.
I hope.. it helps...
Regards...
‎2008 Nov 20 9:31 AM
Hello Zeroabaper,
Lemme point out sth.
S_SCR-LOW = 15, S_SCR-HIGH = 20.
After SELECT stmt IT1 contents --> 15, 16, 17, 18, 19, 20.
If we use ur code IT2 will have only 15 & not all the entries in IT1. Correct me if i am wrong.
BR,
Suhas
‎2008 Nov 20 9:35 AM
Suhas,
You are right .. ... but as per my previous post, you dont have to use the code for internal table 2, if SELECT-OPTION-HIGH is having any value....
Introduce the IF condition and proceed....
what do you say..??
‎2008 Nov 20 9:42 AM
Hello,
With David not disclosing his queer requirement & we have no option bt to work on this )
Frankly i was thinking the only way to do so will be the way u ve mentioned. But then dropped the idea bcoz of the probs as mentioned by me above.
BR,
Suhas
‎2008 Nov 20 9:42 AM
Hello,
The select-option internal table looks like this:
SIGN OPTION LOW HIGH
I EQ 8446
I BT 9260 9275
I EQ 9224
I EQ 8275
I need the entries in exactly the same order.
eg.
8446
9261
9265
9224
8275
Thanks.
‎2008 Nov 20 9:50 AM
Well..
You can try this...
loop at s_option
if s_option = 'EQ'.
wa_itab-entry = s_option-low.
append wa_itab to t_itab.
continue.
endif.
if s_option = 'BT'.
do.
w_count = s_option-low.
wa_itab-entry = w_count.
w_count = w_count + 1.
append w_count to t_itab.
while w_count > s_option-high.
endloop.
endloop.
‎2008 Nov 20 9:53 AM
As per the above code, you will have the entries of the select options, as you wish.
You can then loop into this table...read the table in which you have the data.. and append that data which you get into a third internal table.
That internal table will have the values as you wish..
‎2008 Nov 20 10:07 AM
Hello,
Came up with a novel soln. But will work only for numeric values )
REPORT ZTEST01335.
TABLES: T001.
TYPES:
BEGIN OF TY_T001,
BUKRS TYPE BUKRS,
END OF TY_T001.
DATA:
IT1 TYPE STANDARD TABLE OF TY_T001,
IT2 TYPE STANDARD TABLE OF TY_T001,
WA TYPE TY_T001,
V_CNT TYPE I VALUE 1.
SELECT-OPTIONS: S_OPT FOR T001-BUKRS.
SELECT BUKRS
INTO TABLE IT1
FROM T001
WHERE BUKRS IN S_OPT.
IF SY-SUBRC = 0.
SORT IT1 BY BUKRS.
LOOP AT S_OPT.
IF S_OPT-OPTION = 'EQ'.
READ TABLE IT1 INTO WA
WITH KEY BUKRS = S_OPT-LOW BINARY SEARCH.
IF SY-SUBRC = 0.
APPEND WA TO IT2.
CLEAR WA.
ENDIF.
ELSEIF S_OPT-OPTION = 'BT'.
V_CNT = S_OPT-LOW.
DO.
READ TABLE IT1 INTO WA
WITH KEY BUKRS = V_CNT BINARY SEARCH.
IF SY-SUBRC = 0.
APPEND WA TO IT2.
CLEAR WA.
ENDIF.
V_CNT = V_CNT + 1.
IF V_CNT > S_OPT-HIGH.
EXIT.
ENDIF.
ENDDO.
ENDIF.
ENDLOOP.
ENDIF.
‎2008 Nov 20 9:51 AM
Dear David,
just try this.
ranges:
lr_fkart for vbrk-fkart,
lr_fkart-sign = 'I'.
lr_fkart-option = 'EQ'.
lr_fkart-low = 'SO_low'.
lr_fkart-high = 'SO_high'.
append lr_fkart.
now use this lr_fkart in where condition.(OR)
once fetching data into an internal table .just compare this with the order range table (lr_fkart) and append it to some another table. hope this may help you.
UR's
GSANA
‎2008 Nov 20 10:21 AM
User input is stored in an internal table such as S_USER_INPUT
Entries from select query are stored in internal table S_INT_TAB
Now For S_USER_INPUT-OPTION EQ 'BT' then fill internal table S_USER_INPUT2 with all enties between, similary for 'EQ' fill enties in S_USE_INPUT2.
Then LOOP AT S_USER_INPUT2 INTO WA.
READ TABLE S_INT_TAB FROM WA
IF SUCCESSFUL READ THEN MOVE WA to INT_TAB2
ELSE
CONTINUE.
ENDLOOP.
‎2008 Nov 20 10:24 AM
How about this approach:
You loop at each row of the selection-option, inside the loop you move that row to another range, do the select using that range, appending to your internal result table.
Thomas
‎2008 Nov 20 10:27 AM
Hello Thomas,
Are you proposing to SELECT within the LOOP. Will this not affect the program's performance ??
BR,
Suhas
‎2008 Nov 20 11:37 AM
It depends. If you're using a full primary or secondary key, and there is not let's say more than a few hundred user selections at max, it should not be a problem. It will be a little slower than a single select, however one should balance this performance aspect with the maintainability of the program code (the easier to understand, the better).
One could also try two versions and compare.
Thomas
‎2008 Nov 20 12:01 PM
Point taken Thomas. I wish QA reviewers out here are listening to Thomas )
BR,
Suhas
‎2008 Nov 20 12:05 PM
Hi,
Try the following logic. It wont affect ur performance advesely.
SELECTION-SCREEN begin OF BLOCK b1 with FRAME.
select-OPTIONS: s_matnr for mara-matnr.
SELECTION-SCREEN END OF BLOCK b1.
select * from mara into CORRESPONDING FIELDS OF TABLE it_mara
where matnr in s_matnr.
if sy-subrc <> 0.
endif.
loop at s_matnr.
if s_matnr-high is initial.
loop at it_mara into wa_mara where matnr = s_matnr-low.
MOVE-CORRESPONDING wa_mara to wa_mara1.
append wa_mara1 to it_mara1.
clear wa_mara1.
endloop.
else.
loop at it_mara into wa_mara WHERE matnr >= s_matnr-low
and matnr < s_matnr-high.
MOVE-CORRESPONDING wa_mara to wa_mara1.
append wa_mara1 to it_mara1.
clear wa_mara1.
enloop.
endif.