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

Inner Join with the same table two times

Former Member
0 Likes
5,401

Hi all,

I want to join these two select statement in one, it's possible?

Both selects works fine individually, but if i try to join it is wrong allways return to me sy-subrc = 4.

SELECT DISTINCT object_id "... ... ... 
       INTO CORRESPONDING FIELDS OF TABLE it_po
       FROM crmd_orderadm_h AS h   "Header Data
       INNER JOIN crmd_link AS l        "Link Data
        ON l~guid_hi EQ h~guid
       INNER JOIN bbp_pdorg AS o       " Org. Str
        ON l~guid_set EQ o~set_guid
       WHERE h~object_id IN r_po
         AND proc_org IN r_ekorg
         AND proc_group IN r_ekgrp
         AND posting_date IN r_date
         AND object_type EQ 'BUS2201'.

SELECT DISTINCT object_id "... ... ...
    INTO CORRESPONDING FIELDS OF TABLE it_po
    FROM crmd_orderadm_h AS h  "Header
    INNER JOIN crmd_link AS l       "Link Data
     ON l~guid_hi EQ h~guid
    INNER JOIN CRMD_PARTNER AS p  "Vendors
     ON l~guid_set EQ p~guid
    INNER JOIN but000 AS b
     ON b~partner_guid EQ p~partner_no
    WHERE h~object_id IN r_po
      AND posting_date IN r_date
      AND b~partner IN r_vendor
      AND object_type EQ 'BUS2201'.

The problem is i need to use two times the table CRMD_LINK to join the tables CRMD_PARTNER and BBP_PDORG.

If i try to do the following, don't work;

SELECT DISTINCT object_id "... ... ...
    INTO CORRESPONDING FIELDS OF TABLE it_po
    FROM crmd_orderadm_h AS h  "Header
    INNER JOIN crmd_link AS l       "Link Data
     ON l~guid_hi EQ h~guid
    INNER JOIN bbp_pdorg AS o       " Org. Str
        ON l~guid_set EQ o~set_guid
    INNER JOIN CRMD_PARTNER AS p  "Vendors
     ON l~guid_set EQ p~guid
    INNER JOIN but000 AS b
     ON b~partner_guid EQ p~partner_no
    WHERE h~object_id IN r_po
      AND proc_org IN r_ekorg
      AND proc_group IN r_ekgrp
      AND posting_date IN r_date
      AND b~partner IN r_vendor
      AND object_type EQ 'BUS2201'.

Is it possible to merge these two selects?

Thanks in advance.

1 ACCEPTED SOLUTION
Read only

kesavadas_thekkillath
Active Contributor
0 Likes
2,550

Is it a self join what you need ?

Hi all,

I want to join these two select statement in one, it's possible?

Both selects works fine individually, but if i try to join it is wrong allways return to me sy-subrc = 4.

SELECT DISTINCT object_id "... ... ... 
       INTO CORRESPONDING FIELDS OF TABLE it_po
       FROM crmd_orderadm_h AS h   "Header Data
       INNER JOIN crmd_link AS l        "Link Data
        ON l~guid_hi EQ h~guid
       INNER JOIN bbp_pdorg AS o       " Org. Str
        ON l~guid_set EQ o~set_guid
       WHERE h~object_id IN r_po
         AND proc_org IN r_ekorg
         AND proc_group IN r_ekgrp
         AND posting_date IN r_date
         AND object_type EQ 'BUS2201'.

SELECT DISTINCT object_id "... ... ...
    INTO CORRESPONDING FIELDS OF TABLE it_po
    FROM crmd_orderadm_h AS h  "Header
    INNER JOIN crmd_link AS l       "Link Data
     ON l~guid_hi EQ h~guid
    INNER JOIN CRMD_PARTNER AS p  "Vendors
     ON l~guid_set EQ p~guid
    INNER JOIN but000 AS b
     ON b~partner_guid EQ p~partner_no
    WHERE h~object_id IN r_po
      AND posting_date IN r_date
      AND b~partner IN r_vendor
      AND object_type EQ 'BUS2201'.

The problem is i need to use two times the table CRMD_LINK to join the tables CRMD_PARTNER and BBP_PDORG.

If i try to do the following, don't work;

SELECT DISTINCT object_id "... ... ...
    INTO CORRESPONDING FIELDS OF TABLE it_po
    FROM crmd_orderadm_h AS h  "Header
    INNER JOIN crmd_link AS l       "Link Data
     ON l~guid_hi EQ h~guid
    INNER JOIN bbp_pdorg AS o       " Org. Str
        ON l~guid_set EQ o~set_guid
    INNER JOIN CRMD_PARTNER AS p  "Vendors
     ON l~guid_set EQ p~guid
    INNER JOIN but000 AS b
     ON b~partner_guid EQ p~partner_no
    WHERE h~object_id IN r_po
      AND proc_org IN r_ekorg
      AND proc_group IN r_ekgrp
      AND posting_date IN r_date
      AND b~partner IN r_vendor
      AND object_type EQ 'BUS2201'.

Is it possible to merge these two selects?

Thanks in advance.

6 REPLIES 6
Read only

deepak_dhamat
Active Contributor
0 Likes
2,550

hi ,

why i could not find this table BBP_PDORG

which version you are using ....

regards

Deepak.

Edited by: Deepak Dhamat on Nov 12, 2010 10:10 AM

Read only

kesavadas_thekkillath
Active Contributor
0 Likes
2,551

Is it a self join what you need ?

Read only

0 Likes
2,550

Hi,

Deepak, i'm using SRM 7.0.

Keshav.T, i never use self joins... I don't know if this is what i nedd.

May be the example i put is a bit complicated, i have a main table and two secundary tables, the relationship between the main table with the other two is with other table... then i need to do a join two times with this table... you got my point?

Read only

0 Likes
2,550

Hi ,

Instead of Inner join then go Step by Step by collecting data from first two tables and

pass that table to another Query .

Ofcourse this will degrade your performance as you will be using for all entries , but still you will get all required details

Regards

Deepak.

Read only

0 Likes
2,550

Yes i did it, and works fine...

But, i want to know if it's possible to do this in one select...

Read only

0 Likes
2,550

It is possible but I'm not sure how it will affect the performance.

SELECT DISTINCT object_id "... ... ...

INTO CORRESPONDING FIELDS OF TABLE it_po

FROM crmd_orderadm_h AS h "Header Data

INNER JOIN crmd_link AS l "Link Data

ON lguid_hi EQ hguid

WHERE EXISTS (

  • code from your first query

SELECT DISTINCT object_id "... ... ...

FROM crmd_orderadm_h AS h "Header Data

INNER JOIN crmd_link AS l "Link Data

ON lguid_hi EQ hguid

INNER JOIN bbp_pdorg AS o " Org. Str

ON lguid_set EQ oset_guid

WHERE h~object_id IN r_po

AND proc_org IN r_ekorg

AND proc_group IN r_ekgrp

AND posting_date IN r_date

AND object_type EQ 'BUS2201' )

OR EXISTS (

  • code from your second query

SELECT DISTINCT object_id "... ... ...

FROM crmd_orderadm_h AS h "Header

INNER JOIN crmd_link AS l "Link Data

ON lguid_hi EQ hguid

INNER JOIN CRMD_PARTNER AS p "Vendors

ON lguid_set EQ pguid

INNER JOIN but000 AS b

ON bpartner_guid EQ ppartner_no

WHERE h~object_id IN r_po

AND posting_date IN r_date

AND b~partner IN r_vendor

AND object_type EQ 'BUS2201' ).

The code I've put together may not be 100% correct as I do not have SRM to test it; but does show what you need to do.

Basically what it is doing is returning the OBJECT_ID from the first join provided that it exists in the results of the two queries you had that worked individually. I would have done it slightly differently in a database system but ABAP doesn't seem to support union queries and SQL queries on the results of other SQL queries.

Regards,

John