Inner join select

I had my data declaration as below.
DATA :BEGIN OF t_plaf OCCURS 0,
       normt LIKE mara-normt,
       matnr LIKE plaf-matnr,
       pwwrk LIKE plaf-pwwrk,
       paart LIKE plaf-paart,
       verid LIKE plaf-verid,
       auffx LIKE plaf-auffx,
       psttr LIKE plaf-psttr,
       gsmng LIKE plaf-gsmng,
      END OF t_plaf.
corresponding inner join statement
  SELECT maranormt  plafmatnr
         plafpwwrk  plafpaart  plaf~verid
         plafauffx  plafpsttr  plaf~gsmng
    INTO TABLE t_plaf
    FROM plaf
   INNER JOIN mara
      ON plafmatnr EQ maramatnr
   WHERE plaf~paart = 'LA'.
Now I have changed the declaration as below
DATA : BEGIN OF t_plaf OCCURS 0,
       normt LIKE mara-normt.
        INCLUDE STRUCTURE plaf.
DATA : END OF t_plaf.
now i have changed the data declaration as mentioned above and need a inner join statement for the data declaration i have specified.
Any suggestions,
MAdhu

Hi Madhu,
USE INTO CORRESPONDING option instead of INTO TABLE.
SELECT maranormt plafmatnr
plafpwwrk plafpaart plaf~verid
plafauffx plafpsttr plaf~gsmng
<b>INTO CORRESPONDING FIELDS OF TABLE t_plaf</b>
FROM plaf
INNER JOIN mara
ON plafmatnr EQ maramatnr
WHERE plaf~paart = 'LA'.
Thanks,
Vinay

Similar Messages

  • Dynamic table name in an inner join - select statement

    Hi,
    Please can you let me know if is possible to use a Dynamic table name in an inner join?
    Something like the statement below? (It works in a simple select statement but not in an inner join)
    SELECT  *
         INTO CORRESPONDING FIELDS OF <t_itab>
          FROM <Dynamic table name> INNER JOIN pa0050 ON
          ( <Dynamic table name>pernr =  pa0050pernr )
           WHERE <Dynamic table name>~pernr = it_pernr-l_pernr
           AND pa0050~bdegr = f_bdegr.
    Any help would be apprecited very much.
    Thanks & Regards.

    Hi,
    Check this link.
    [http://help.sap.com/saphelp_nw04/helpdata/en/fc/eb39c4358411d1829f0000e829fbfe/frameset.htm]
    [Re: accessing dynamic internal table's fields??;
    hope it'll help u.
    Regards,
    Sneha.
    Edited by: sneha kumari on Jun 18, 2009 1:57 PM

  • Inner join-select -primary key in table issue

    Hi ,
            Iam using FEBKO(header) and FEBEP(item) in inner join  select .But the datas fetching by this selct in not correct.The analysis is the is no common primary fields in the both table.
    Question 1-> Can i use inner join without common primary key in the both tables, weather it possible to make a select without common primary key in both table. Please kindly let me know.
    Question 2-> What is the other possible way to give the selct for both table(better performance)
    Regards,
    Veera

    Hi,
    When you use INNER JOIN in this case, link your tables based on KUKEY and ESNUM fields, bcoz there can be many items under a single header. So this will work for you, even from the performance point of view.
    Hope this is helpful to you. If you need further information, revert back.
    Reward all the helpful answers.
    Regards
    Nagaraj T

  • Inner Join Select query

    hello all,
    Below is a inner join query
        SELECT aebeln aebelp apackno bzekkn b~ps_psp_pnr
                     FROM ( ( ekpo AS a INNER JOIN ekkn AS b
                              ON aebeln = bebeln
                              AND  aebelp = bebelp )
                             INNER JOIN prps AS c
                              ON bps_psp_pnr = cpspnr )
                     INTO TABLE t_wbspo
                      WHERE a~ebeln IN s_ebeln
                       AND  c~psphi IN s_psphi
                       AND  stufe = c_task.
    I want to modify this select and put one more join on ekkn-aufnr
    and aufk-afunr .
    Now for this i write the below code
      SELECT aebeln aebelp apackno bzekkn bps_psp_pnr baufnr
               FROM ( ( ( ekpo AS a INNER JOIN ekkn AS b
                        ON aebeln = bebeln
                        AND  aebelp = bebelp )
                       INNER JOIN prps AS c
                        ON bps_psp_pnr = cpspnr )
                        INNER JOIN aufk AS d
                        ON baufnr = daufnr )
               INTO TABLE t_wbspo
                WHERE a~ebeln IN s_ebeln
                 AND  c~psphi IN s_psphi
                 AND  d~aufnr IN s_aufnr
        AND  stufe = c_task.
    By the above way does it puts a join on ekkn-aufnr and aufk-aufnr ?
    Is the above way the right way to do it ?
    Please confirm
    regards

    Hello Bhanu,
    Do you face any performance issue? Query looks good now, further you can refer to following threads to know more on JOIN-
    Re: Inner join
    http://help.sap.com/saphelp_nw04/helpdata/en/cf/21ec77446011d189700000e8322d00/frameset.htm
    Thank You,
    Nishikant Kumbhar.

  • Inner join -select options

    Hi all,
    I have a query, but it doesn't work when i give select-options range table in WHERE clause.
    SELECT-OPTIONS  :
            l_bukrs  FOR  WA_INPUT-bukrs NO INTERVALS ,
      SELECT
                VBAK~BUKRS_VF AS BUKRS   "COMPANY CODE
                PA0001~ENAME     "SALES EMPLOYEE NAME
                KNA1~NAME1       "CLIENT
                VBKD~BSTKD       "PO NO
                VBAK~VBELN       "SO NO
                VBAK~AUDAT       "SO DATE
                VBAP~POSNR       "LINE
                VBAP~KWMENG      "QTY
                VBAP~VRKME       "UNIT
                VBAP~NETPR       "RATE
                VBAP~NETWR       "ORDER VALUE
                VBAP~WAERK       "DOCUEMNT CURRENCY
                VBAP~MATNR       "MATERIAL NO
                VBAP~ARKTX       "MATERIAL NAME
                VBKD~KURSK       "EXCHANGE RATE
                    INTO CORRESPONDING FIELDS OF TABLE IT_OPSALES
                      FROM  VBAP
                                LEFT OUTER JOIN  VBPA ON VBPAVBELN = VBAPVBELN AND VBPA~PARVW = 'AF'
                                INNER JOIN PA0001 ON PA0001PERNR = VBPAPERNR
                                INNER JOIN VBAK ON VBAKVBELN  = VBAPVBELN
                                INNER JOIN KNA1 ON KNA1KUNNR  = VBAKKUNNR
                                INNER JOIN VBKD ON VBKDVBELN = VBAKVBELN AND VBKDPOSNR = VBAPPOSNR
                      WHERE  BUKRS  IN L_BUKRS
    it wont support when i give L_bukrs in inner join clause..!!
    any idea to change the query??
    thaks and regards
    Jose

    Hi,
    Declare the select option as  s_bukrs .
    SELECT-OPTIONS :
    S_bukrs FOR WA_INPUT-bukrs NO INTERVALS ,
    SELECT
    VBAK~BUKRS_VF AS BUKRS "COMPANY CODE
    PA0001~ENAME "SALES EMPLOYEE NAME
    KNA1~NAME1 "CLIENT
    VBKD~BSTKD "PO NO
    VBAK~VBELN "SO NO
    VBAK~AUDAT "SO DATE
    VBAP~POSNR "LINE
    VBAP~KWMENG "QTY
    VBAP~VRKME "UNIT
    VBAP~NETPR "RATE
    VBAP~NETWR "ORDER VALUE
    VBAP~WAERK "DOCUEMNT CURRENCY
    VBAP~MATNR "MATERIAL NO
    VBAP~ARKTX "MATERIAL NAME
    VBKD~KURSK "EXCHANGE RATE
    INTO CORRESPONDING FIELDS OF TABLE IT_OPSALES
    FROM VBAP
    LEFT OUTER JOIN VBPA ON VBPAVBELN = VBAPVBELN AND VBPA~PARVW = 'AF'
    INNER JOIN PA0001 ON PA0001PERNR = VBPAPERNR
    INNER JOIN VBAK ON VBAKVBELN = VBAPVBELN
    INNER JOIN KNA1 ON KNA1KUNNR = VBAKKUNNR
    INNER JOIN VBKD ON VBKDVBELN = VBAKVBELN AND VBKDPOSNR = VBAPPOSNR
    WHERE BUKRS IN S_BUKRS
    Hope this helps you.
    Thanks & Regards,
    Y.R.Prem Kumar

  • How to use INNER JOIN in such case

    when i program as below[CASE1]. the code is able to active.
    CASE1:
    ==========================
    DATA: WK_BUKRS LIKE T001-BUKRS,
          WK_BUTXT LIKE T001-BUTXT,
          WK_TABLE(4) TYPE C VALUE 'T001'.
    START-OF-SELECTION.
      WK_BUKRS = 'DECN'.
      PERFORM GET_BUTXT USING WK_BUKRS WK_TABLE
                     CHANGING WK_BUTXT.
      WRITE: WK_BUTXT.
      FORM GET_BUTXT USING I_BUKRS LIKE T001-BUKRS
                           I_TABLE
                  CHANGING O_BUTXT LIKE T001-BUTXT.
        SELECT SINGLE BUTXT
                 INTO O_BUTXT
                 FROM (I_TABLE)
                WHERE BUKRS = I_BUKRS.
      ENDFORM.                   
    ===========================
    but when I need to INNER JOIN another table [CASE2]
    CASE2:
    =======================
    DATA: WK_BUKRS LIKE T001-BUKRS,
          WK_BUTXT LIKE T001-BUTXT,
          WK_TABLE(4) TYPE C VALUE 'T001'.
    START-OF-SELECTION.
      WK_BUKRS = 'DECN'.
      PERFORM GET_BUTXT USING WK_BUKRS WK_TABLE
                     CHANGING WK_BUTXT.
      WRITE: WK_BUTXT.
      FORM GET_BUTXT USING I_BUKRS LIKE T001-BUKRS
                           I_TABLE
                    CHANGING O_BUTXT LIKE T001-BUTXT.
        SELECT SINGLE BUTXT
                 INTO O_BUTXT
                 FROM (I_TABLE) AS G INNER JOIN BKPF AS H
                   ON GBUKRS = HBUKRS
                WHERE G~BUKRS = I_BUKRS.
      ENDFORM.          
    =================================
    Syntax error:
    Wrong expression "INNER" in FROM clause. WHERE condition.
    Can anybody help me to solve the problem.
    My requirement is to use INNER JOIN with variable table

    hi slam,
    chk this sample code.
    hi,
    table emp
    empno name
    a sasi
    b xxx
    c yyy
    table sal
    empno salary
    a 1000
    b 2000
    Inner join
    select eempno ename
    s~sal
    into table int_table
    from emp as e
    inner join sal as s
    on
    eempno = sempno.
    if you made inner join between table a and b by emp no
    the selection retrives only if the condition satisfy the output will be
    a sasi 1000
    b xxx 2000
    rgds
    anver
    if hlped mark points
    Message was edited by: Anversha s

  • Inner join with if statement , how to combine these code ?

    Hello everyone, I have 2 group of code here. This 2 group of code is working without error. But I face a problem , I need to group up this 2 group of code. Please take a look, thank you This is the first code i create.
    SELECT r.Name , r.Restaurant_ID, f.feature, r.Price_Range, r.Cuisine_ID, c.Cuisine,
        s.State_ID, s.State, l.Location_ID, l.Area, l.State_ID, r.Name, r.Location_ID
    FROM Restaurants r, Bridge1_Restaurant_Features b, Features f, Cuisine c, State s, Location l
            where 0=0
            AND b.Feature_ID = f.Feature_ID
            AND b.Restaurant_ID = r.Restaurant_ID
            AND r.Cuisine_ID = c.Cuisine_ID
            AND r.Location_ID = l.Location_ID
            AND l.State_ID = s.State_ID
            <cfif ARGUMENTS.Feature_ID IS NOT "">
            AND b.Feature_ID IN (#ARGUMENTS.Feature_ID#)
            </cfif>
            <cfif ARGUMENTS.Price_Range IS NOT "">
            AND r.Price_Range IN (#ARGUMENTS.Price_Range#)
            </cfif>
            <cfif ARGUMENTS.Cuisine IS NOT "">
            AND r.Cuisine_ID = (#ARGUMENTS.Cuisine#)
            </cfif>
            <cfif val(ARGUMENTS.LocationID2) IS #val(ARGUMENTS.StateID)#>
            AND l.State_ID = #val(ARGUMENTS.LocationID2)#
            <cfelse>
                AND l.Location_ID = #val(ARGUMENTS.LocationID2)#
            </cfif>
    Then, I notice the feature_ID i need to use another logic to show the result. The code is like this
    SELECT r.Restaurant_ID, r.Name, f.Feature
    FROM   Restaurants r
    INNER JOIN Bridge1_Restaurant_Features b ON b.Restaurant_ID = r.Restaurant_ID
    INNER JOIN Features f ON b.Feature_ID = f.Feature_ID
    INNER JOIN
         SELECT Restaurant_ID, COUNT(Feature_ID) AS FeatureCount
         FROM   Bridge1_Restaurant_Features
         <!--- find matching features --->
         WHERE  Feature_ID IN ( <cfqueryparam value="#ARGUMENTS.Feature_ID#" cfsqltype="cf_sql_integer" list="true"> )
        GROUP BY Restaurant_ID
        <!--- having ALL of the requested features --->
         HAVING COUNT(Feature_ID) = <cfqueryparam value="#listLen(ARGUMENTS.Feature_ID)#" cfsqltype="cf_sql_integer">
    ) ck ON ck.Restaurant_ID = r.Restaurant_Id
    I want combine this 2 group together. The second group have to replace
    <cfif ARGUMENTS.Feature_ID IS NOT "">
        AND f.Feature_ID IN (#ARGUMENTS.Feature_ID#)
    </cfif>
    I try few way to group this 2 code, but fail to make it. The code i try is on below, it get error.
    SELECT r.Name , r.Restaurant_ID, f.feature, r.Price_Range, r.Cuisine_ID, c.Cuisine,
            s.State_ID, s.State, l.Location_ID, l.Area, l.State_ID,  r.Location_ID
            FROM Restaurants r, Features f, Cuisine c, State s, Location l
            INNER JOIN Bridge1_Restaurant_Features b ON b.Restaurant_ID = r.Restaurant_ID
            INNER JOIN Features f ON b.Feature_ID = f.Feature_ID
            AND r.Cuisine_ID = c.Cuisine_ID
            AND r.Location_ID = l.Location_ID
            AND l.State_ID = s.State_ID
            <cfif ARGUMENTS.Feature_ID IS NOT "">
            INNER JOIN
                 SELECT Restaurant_ID, COUNT(Feature_ID) AS FeatureCount
                 FROM   Bridge1_Restaurant_Features
                 <!--- find matching features --->
                 WHERE  Feature_ID IN ( <cfqueryparam value="#ARGUMENTS.Feature_ID#" cfsqltype="cf_sql_integer" list="true"> )
                GROUP BY Restaurant_ID
                <!--- having ALL of the requested features --->
                 HAVING COUNT(Feature_ID) = <cfqueryparam value="#listLen(ARGUMENTS.Feature_ID)#" cfsqltype="cf_sql_integer">
            ) ck ON ck.Restaurant_ID = r.Restaurant_Id
            </cfif>
            <cfif ARGUMENTS.Price_Range IS NOT "">
            AND r.Price_Range IN (#ARGUMENTS.Price_Range#)
            </cfif>
            <cfif ARGUMENTS.Cuisine IS NOT "">
            AND r.Cuisine_ID = (#ARGUMENTS.Cuisine#)
            </cfif>
            <cfif val(ARGUMENTS.LocationID2) IS #val(ARGUMENTS.StateID)#>
            AND l.State_ID = #val(ARGUMENTS.LocationID2)#
            <cfelse>
                AND l.Location_ID = #val(ARGUMENTS.LocationID2)#
            </cfif>

    Yeah Adam I am listen to you ! But Finally i solve it.
    This is the code i solve .
    SELECT r.Restaurant_ID, r.Name, f.Feature, r.Price_Range, r.Cuisine_ID, c.Cuisine,
                        l.Location_ID, l.Area, s.State
              FROM   Restaurants r
              <cfif ARGUMENTS.Feature_ID IS NOT "">
              INNER JOIN
                   SELECT Restaurant_ID, COUNT(Feature_ID) AS FeatureCount
                   FROM   Bridge1_Restaurant_Features
                   <!--- find matching features --->
                   WHERE  Feature_ID IN ( <cfqueryparam value="#ARGUMENTS.Feature_ID#" cfsqltype="cf_sql_integer" list="true"> )
                  GROUP BY Restaurant_ID
                  <!--- having ALL of the requested features --->
                   HAVING COUNT(Feature_ID) = <cfqueryparam value="#listLen(ARGUMENTS.Feature_ID)#" cfsqltype="cf_sql_integer">
              ) ck ON ck.Restaurant_ID = r.Restaurant_Id
              </cfif>
              INNER JOIN Location l ON r.Location_ID = l.Location_ID
              INNER JOIN State s ON l.State_ID = s.State_ID
              INNER JOIN Cuisine c ON r.Cuisine_ID = c.Cuisine_ID
              INNER JOIN Bridge1_Restaurant_Features b ON b.Restaurant_ID = r.Restaurant_ID
              INNER JOIN Features f ON b.Feature_ID = f.Feature_ID
              <cfif ARGUMENTS.Cuisine IS NOT "">
              AND r.Cuisine_ID = (#ARGUMENTS.Cuisine#)
              </cfif>
              <cfif ARGUMENTS.Price_Range IS NOT "">
              AND r.Price_Range IN (#ARGUMENTS.Price_Range#)
              </cfif>
              <cfif val(ARGUMENTS.LocationID2) IS #val(ARGUMENTS.StateID)#>
              AND l.State_ID = #val(ARGUMENTS.LocationID2)#
              <cfelse>
                   AND l.Location_ID = #val(ARGUMENTS.LocationID2)#
              </cfif>
    Is it the code can be improve more ?

  • Inner join in ABAP and exporting the data

    Hi,
    I want to use inner join in ABAP for retrieving data from 2 tables and export that data.How will i do it?

    Hi,
    Refer following code
    *--Select query to pick the data from table VBRK
    VBRP and VBPA using inner join
      SELECT a~vbeln                            "Billing document
             a~fkdat                            "Billing date
             a~fktyp                            "Billing category
             a~fkart                            "Billing type
             a~vtweg                            "Distribution channel
             a~knumv                            "Number of doc condition
             b~posnr                            "Billing item
             b~matnr                            "Material Number
             b~werks                            "Plant
             b~vgbel                            "Referance
             b~netwr                            "Net value of billing item
             b~wavwr                            "Cost in document currency
             c~kunnr                            "Partner function
             FROM vbrk AS a
             INNER JOIN vbrp AS b
             ON avbeln EQ bvbeln
             INNER JOIN vbpa AS c
             ON  bvbeln EQ cvbeln
            AND bposnr EQ cposnr
             INTO CORRESPONDING FIELDS OF TABLE it_vbrp
             WHERE fkdat IN s_erdat
             AND   parvw EQ 'ZS'.
      IF sy-subrc EQ 0.
    *--Sort table by
        SORT it_vbrp BY vgbel1.
      ENDIF.
    Regards,
    Prashant

  • Nester inner joins

    SELECT order_head.ID col
    FROM
    order order_head
    INNER JOIN customer customer_2 ON order_head.customerID = customer_2.ID
    WHERE
    order_head.orderDate BETWEEN '12/12/2001'
    AND '12/12/2009'
    AND order_head.ID <> 0
    AND customer_2.LastName = 'smith'
    AND order_head.status = 1
    INNER JOIN (customer_2 INNER JOIN order_head ON customer_2.ID = order_head.customerID) ON cust_address.ID = customer_2.AddressID )
    WHERE cust_address.postcode = 'NW1 9PQ'
    Error : Incorrect syntax near the keyword 'INNER'.
    The last bit in bold does not work the other stuff works correctly, what I am trying to do that does not work is find orders from customers with last name of smith and post code of 'NW1 9PQ', it is unfortunate that the tables are from a third party and cannot be changed and so I am stuck with the table structure. I am also trying to stick with joins.
    I have tested this inner join in isolation and appears to work fine !!!!
    Any suggestions or pointers will be most appriciated, thanks in advance....
    BM

    [email protected] wrote:
    Error : Incorrect syntax near the keyword 'INNER'.
    The last bit in bold does not work the other stuff works correctly, what I am trying to do that does not work is find orders from customers with last name of smith and post code of 'NW1 9PQ', it is unfortunate that the tables are from a third party and cannot be changed and so I am stuck with the table structure. I am also trying to stick with joins.
    I have tested this inner join in isolation and appears to work fine !!!!
    Any suggestions or pointers will be most appriciated, thanks in advance....BM,
    probably you just need follow the correct syntax described in the previous post and put first the join operators. If you really think you have the need to use nested query, you could use an approach like that:
    SELECT order_head.ID col
    FROM order order_head
    INNER JOIN customer customer_2 ON order_head.customerID = customer_2.ID
    INNER JOIN (
    select c2.* from customer c2 INNER JOIN order o2 ON c2.ID = o2.customerID
    ) cust_address ON cust_address.ID = customer_2.AddressID
    WHERE order_head.orderDate BETWEEN '12/12/2001'
    AND '12/12/2009'
    AND order_head.ID <> 0
    AND customer_2.LastName = 'smith'
    AND order_head.status = 1
    AND cust_address.postcode = 'NW1 9PQ'Probably above statement doesn't make sense content-wise but I just wanted to use it for illustrative purposes that you can use nested subqueries like that.
    Regards,
    Randolf
    Oracle related stuff blog:
    http://oracle-randolf.blogspot.com/
    SQLTools++ for Oracle (Open source Oracle GUI for Windows):
    http://www.sqltools-plusplus.org:7676/
    http://sourceforge.net/projects/sqlt-pp/

  • Re field order in INNER JOIN

    Hi,
    I need a suggestion for improvement of my inner JOIN statement.
    internal table itab is of below type
    type: begin of ty_itab
            field a
            field b
            field c
            field d
            end of ty_itab
    I have to join two database table TABLE1 and TABLE2. Fields a and d are from TABLE2 and fields b and c are from TABLE 1.
    But in the internal table I must have the fields in the order fielda,b,c,d.
    Which of the below two options will be better for performance ? And why ?
    option 1:
    SELECT tab2~a
                  tab1~b
                  tab1~c
                  tab2~d into corresponding fields of itab from TABLE1 as tab1 and TABLE2 as tab2 on..........
    option2:
    SELECT tab1~b
                  tab1~c
                  tab2~a 
                  tab2~d into corresponding fields of itab from TABLE1 as tab1 and TABLE2 as tab2 on..........
    Thanks in advance...
    Best Regards
    Ananya Mukherjee
    Moderator message - Cross post locked
    Edited by: Rob Burbank on May 28, 2009 9:43 AM

    I am not satisfied with your two options. Because of following reason,
    1. First I want to know which one is master table and item table. Because in inner join select query you want to use master table as first and you want to use the item table as second.
    Example: 
    SELECT TAB2A  TAB1B TAB1C TAB2D
          INTO TABLE ITAB
        FROM <MASTER TABLE1> AS TAB1 INNER JOIN <ITEM TABLE2> AS TAB2 ON..........
    2. You should not use the INTO CORRESPONDING FIELDS OF ITAB. Instead of this you can change the field order in internal table or in select query.
    Example:
    Use following way
    TYPE: BEGIN OF TY_ITAB
                    FIELD A
                    FIELD B
                    FIELD C
                    FIELD D
               END OF TY_ITAB
    SELECT TAB2A  TAB1B TAB1C TAB2D
         INTO TABLE ITAB
       FROM <MASTER TABLE1> AS TAB1 INNER JOIN <ITEM TABLE2> AS TAB2 ON..........
    Or , Anyway you are going to use in program only. It will not affect your logic if change the field order in the internal table.
    TYPE: BEGIN OF TY_ITAB
                    FIELD B
                    FIELD C
                    FIELD A
                    FIELD D
              END OF TY_ITAB
    SELECT TAB1B  TAB1C  TAB2A  TAB2D
         INTO TABLE ITAB
       FROM <MASTER TABLE1> AS TAB1 INNER JOIN <ITEM TABLE2> AS TAB2 ON..........
    3. In inner join query, the order of Where condition also create the perform issue. Because in where condition maximum you try to use all the primary key from <MASTER TABLE1>  and <ITEM TABLE2> and field order in where condition should be same order in <MASTER TABLE1>  and  <ITEM TABLE>.
    This are the things you want to take care when you try to use INNER JOIN Query. If you have any clarification please let me know. I will try answer your Questions.
    Ravi.
    Edited by: RavivarmanP on May 28, 2009 6:55 PM

  • Alternative to INNER JOIN

    I've a table like this:
    CREATE TABLE ADRESY
    IDPOWIATU NUMBER(10) NOT NULL,
    IDOBIEKTU NUMBER(10) NOT NULL,
    IDADRESU NUMBER(10) NOT NULL,
    TYPADRESU NUMBER(10) NOT NULL,
    NAZWAWLASNA VARCHAR2(255 BYTE),
    IDKRAJU NUMBER(10),
    IDWOJEWS NUMBER(10),
    IDPOWIATUS NUMBER(10),
    IDGMINYS NUMBER(10),
    IDMIEJSC NUMBER(10),
    IDULICY NUMBER(10),
    NRDOMU VARCHAR2(50 BYTE),
    NRLOKALU VARCHAR2(50 BYTE),
    KODPOCZTOWY VARCHAR2(50 BYTE),
    POCZTA VARCHAR2(50 BYTE),
    STATUS NUMBER(3) NOT NULL,
    DATAUTW DATE NOT NULL,
    DATAWER DATE NOT NULL,
    DATAARCH DATE
    and need to query all the duplicate rows that have a common subset of columns:
    SELECT
    ADRA.IdObiektu,
    ADRA.Status,
    ADRB.IdObiektu AS ZnalezioneId
    FROM
    ADRESY ADRA
    INNER JOIN
    ADRESY ADRB
    ON
    (ADRA.IDPOWIATU = ADRB.IDPOWIATU) AND
    (ADRA.TYPADRESU = ADRB.TYPADRESU) AND
    (ADRA.NAZWAWLASNA = ADRB.NAZWAWLASNA OR (ADRA.NAZWAWLASNA IS NULL AND ADRB.NAZWAWLASNA IS NULL)) AND
    (ADRA.IDKRAJU = ADRB.IDKRAJU OR (ADRA.IDKRAJU IS NULL AND ADRB.IDKRAJU IS NULL)) AND
    (ADRA.IDWOJEWS = ADRB.IDWOJEWS OR (ADRA.IDWOJEWS IS NULL AND ADRB.IDWOJEWS IS NULL)) AND
    (ADRA.IDPOWIATUS = ADRB.IDPOWIATUS OR (ADRA.IDPOWIATUS IS NULL AND ADRB.IDPOWIATUS IS NULL)) AND
    (ADRA.IDGMINYS = ADRB.IDGMINYS OR (ADRA.IDGMINYS IS NULL AND ADRB.IDGMINYS IS NULL)) AND
    (ADRA.IDMIEJSC = ADRB.IDMIEJSC OR (ADRA.IDMIEJSC IS NULL AND ADRB.IDMIEJSC IS NULL)) AND
    (ADRA.IDULICY = ADRB.IDULICY OR (ADRA.IDULICY IS NULL AND ADRB.IDULICY IS NULL)) AND
    (ADRA.NRDOMU = ADRB.NRDOMU OR (ADRA.NRDOMU IS NULL AND ADRB.NRDOMU IS NULL)) AND
    (ADRA.NRLOKALU = ADRB.NRLOKALU OR (ADRA.NRLOKALU IS NULL AND ADRB.NRLOKALU IS NULL)) AND
    (ADRA.KODPOCZTOWY = ADRB.KODPOCZTOWY OR (ADRA.KODPOCZTOWY IS NULL AND ADRB.KODPOCZTOWY IS NULL)) AND
    (ADRA.POCZTA = ADRB.POCZTA OR (ADRA.POCZTA IS NULL AND ADRB.POCZTA IS NULL)) AND
    ADRA.IdAdresu <> ADRB.IdAdresu
    WHERE
    ADRA.IdObiektu IN (SELECT IdObiektu FROM ObiektyTrans) AND
    ADRB.STATUS = 0 AND
    ADRA.STATUS IN (2,5);
    For 64000 matching rows (SELECT IdObiektu FROM ObiektyTrans) and 64428 rows in ADRESY it takes about few minutes to
    complete. I try to use GROUP BY or analytical functions
    to write faster query but so far didn't succeeded.
    Any ideas ?
    Thank You

    I think I've found the solution for my problem, quite a big one :)
    SELECT
      adra.idobiektu,
      adra.status,
      adrb.idobiektu AS znalezioneid
    FROM
      SELECT
        idobiektu,
        status,
        klasa
      FROM
    SELECT idobiektu, status, klasa
        FROM (SELECT idobiektu, status,
                     MIN (idadresu) OVER (PARTITION BY idpowiatu, typadresu, nazwawlasna, idkraju, idwojews, idpowiatus, idgminys, idmiejsc, idulicy, nrdomu, nrlokalu, kodpocztowy, poczta)
                                                                            klasa,
                     SUM (DECODE (status, 2, 1, 5, 1, 0)) OVER (PARTITION BY idpowiatu, typadresu, nazwawlasna, idkraju, idwojews, idpowiatus, idgminys, idmiejsc, idulicy, nrdomu, nrlokalu, kodpocztowy, poczta)
                                                                         status25,
                     SUM (DECODE (status, 0, 1, 0)) OVER (PARTITION BY idpowiatu, typadresu, nazwawlasna, idkraju, idwojews, idpowiatus, idgminys, idmiejsc, idulicy, nrdomu, nrlokalu, kodpocztowy, poczta)
                                                                          status0
                FROM adresy)
       WHERE status0 <> 0 AND status25 <> 0   
      WHERE
        status IN (2, 5) AND
        idobiektu IN (SELECT idobiektu FROM obiektytrans@lnkmatraakt)
    ) adra
    INNER JOIN
      SELECT
        idobiektu,
        klasa
      FROM
    SELECT idobiektu, status, klasa
        FROM (SELECT idobiektu, status,
                     MIN (idadresu) OVER (PARTITION BY idpowiatu, typadresu, nazwawlasna, idkraju, idwojews, idpowiatus, idgminys, idmiejsc, idulicy, nrdomu, nrlokalu, kodpocztowy, poczta)
                                                                            klasa,
                     SUM (DECODE (status, 2, 1, 5, 1, 0)) OVER (PARTITION BY idpowiatu, typadresu, nazwawlasna, idkraju, idwojews, idpowiatus, idgminys, idmiejsc, idulicy, nrdomu, nrlokalu, kodpocztowy, poczta)
                                                                         status25,
                     SUM (DECODE (status, 0, 1, 0)) OVER (PARTITION BY idpowiatu, typadresu, nazwawlasna, idkraju, idwojews, idpowiatus, idgminys, idmiejsc, idulicy, nrdomu, nrlokalu, kodpocztowy, poczta)
                                                                          status0
                FROM adresy)
       WHERE status0 <> 0 AND status25 <> 0   
      WHERE
        status = 0
    ) adrb
    USING (klasa);
    I've also tried sthg like this, but it takes to long time to execute this statement:
    SELECT
      adra.idobiektu,
      adra.status,
      adrb.idobiektu AS znalezioneid
    FROM
      SELECT
      FROM
        adresy
      WHERE
        status IN (2, 5) AND
        idobiektu IN (SELECT idobiektu from obiektytrans@lnkmatraakt)
    ) adra
    INNER JOIN
      SELECT
      FROM
        adresy
      WHERE
        status = 0
    ) adrb
    on
        (ADRA.IDPOWIATU = ADRB.IDPOWIATU) AND
        (ADRA.TYPADRESU = ADRB.TYPADRESU) AND
        (ADRA.NAZWAWLASNA = ADRB.NAZWAWLASNA OR (ADRA.NAZWAWLASNA IS NULL AND ADRB.NAZWAWLASNA IS NULL)) AND
        (ADRA.IDKRAJU = ADRB.IDKRAJU OR (ADRA.IDKRAJU IS NULL AND ADRB.IDKRAJU IS NULL)) AND
        (ADRA.IDWOJEWS = ADRB.IDWOJEWS OR (ADRA.IDWOJEWS IS NULL AND ADRB.IDWOJEWS IS NULL)) AND
        (ADRA.IDPOWIATUS = ADRB.IDPOWIATUS OR (ADRA.IDPOWIATUS IS NULL AND ADRB.IDPOWIATUS IS NULL)) AND
        (ADRA.IDGMINYS = ADRB.IDGMINYS OR (ADRA.IDGMINYS IS NULL AND ADRB.IDGMINYS IS NULL)) AND
        (ADRA.IDMIEJSC = ADRB.IDMIEJSC OR (ADRA.IDMIEJSC IS NULL AND ADRB.IDMIEJSC IS NULL)) AND
        (ADRA.IDULICY = ADRB.IDULICY OR (ADRA.IDULICY IS NULL AND ADRB.IDULICY IS NULL)) AND
        (ADRA.NRDOMU = ADRB.NRDOMU OR (ADRA.NRDOMU IS NULL AND ADRB.NRDOMU IS NULL)) AND
        (ADRA.NRLOKALU = ADRB.NRLOKALU OR (ADRA.NRLOKALU IS NULL AND ADRB.NRLOKALU IS NULL)) AND
        (ADRA.KODPOCZTOWY = ADRB.KODPOCZTOWY OR (ADRA.KODPOCZTOWY IS NULL AND ADRB.KODPOCZTOWY IS NULL)) AND
        (ADRA.POCZTA = ADRB.POCZTA OR (ADRA.POCZTA IS NULL AND ADRB.POCZTA IS NULL));

  • Can I di Inner Join on 2 tables by using only 1 key field?

    Hello,
    I hv 2 Std. SAP tables, KNVV, KNKK.
    - KNVV - does hv 2 key fields (KUNNR and some other, say X)
    - KNKK - does hv 2 key fields (KUNNR and some other,say Y)
    I want to build a inner join on these 2 tables.
    So, Can I do a INNER JOIN on these 2 tables by using ONLY one key field (its KUNNR)? bcoz, there is ONLY common field!!
    thank you

    Hi,
    The correctness depends on your bussiness requirement. If the common key field is not a primary key, you may end up fetching duplicate entries which you may have to delete after the select query. You dont have to take of any extra precautions in a inner join select query compared to a ordinary select query. Just make sure you use most of the primary key fields of both the tables  in the where clause of the select query for better performance
    Vikranth

  • Inner Join Subquery data display

    I have a query that uses a sub query on a INNER JOIN 
    SELECT  SUM(A.qtyord), A.Item, A.Description, A.Class, B.Forecast, 
                    CASE 
                            WHEN B.Forcast = 0
                    THEN A.Item 
                    ELSE 
                            CASE 
                            WHEN (SUM(A.ORDQ)/B.Forcase) > 0.20  
                            THEN A.Item 
                            ELSE '' 
                        END  
                    END AS SKU
                FROM        tableA A
            INNER JOIN ((SELECT Item, ( SUM(BBAL) + SUM(RECV) + SUM(ADJU) - SUM(ALOC)) as onhand FROM tableD GROUP BY Item)) D ON A.Item = D.Item
            INNER JOIN tableB B ON A.Item=B.Item
            INNER JOIN tableC C ON B.Item=C.Item
        GROUP BY A.Item, A.Description, A.Class, B.Forecast
        ORDER BY A.Class DESC
    How do I get the data from the sub query to display.

    Hello,
    Please check whether the following statement meet your requirment:
    ;With cte as
    SELECT Item, ( SUM(BBAL) + SUM(RECV) + SUM(ADJU) - SUM(ALOC)) as onhand
    FROM tableD GROUP BY Item)
    SELECT SUM(A.qtyord), A.Item, A.Description, A.Class, B.Forecast, cte.onhand,
    CASE
    WHEN B.Forcast = 0
    THEN A.Item
    ELSE
    CASE
    WHEN (SUM(A.ORDQ)/B.Forcase) > 0.20
    THEN A.Item
    ELSE ''
    END
    END AS SKU
    FROM tableA A
    INNER JOIN cte ON A.Item = cte.Item
    INNER JOIN tableB B ON A.Item=B.Item
    INNER JOIN tableC C ON B.Item=C.Item
    GROUP BY A.Item, A.Description, A.Class, B.Forecast
    ORDER BY A.Class DESC
    Regards,
    Fanny Liu
    If you have any feedback on our support, please click here.
    Fanny Liu
    TechNet Community Support

  • Inner Join within results?

    So if an item is a bottle, it will have a unique BottleKey and Null for CaseKey and if
    an item is a Case, it will have a unique CaseKey and Null for BottleKey. However, the PrimaryIDs are the same for both the bottle and case. I get the results to look like this:
    Bottlekey CaseKey PrimaryID
    4754 NULL ABC-234
    NULL 5465 ABC-234
    Is there a way to get the result sorted by Primary ID? 
    So that the rows are halved from what we have in the table above and the results look like
    PrimaryID CaseKey BottleKey
    ABC-234 5465 4754 
    Thanks

    select distinct a.PrimaryId,c.CaseKey,b.BottelKey from test a
    inner join
    (select distinct PrimaryId, BottelKey from test where BottelKey is not null) b
    on a.PrimaryId=b.PrimaryId
    inner join
    (select distinct PrimaryId,CaseKey from test where CaseKey is not null) c
    on a.PrimaryId=c.PrimaryId
    Replace test table with your orignal table

  • Select SUM( ) in inner join - error in code

    Hi All,
    The following code is using ABAP OO and I get the following error
    E:The addition "FOR ALL ENTRIES" excludes all aggregate functions with
    the exception of "COUNT( * )", as the single element of the SELECT
    clause.
    Here is the Code:
    select a/bic/ZEDM_ENTP amaterial a~/bic/zedm_stid
    a/bic/ZEDM_SDT a/bic/ZEDM_MINH a~/bic/ZEDM_EHD
    SUM( b~/bic/ZEDM_QPLN )
    c~/bic/ZDEMQTY
    from /bic/azdrp_1900 as a inner join /bic/azdrp_0400 as b
    on
    a/bic/ZEDM_ENTP = b/bic/ZEDM_ENTP and
    amaterial = bmaterial and
    a/bic/zedm_stid = bplant and
    a/bic/ZEDM_SDT = b/bic/zedm_pshp
    inner join /bic/azrp_0800 as C
    on
    amaterial = cmaterial and
    a/bic/zedm_stid = c/bic/zedm_stid and
    a/bic/ZEDM_SDT = c/bic/ZEDM_ESDT
    into table z_it_inv for all entries in
    RESULT_PACKAGE where
    a~/bic/ZEDM_ENTP = RESULT_PACKAGE-/bic/ZEDM_ENTP
    and a~material = RESULT_PACKAGE-material
    and a~/bic/ZEDM_SDT = RESULT_PACKAGE-/BIC/OIZEDM_EDT
    and a~/bic/zedm_stid = RESULT_PACKAGE-/BIC/OIZEDM_TOST
    group by b/bic/ZEDM_ENTP bmaterial b~plant
    b~/bic/zedm_pshp
    Can you please tell me how to correct this?
    Thanks

    Hi,
    You can get all records first and then LOOP AT Internal table and use COLLECT statement to sum up required column.
    This will be a better idea than trying to use SUM in SELECT with JOINS.
    ashish

Maybe you are looking for

  • Batch secure multiple pdf files

    Hi everyone, I have around 100 separate 1page pdf files. Each of these files needs to have security applied that ensures that no changes are made, low-res printing is allowed and a password needs to be applied to prevent the security settings being c

  • Cycling--Searching, No Service, Signal!!!! Maybe a fix!

    I think I figured out this problem. At random, my iPhone4 hits a snag. It will say AT&T 3G > Searching > No Service > 3G > AT&T and so on. This has happened to me at least 4 times already in the last 3 months. For several hours, it will do this and I

  • ERIC CHAN:  weirdness in ACR v 8.7.1

    Hi Eric, There's something weird in ACR 8.7.1. It's immediately putting the Adjustment icon (tiny circle with two slider symbols) visible in Bridge upon opening a raw image without having applied any adjustments whatsoever in ACR .  This is not koshe

  • Safari 5.03 crashes every time

    Hi people, I've googled the problems with Safari 5.03 and have seen people getting similar problems when launching Safari. I also have them. I tried a number of options like deleting the cache of Safari, but the problem still persists. I'm thinking o

  • OSX Yosemite Guest Account Internet is not working.

    The Admin has internet access but the Guest Account doesn't. Can anyone help? Thanks.