Skip to Content

Check if a non-key column of internal table holds same value for all records

Hello folks,

What is the best way to compare records of an internal table based on a single column values?

Say, I have an internal table with values as below:

A B C 1001 10 1001 20 X 1001 30

My requirement is to check if the column C contains identical values or not. In case of blank, all records should hold blank value in column C; in case 'X, all records should hold 'X'.

Kindly suggest the best approach to achieve this.

Thanks,

Rics

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

6 Answers

  • Best Answer
    Posted on Aug 18, 2016 at 09:45 PM

    And what should happen if you have 2 blank fields and 2 with value 'X'?

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Aug 19, 2016 at 04:00 AM

    Hi Rics,

    1. Copy the internal table to another internal table it_tab2[] = it_tab1[].

    2. Read the first record of it_tab1[].

    Read table it_tab1 into wa_tab1 index 1.

    3. Then, delete it_tab2[] with following condition,

    Delete it_tab2 where c = wa_tab1-c.

    4. If all the records of it_tab2 is deleted, then C field has identical values. If it_tab2 is not completed deleted then C field has values which are not identical

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Aug 19, 2016 at 05:57 AM

    To check if there is only one given value in a column of an internal table, you can do the following:

    READ TABLE itab WITH KEY c = 'X' TRANSPORTING NO FIELDS.

    DATA(subrc1) = sy-subrc.

    LOOP AT itab TRANSPORTING NO FIELDS WHERE c <> 'X'.

    EXIT.

    ENDLOOP.

    DATA(subrc2) = sy-subrc.

    IF subrc1 = 0 AND subrc2 <> 0.

    "only X

    ENDIF.

    If you can assert that you have exactly two values as in your scenario above, you can do it as follows:

    READ TABLE itab WITH KEY c = 'X' TRANSPORTING NO FIELDS.

    DATA(subrc1) = sy-subrc.

    READ TABLE itab WITH KEY c = ' ' TRANSPORTING NO FIELDS.

    DATA(subrc2) = sy-subrc.

    IF subrc1 = 0 AND subrc2 <> 0.

    "only X

    ELSEIF subrc1 <> 0 AND subrc2 = 0.

    "only space

    ELSE.

    "both

    ENDIF.


    Or in a modern way (elegant, but in fact too much read accesses ...):


    IF line_exists( itab[ c = 'X' ] ) AND

    NOT line_exists( itab[ c = ' ' ).

    "only X

    ELSEIF NOT line_exists( itab[ c = 'X' ] ) AND

    line_exists( itab[ c = ' ' ] ).

    "only space

    ELSE.

    "both

    ENDIF.


    In case that there are more values possible than 'X' and ' ', see my answer above.

    Add a comment
    10|10000 characters needed characters exceeded

    • Assuming you're on release 7.40 or higher, you can use IF NOT ( <expression1> EQUIV <expression2> ) to implement an "exclusive or" statement.

      So to refine Horst's example a little :

      TYPES: BEGIN OF ts_itab, c TYPE c, END OF ts_itab.

      TYPES tt_itab TYPE STANDARD TABLE OF ts_itab WITH DEFAULT KEY.

      DATA(itab) = VALUE tt_itab( ( c = 'X' ) ( c = 'X' ) ( c = 'X' ) ).

      IF NOT ( line_exists( itab[ c = 'X' ] ) EQUIV line_exists( itab[ c = ' ' ] ) ).

      "table contains only X or only ' '

      ENDIF.

  • Posted on Aug 18, 2016 at 08:37 PM

    Probably just a SORT using COMPARE BY.

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Aug 19, 2016 at 07:21 AM

    Hi All,

    Thank you for your quick responses, which were all very helpful.

    My problem has been solved with @Horst Keller solution. Thank you for your time and patience. 😊

    Regards,

    Rics

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Aug 22, 2016 at 08:23 AM

    itab2[] = itab1[].

    delete itab2 where c = itab1[1]-c.

    if itab2[] is not initial and

    itab1[1]-c is initial.

    itab1-c = 'X'.

    else

    itab1-c = space.

    endif.

    modify itab1 transforting c where a = itab1[1]-a.

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.