Application Development 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: 

sort itab by select-options

Former Member
0 Kudos

Hi,

i have this code:


*
SELECT-OPTIONS:   S_MATNR   FOR MARA-MATNR.
*
...
*
  SELECT  * FROM MARA WHERE MATNR IN S_MATNR.
*
    CLEAR: WA_ITAB.
    WA_ITAB-MATNR = MARA-MATNR.
    WA_ITAB-MTART = MARA-MTART.
    WA_ITAB-MATKL = MARA-MATKL.

*
    APPEND WA_ITAB TO IT_ITAB.
*
  ENDSELECT.

now i want to sort the it_itab by the given select-options. Is this possible?

( select-options can have single valuse, ranges, meta characters)

Or is it possible to use select from table mara in order by select-options?

Regards, Dieter

1 ACCEPTED SOLUTION

raymond_giuseppi
Active Contributor
0 Kudos

Try something like

TABLES: mara.

SELECT-OPTIONS:   s_matnr   FOR mara-matnr.

TYPES: BEGIN OF type_mara,
         matnr LIKE mara-matnr,
         mtart LIKE mara-mtart,
         matkl LIKE mara-matkl,
       END OF type_mara.

DATA: wa_itab TYPE type_mara,
      lt_itab TYPE TABLE OF type_mara,
      gt_itab TYPE TABLE OF type_mara,
      lr_matnr TYPE RANGE OF mara-matnr,
      l_matnr LIKE LINE OF lr_matnr.

REFRESH: lt_itab, gt_itab.

SELECT matnr mtart matkl
  INTO TABLE lt_itab
  FROM mara
  WHERE matnr IN s_matnr.

SORT lt_itab BY matnr.

LOOP AT s_matnr WHERE sign EQ 'I'. " for each inclusive criteria
  REFRESH lr_matnr.
  APPEND s_matnr TO lr_matnr.
  LOOP AT lt_itab INTO wa_itab
    WHERE matnr IN lr_matnr.
    DELETE lt_itab. " only once
    APPEND wa_itab TO gt_itab.
  ENDLOOP.
ENDLOOP.
APPEND LINES OF lt_itab TO gt_itab. " if no inclusive criteria

Regards,

Raymond

9 REPLIES 9

Former Member
0 Kudos

Hi,

Could you please explain the req better..

If i have understood that the it_tab needs to be sorted then,

SORT it_tab by matnr ascending/descending. (choose whichever u want)

former_member555112
Active Contributor
0 Kudos

Hi,

First of all please avoid such a select statement. It causes a lot of performance problem.

Code like this

SELECT  *  FROM MARA 
INTO CORRESPONDING FIELDS OF TABLE IT_ITAB
WHERE MATNR IN S_MATNR.

Secondly what do you mean by SORT ITAB by SELECT_OPTIONS S_MATNR.

You yourself have said that the select-option can contain single values or multiple values or ranges.

So how should the SORT be?

Kinldy check with the requirment. It seems a little weird.

Regards,

Ankur Parab

0 Kudos
select 
           MATNR 
           MTART 
           MATKL
           from MARA
           into table IT_MARA
           where MATNR in S_MATNR group by matnr.

you can either use group by MATNR.

or after selecting you can write

sort it_MATA by MATNR.

0 Kudos

Hi Ankur,

when i insert this value in select-options:

200009

200007

200001 - 200005

200006

i get the it_itab in this order:

200001

200002

200003

200004

200005

200006

200007

200009

but i want this:

200009

200007

200001

200002

200003

200004

200005

200006

Is this possible?

regards, Dieter

0 Kudos

Hi,

No this is not possible .... you need to round about logic to acheive.

0 Kudos

Hi,

This is not achievable with the SORT statement.

You will have to acheive it by LOOPING on the select-options table and then reading your internal table ITAB and then appending it in other internal table.

Also what should be done incase the user chooses NE in the select-option or excludes some values in the range?

It really seems a weird requirement.

Regards,

Ankur Parab

raymond_giuseppi
Active Contributor
0 Kudos

Try something like

TABLES: mara.

SELECT-OPTIONS:   s_matnr   FOR mara-matnr.

TYPES: BEGIN OF type_mara,
         matnr LIKE mara-matnr,
         mtart LIKE mara-mtart,
         matkl LIKE mara-matkl,
       END OF type_mara.

DATA: wa_itab TYPE type_mara,
      lt_itab TYPE TABLE OF type_mara,
      gt_itab TYPE TABLE OF type_mara,
      lr_matnr TYPE RANGE OF mara-matnr,
      l_matnr LIKE LINE OF lr_matnr.

REFRESH: lt_itab, gt_itab.

SELECT matnr mtart matkl
  INTO TABLE lt_itab
  FROM mara
  WHERE matnr IN s_matnr.

SORT lt_itab BY matnr.

LOOP AT s_matnr WHERE sign EQ 'I'. " for each inclusive criteria
  REFRESH lr_matnr.
  APPEND s_matnr TO lr_matnr.
  LOOP AT lt_itab INTO wa_itab
    WHERE matnr IN lr_matnr.
    DELETE lt_itab. " only once
    APPEND wa_itab TO gt_itab.
  ENDLOOP.
ENDLOOP.
APPEND LINES OF lt_itab TO gt_itab. " if no inclusive criteria

Regards,

Raymond

0 Kudos

Hi Raymond,

that's what i'm looking for, very simple and good solution.

thanks for your help.

Regards, Dieter

Former Member
0 Kudos

Hi,

Maybe what you can do is... get the values entered in the select-options, then put it inside an internal table with a counter.

E.G.

select-option:

2001

2009

2005

2006

2002

2003

Put it inside an internal table with a counter....

1 2001

2 2009

3 2005

4 2006

5 2002

6 2003

Then sort it by that counter.

Just a suggestion.

Hope this helps.

Benedict.