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: 

Comparison amoung two tables

Former Member
0 Kudos

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

1 ACCEPTED SOLUTION

Former Member
0 Kudos

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

27 REPLIES 27

Former Member
0 Kudos

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

0 Kudos

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

Former Member
0 Kudos

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

0 Kudos

Neha,

That looks close to being what I need but where you say: <b>select the field (MARM-MEINH) into one internal table itab1 supp.</b> how do I do that?

Thanks,

Aaron

0 Kudos

hi Aaron

do it before the loop

0 Kudos

Neha, I mean how do I do it? When I say this is my first ABAP program I'm not lying. This is my very first one so the things that seem simple to you are a mountain to me. Thanks for your help.

Aaron

0 Kudos

Hello Aaron

Here is the complete solution using SELECT statements:

*&---------------------------------------------------------------------*
*& Report  ZUS_SDN_SELECT_EXISTS
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  zus_sdn_select_exists.


TABLES: mara.

DATA:
  gs_mara        TYPE mara,
  gt_mara_ok     TYPE STANDARD TABLE OF mara,
  gt_mara_nok    TYPE STANDARD TABLE OF mara.


PARAMETERS:
  p_matkl      TYPE mara-matkl OBLIGATORY DEFAULT '00200',  " PC system
  p_unit       TYPE mara-meins OBLIGATORY DEFAULT 'ST'.     " piece


START-OF-SELECTION.

  SELECT * FROM mara AS m INTO TABLE gt_mara_ok
    WHERE ( matkl = p_matkl   AND
            meins = p_unit  )
    AND EXISTS ( SELECT * FROM marm
                   WHERE matnr = m~matnr
                   AND   meinh = p_unit ).


  SELECT * FROM mara AS m INTO TABLE gt_mara_nok
    WHERE  matkl = p_matkl
    AND NOT EXISTS ( SELECT * FROM marm
                   WHERE matnr = m~matnr
                   AND   meinh = p_unit ).

  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
    EXPORTING
      I_STRUCTURE_NAME                  = 'MARA'
    TABLES
      t_outtab                          = gt_mara_ok
    EXCEPTIONS
      PROGRAM_ERROR                     = 1
      OTHERS                            = 2.
  IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.

  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
    EXPORTING
      I_STRUCTURE_NAME                  = 'MARA'
    TABLES
      t_outtab                          = gt_mara_nok
    EXCEPTIONS
      PROGRAM_ERROR                     = 1
      OTHERS                            = 2.
  IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.
END-OF-SELECTION.

Regards

Uwe

0 Kudos

Hello Aaron

There was a little mistake in the previous posting which I have corrected:

*&---------------------------------------------------------------------*
*& Report  ZUS_SDN_SELECT_EXISTS
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  zus_sdn_select_exists.


TABLES: mara.

DATA:
  gs_mara        TYPE mara,
  gt_mara_ok     TYPE STANDARD TABLE OF mara,
  gt_mara_nok    TYPE STANDARD TABLE OF mara.


PARAMETERS:
  p_matkl      TYPE mara-matkl OBLIGATORY DEFAULT '00200',  " PC system
  p_unit       TYPE mara-meins OBLIGATORY DEFAULT 'ST'.     " piece


START-OF-SELECTION.

  SELECT * FROM mara AS m INTO TABLE gt_mara_ok
    WHERE ( matkl = p_matkl   AND
            meins = p_unit  )
    AND EXISTS ( SELECT * FROM marm
                   WHERE matnr = m~matnr
                   AND   meinh = p_unit ).



  SELECT * FROM mara AS m INTO TABLE gt_mara_nok
    WHERE ( matkl = p_matkl  AND
            meins <> p_unit )     " missing in previous post
    AND NOT EXISTS ( SELECT * FROM marm
                   WHERE matnr = m~matnr
                   AND   meinh = p_unit ).

  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
    EXPORTING
      i_structure_name = 'MARA'
    TABLES
      t_outtab         = gt_mara_ok
    EXCEPTIONS
      program_error    = 1
      OTHERS           = 2.
  IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.

  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
    EXPORTING
      i_structure_name = 'MARA'
    TABLES
      t_outtab         = gt_mara_nok
    EXCEPTIONS
      program_error    = 1
      OTHERS           = 2.
  IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.

END-OF-SELECTION.

Regards

Uwe

0 Kudos

Uwe, thanks for the reply but that does not give me any results. I tried a simple test case where there is only one material in a group. I input EA as the unit of measure and that should give me a result of no records found (EA is an alternate unit of measure for that material). Then I used the same material group but used GAL as the alternate unit of measure. That should give me a value of one record (material) because GAL is not the base unit of measure and it is not one of the alternative unit of measure.

0 Kudos

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

uwe_schieferstein
Active Contributor
0 Kudos

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

0 Kudos

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

Former Member
0 Kudos

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

0 Kudos

Srinu K, I was with you up until this:

else.

read table it_marm with key meinh = pa_unit.

if sy-subrc = 0.

ur requirement.

What will <b>read table it_marm with key meinh = pa_unit.</b> do for me?

0 Kudos

Yes it is correct, it does not fill the table which has pa_unit as meins and meinh

it reads the table it_marm which has alternate unit of measure = pa_unit. But it hits the first record of the table which satisfies to the condition. so u can extend the logic for more precise like..

Read table It_marm with key meinh = pa_unit matnr = wa_mara-matnr.

so, it checks for the each material number.... and check the meinh too.

hope this solves ur problem

P.S: Did u solve the SApscript problem?

Regards

CNU

0 Kudos

Srinu, no I did not solve my SAPScript problem yet.

Former Member
0 Kudos

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?

Former Member
0 Kudos

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

Former Member
0 Kudos

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.

Former Member
0 Kudos

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

0 Kudos

I did. If I use material group 011 there should be a material number in there of 42. mat 42 has a base unit of each (EA). When I run the program an input 011 for the group and GAL for the unit I should get a result of material number 42 but I get nothing.

0 Kudos

Here is what I have so far:

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

sy-subrc always returns 4 so that means there is something wrong with my select statement.

0 Kudos

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

Former Member
0 Kudos

Hi,

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

Thanks,

Naren

Former Member
0 Kudos

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

Former Member
0 Kudos

hi Aaron

is ur problem solved...

0 Kudos

Yes, it is, thanks to you and everybody else that chimed in on this topic. Thank you so much for all of your help!

Aaron