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

selection order in SELECT

Former Member
0 Likes
2,077

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.

23 REPLIES 23
Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
1,780

Hello David,

Can i ask u sth. WHY DO U NEED TO DO THIS ?????

BR,

Suhas

Read only

Former Member
0 Likes
1,780

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.

Read only

Former Member
0 Likes
1,780

i have a BIG doubt???

Do u have more than one SELECT-OPTION for an SINGLE FIELD?

Read only

Former Member
0 Likes
1,780

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.

Read only

Former Member
0 Likes
1,780

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.

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
1,780

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

Read only

0 Likes
1,780

Hello ZeroAbaper,

This code can't handle situations where both LOW and HIGH values are entered.

Thanks.

Read only

0 Likes
1,780

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.

Read only

0 Likes
1,780

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

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
1,780

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

Read only

0 Likes
1,780

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..??

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
1,780

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

Read only

0 Likes
1,780

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.

Read only

0 Likes
1,780

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.

Read only

0 Likes
1,780

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

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
1,780

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.

Read only

Former Member
0 Likes
1,780

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

Read only

Former Member
0 Likes
1,780

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.

Read only

ThomasZloch
Active Contributor
0 Likes
1,780

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

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
1,780

Hello Thomas,

Are you proposing to SELECT within the LOOP. Will this not affect the program's performance ??

BR,

Suhas

Read only

0 Likes
1,780

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

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
1,780

Point taken Thomas. I wish QA reviewers out here are listening to Thomas )

BR,

Suhas

Read only

Former Member
0 Likes
1,780

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.