Skip to Content
author's profile photo Former Member
Former Member

Comparison amoung two tables

Ok, I'm attempting to write my first ABAP report. I am trying to write a report that allows a user to input a unit of measure and a material group. Then it will look at every material in that group. If a material's base unit of measure or alternate unit of measure(s) do not equal the user-supplied unit then that material will be written to the screen. Otherwise nothing will happen. Here is what I have so far:

REPORT ZBASE_UNIT_SELECT.

PARAMETERS: PA_MATKL TYPE MARA-MATKL, PA_UNIT TYPE MARA-MEINS.

DATA: IT_MARA TYPE TABLE OF MARA,

WA_MARA LIKE LINE OF IT_MARA.

DATA: IT_MARM TYPE TABLE OF MARM,

WA_MARM LIKE LINE OF IT_MARM.

START-OF-SELECTION.

SELECT * FROM MARA INTO TABLE IT_MARA WHERE MATKL = PA_MATKL.

LOOP AT IT_MARA INTO WA_MARA.

if wa_mara-meins = pa_unit.

This is where I get stuck. I need to determine of the base unit of measure (MARA-MEINS) equals the supplied unit (PA_UNIT) and if it does then I need to move on to the next one. If it doesn't match then I need to compare the alternate unit of measures (MARM-MEINH) to the supplied unit (PA_UNIT). How can I do this comparison using two different tables?

Any help is more than appreciated!

Thanks,

Aaron

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

11 Answers

  • Best Answer
    author's profile photo Former Member
    Former Member
    Posted on Jan 09, 2007 at 02:13 PM

    hi

    try this

    1.select the field (MARM-MEINH) into one internal table itab1 supp.

    2.then

    LOOP AT IT_MARA INTO WA_MARA.

    if wa_mara-meins EQ pa_unit.

    continue.

    else.

    read itab1 into wa_itab1

    compare the alternate unit of measures (MARM-MEINH) to the supplied unit (PA_UNIT).

    endif.

    endloop.

    reward if useful

    Message was edited by:

    neha gupta

    Add a comment
    10|10000 characters needed characters exceeded

    • Former Member Uwe Schieferstein

      Uwe, thank you so much for that solution you posted! I am a bit ashamed to say that I am using your solution with very minor changes. I will be trying to develop my own solution once I get some time but for now I will be using yours. Thanks again!

      Thanks to all how helped me. I wish I could give everybody a "Very Helpful" rating but it limits how many you can give out. I apologize for not realizing that the solution was working and that I was just testing it when there wasn't any data available to me.

      Thanks a million!

      Aaron

  • author's profile photo Former Member
    Former Member
    Posted on Jan 09, 2007 at 02:11 PM

    Hi Aaron,

    You need to use IF..ELSEIF statements for your requirement.

    IF <condtion1>.

    statements(1).

    ELSEIF <condtion2>.

    statements(2).

    ENDIF.

    If <condtion1> is true then statements(1) are executed,if not it will check whether <condtion1> is true.If it is true then statements(2) are executed.

    LOOP AT IT_MARA INTO WA_MARA.

    if wa_mara-meins = pa_unit.

    >>Do as per ur requirement(1)

    elseif wa_mara-meins = pa_unit.

    >>Do as per ur requirement(2)

    endif.

    ENDLOOP.

    Thanks,

    Vinay

    Add a comment
    10|10000 characters needed characters exceeded

    • Former Member

      Hi Vinaykumar,

      Thanks for your reply but I'm a bit confused. Aren't your two comparisons doing the same thing?

      LOOP AT IT_MARA INTO WA_MARA.

      <b>if wa_mara-meins = pa_unit.</b>

      >>Do as per ur requirement(1)

      <b>elseif wa_mara-meins = pa_unit.</b>

      >>Do as per ur requirement(2)

      endif.

      To me both the if and elseif are comparing the same fields/values. My trouble is once I loop into the work area (WA_MARA) and determine that WA-MEINS <> (NE) to PA_UNIT how do I look at the alternate unit of measures if they are in a different table than MARA. They are in MARM.

      Thanks,

      Aaron

  • Posted on Jan 09, 2007 at 02:21 PM

    Hello Aaron

    You could simplify your task using two simple SELECT statements:

    DATA:
      gt_mara_ok     TYPE STANDARD TABLE OF mara,
      gt_mara_nok    TYPE STANDARD TABLE OF mara.
    
    
      SELECT * FROM mara INTO TABLE gt_mara_ok
        WHERE  matkl = pa_matkl
        AND     ( meins = pa_unit    OR
                      meinh = pa_unit ).
    
      SELECT * FROM mara INTO TABLE gt_mara_nok
        WHERE  matkl = pa_matkl
        AND     ( meins <> pa_unit    AND
                      meinh <> pa_unit ).

    Regards

    Uwe

    Add a comment
    10|10000 characters needed characters exceeded

    • Former Member

      Hi Uwe,

      Thanks for your reply. I don't understand why those two select statements will work. MEINS is only in table MARA and MEINH is only in table MARM. How can I compare both MEINS and MEINH to PA_UNIT in one select statement?

      Thanks,

      Aaron

  • author's profile photo Former Member
    Former Member
    Posted on Jan 09, 2007 at 03:11 PM

    REPORT ZBASE_UNIT_SELECT.

    PARAMETERS: PA_MATKL TYPE MARA-MATKL, PA_UNIT TYPE MARA-MEINS.

    DATA: IT_MARA TYPE TABLE OF MARA,

    WA_MARA LIKE LINE OF IT_MARA.

    DATA: IT_MARM TYPE TABLE OF MARM,

    WA_MARM LIKE LINE OF IT_MARM.

    START-OF-SELECTION.

    SELECT * FROM MARA INTO TABLE IT_MARA WHERE MATKL = PA_MATKL.

    select * from marm into table it_marm where ( ur condition).

    or..

    select * into corresponding fields of table it_marm from marm for all entries in it_mara where matnr = it_mara-matnr.

    LOOP AT IT_MARA INTO WA_MARA.

    if wa_mara-meins = pa_unit.

    ur requirement.

    else.

    read table it_marm with key meinh = pa_unit.

    if sy-subrc = 0.

    ur requirement.

    endif.

    endif.

    hope this helps..

    Regards

    CNU

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jan 09, 2007 at 03:19 PM

    If you don't mind, look at the following:

    DATA: BEGIN OF T_TABLE OCCURS 0,

    MATNR,

    MEINS

    MEINH

    END OF TABLE.

    SELECT AMATNR AMEINS B~MEINH INTO TABLE T_TABLE

    FROM MARA AS A INNER JOIN MARM AS B

    ON AMATNR = BMATNR

    WHERE AMEINS <> PA_UNIT AND BMEINH <> PA_UNIT.

    My guess is this will fill table T_TABLE with materials where the base unit of measure (MEINS) and alternate unit of measure (MEINH) do not equal the supplied unit (PA_UNIT). Is this correct?

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jan 09, 2007 at 03:42 PM

    DATA:

    gt_mara_ok TYPE STANDARD TABLE OF mara,

    gt_mara_nok TYPE STANDARD TABLE OF mara.

    SELECT * FROM mara INTO TABLE gt_mara_ok

    WHERE matkl = pa_matkl

    AND ( meins = pa_unit OR

    meinh = pa_unit ).

    SELECT * FROM mara INTO TABLE gt_mara_nok

    WHERE matkl = pa_matkl

    AND ( meins <> pa_unit AND

    meinh <> pa_unit ).

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jan 09, 2007 at 04:34 PM

    Here is what I have now (thanks to your wonderful help)

    PARAMETERS: PA_MATKL TYPE MARA-MATKL, PA_UNIT TYPE MARA-MEINS.
    
    DATA: BEGIN OF T_TABLE OCCURS 0,
            MATNR TYPE MARA-MATNR,
            MEINS TYPE MARA-MEINS,
            MEINH TYPE MARM-MEINH,
          END OF T_TABLE.
    
    
    SELECT A~MATNR A~MEINS B~MEINH INTO TABLE T_TABLE
          FROM MARA AS A INNER JOIN MARM AS B
          ON A~MATNR = B~MATNR
          WHERE A~MEINS <> PA_UNIT AND B~MEINH <> PA_UNIT and A~MATKL = PA_MATKL.
    
    if sy-subrc = 4.
    write: / 'No records found'.
    endif.
    
    
    LOOP AT T_TABLE.
      IF T_TABLE-MEINS = PA_UNIT OR T_TABLE-MEINH = PA_UNIT.
        DELETE T_TABLE WHERE MATNR = T_TABLE-MATNR.
      ENDIF.
    ENDLOOP.
    
    LOOP AT T_TABLE.
      WRITE: / T_TABLE-MATNR.
    ENDLOOP.

    However, no records are found EVER. I guess there is something wrong with the select statement but I'm not sure what.

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jan 09, 2007 at 04:52 PM

    Hi,

    Manually check in SE16 if there is any material in the tables MARA & MARM that is not having the UOM of what you have given in the selection screen with the material group (MATKL)..

    Thanks,

    Naren

    Add a comment
    10|10000 characters needed characters exceeded

    • Former Member Former Member

      Hi Aaron,

      I executed ur code... seems everything is fine....i get the output for different mtkl and pa_unit.

      Pls check again manually in SE16 for those two tables.

      or..

      SELECT AMATNR AMEINS B~MEINH INTO T_TABLE

      FROM MARA AS A INNER JOIN MARM AS B

      ON AMATNR = BMATNR

      WHERE A~MATKL = PA_MATKL.

      APPEND T_TABLE. " Put Breakpoint here.

      ENDSELECT.

      IF SY-SUBRC = 4.

      WRITE: / 'No records found'.

      ENDIF.

      ***************************************************************

      put break point there and check the values manually for the entries that it has records or not...

      Regards

      CNU

  • author's profile photo Former Member
    Former Member
    Posted on Jan 09, 2007 at 05:09 PM

    Hi,

    Did you check it in the table MARM for the material 42..Whether there is any record for GAL..

    Thanks,

    Naren

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jan 09, 2007 at 05:18 PM

    This reply goes to everybody. I am so sorry I didn't realize that I was looking at the tables in our production box but I was developing in our dev box. It seems like I have my code working now but I won't know until I transport it to our test box and test it out. I appreciate all of the help I received so far. I'm keeping my fingers crossed! If my solution works I will change the question to answered and award points accordingly Thanks again!

    Aaron

    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.