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

unique value

former_member667493
Discoverer
0 Likes
2,675

I have 2 tables VBAK and vBAP. I wanted to get sale organisation, customer and distribution channel from vbak and matnr from VBAP table and compare with it_cond table.

I am new to ABAP. . Could you please let me know how to take unique value from 2 internal tables vbak and vbap.

6 REPLIES 6
Read only

Dominik_Tylczynski
SAP Champion
SAP Champion
2,126

See SAP Help on: DELETE itab - duplicates

Read only

former_member1716
Active Contributor
2,126

abapnew,

You have to do the following:

1) Write a simple select statement by Joining the two tables VBAK and VBELN with VBELN as the key field to join.

2) Once you have retrieved the value from the two tables delete the duplicate entries as mentioned above.

3) when you say you want to get the unique entry, what do you mean? Same materials can be used in different sales orders, here the combination of Sales order and Material will be unique. Are you trying to fetch the unique Material Numbers? Try to revisit your business purpose and get forward accordingly.

Below code should be fine for starting, however you have to decide what you want from the select query and decide accordingly. In case you want only unique materials then SORT the internal table only with MATNR and delete Adjacent duplicates of MATNR. if you want unique entry on combination of all the fields in select statement then below code should help.

SELECT h~vbeln,
       h~vkorg,
       h~vtweg,
       h~kunnr,
       i~matnr
  FROM vbak AS h
  INNER JOIN vbap AS i
  ON h~vbeln = i~vbeln
  INTO TABLE @DATA(it_tab).

IF sy-subrc EQ 0.
  SORT it_tab.
  DELETE ADJACENT DUPLICATES FROM it_tab COMPARING ALL FIELDS.
ENDIF.

Regards!

Read only

Sandra_Rossi
Active Contributor
0 Likes
2,126

If you know SQL, can you explain what you tried, so that to clarify your question?

Read only

michael_piesche
Active Contributor
0 Likes
2,126

You need to be more specific or at least show some coding of what you have done so far and where exactly you are stuck. You leave everybody guessing so far! Right now, I am assuming you are looking for Option c) below, but you might already be able to solve your problem with Option a). So please have a look and give some feedback:

Option a) If your program is selecting the data from the database, you should use the DISTINCT option with the SELECT statement, which will give you only distinct rows based on the selected column values.

SELECT DISTINCT columns FROM dbtab1 INNER JOIN dbtab2 on key1 = key1 WHERE ... .

Option b) If your program is not selecting the data from the database, but you have the data joined in one internal table already, you should use the SORT and DELETE ADJACENT DUPLICATES statements where you sort by your key-values that need to be distinct/unique and then delete the duplicates.

SORT itable BY key1 key2 ... .
DELETE ADJACENT DUPLICATES FROM itable COMPARING key1 key2 ... .

Option c) If your program is not selecting the data from the database, but you have two internal tables with data that is referenced by attributes, but these attributes are not part of the output you want, you need to LOOP through the main table, have an inner LOOP through the second table by referencing attributes, create out of those matches a third table and use the solution of Option b) to reduce it to distinct values.

LOOP AT itable1 ASSIGNING <fs1>.
  LOOP AT itable2 ASSIGNEN <fs2> where key1 = <fs1>-key1.
    MOVE-CORRESPONDING OF <fs2> TO joinedline.
MOVE-CORRESPONDING OF <fs1> TO joinedline.
INSERT joinedline INTO TABLE itable3. ENDLOOP.
ENDLOOP.
" Use Option b) to reduce itable3 to distinct rows for key-values

There are many other ways to solve your problem, including those that even have a better performance, but that include the right data/table definitions and more advanced statements with more options. But right now, I am trying to keep it simple for you, so you can understand more easily what needs to be done in order to solve your problem.

Read only

venkateswaran_k
Active Contributor
0 Likes
2,126

Hi Prema

What do you want to compare with condition table (it_cond).

Accordingly the query can be framed. However, I believe you want to do in ABAP Program?

Read only

michael_piesche
Active Contributor
0 Likes
2,126

abapnew, please follow up on your open question.

  • comment answers or your question if there are still open issues.
  • otherwise mark an answer as accepted if it helped you solve your problem
  • or post an answer of yourself and accept it if you found another useful solution yourself
  • or redirect your question to another question that is related and was useful to solve your problem
  • in the end, close your question