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 ?

Similar Messages

  • Coupling  INNER  JOIN   with   FOR  ALL  ENTRIES  statement

    Hi  All,
         I   am  coupling   INNER  JOIN  with  FOR  ALL  ENTRIES  statement  .....
         Would   you  please  highlight  its  implications ??   Is  it  a  best  practise ? 
         Is  it  advicable  to  use  MULTIPLE   INNER  JOINs  with  a  FOR  ALL  ENTRIES ???
        SORT itab BY matnr.
        IF NOT itab[] IS INITIAL.
          SELECT       epmatnr epebeln ep~ebelp
                       epwerks epmenge ep~netpr
                       ekps_psp_pnr ebbelnr eb~menge
                       INTO TABLE iekpo
                       FROM ekpo AS ep
                       INNER JOIN ekkn AS ek
                       ON  ekebeln = epebeln
                       AND ekebelp = epebelp
                       INNER JOIN ekbe AS eb
                       ON     ebebeln = epebeln
                       AND    ebebelp = epebelp
                       AND    eb~bwart = '101'
                       FOR ALL  ENTRIES IN itab
                       WHERE   ep~matnr = itab-matnr.
          IF sy-subrc EQ 0.
            SORT iekpo BY matnr werks.
            LOOP  AT  itab ASSIGNING  <itab>.
              READ TABLE iekpo WITH KEY matnr = <itab>-matnr
                                        werks = <itab>-werks
                                        BINARY SEARCH.
              IF sy-subrc EQ 0.
                MOVE iekpo-matnr      TO   itab1-matnr.
                MOVE iekpo-ebeln      TO   itab1-ebeln.
                MOVE iekpo-ebelp      TO   itab1-ebelp.
                MOVE iekpo-netpr      TO   itab1-poprice.
                MOVE iekpo-werks      TO   itab1-werks.
                MOVE iekpo-menge      TO   itab1-menge1.
                MOVE iekpo-menge1     TO   itab1-menge2.
                MOVE iekpo-belnr      TO   itab1-belnr.
                MOVE iekpo-ps_psp_pnr TO   itab1-pspel.
                MOVE <itab>-pspel     TO   itab1-tpspel.
                MOVE <itab>-sobkz     TO   itab1-sobkz.
                MOVE <itab>-fo_qty    TO   itab1-fo_qty.
                MOVE <itab>-schgt     TO   itab1-schgt.
                MOVE <itab>-postp     TO   itab1-postp.
                MOVE <itab>-beskz     TO   itab1-beskz.
                pend_qty = iekpo-menge1 - iekpo-menge2.
                MOVE pend_qty         TO   itab1-pending.
                APPEND itab1.
                pend_qty = 0.
              ENDIF.
            ENDLOOP.
          ENDIF.
        ENDIF.
      ENDIF.
    Regards
    Jaman
    Edited by: ABAP Techie on Sep 15, 2008 12:39 PM
    Edited by: ABAP Techie on Sep 15, 2008 12:41 PM

    best practise ... don't know ... it is allowed and o.k.
    If possible you should of coourse to have no FOR ALL ENTRIES at all !
    Joins, there is no general rule, check indexes etc.
    The first SORT, I don't that it help for anything, use it together with the delete adjacent duplicates if you expect duplicates in the driver table.
    o.k., it can help, if there is a loop afterwards and an append inside, because the new table itab1 is then sorted.
    Siegfried

  • Replacing a inner join with for all entries

    Hi Team,
       In a already developed program I am replacing a inner join with select query follow up with for-all-entris and passing the data to final internal table but in both the case the result should be same then only my replacement will be correct. But my no records in both cases differs. This happening because when i am selecting data from first data base table is 32 lines. then I am doing fo-all-entries moving all the duplicate entries then the no records are four. but in final internal table i am looping the first internal table. So in final internal table the no of records are 32. But in inner join query the records are 16.So please let me know how resolve this issue?
    Thanks and REgards
    Deepa

    Hi Thomas,
      Thanks for ur suggestion.
    The solved that in below.
    In select query I did not change anything The way I had written the code was correct.
    I think many of us know how to write that how to make the performance better in that way.
    I made the change when I transfered the to final internal table.
    The original Inner join code:
    select a~field1 a~field2 a~field3 b~field2 b~field3 b~field4
               from dbtab1 as a  inner join dbtab2 as b
              on a~field1 = b~field1 into it_final where
              a~field1 in s_field1. [Field1  in both the table are key field]
    Before code:
    Sort itab1 by key-fields.
    sort itab2 by keyfields.
    loop at itab1 into wa1.
    move: wa1-field1 to wa_final-field1,
               wa1-field2 to wa_final-field2,
               wa1-field3 to wa_final-field3.
    read table itab2 into wa2 witk key field1 = wa1-field1 binary search.
      if sy-subrc = 0.
      move : wa2-field2 to wa_final-field4,
                 wa2-field3 to wa_final-field5,
                 wa2-field4 to wa_final-field6.
    append wa_final to it_final.
    endif.
    Clear : wa1, wa2, wa_final.
    endloop.
    In this case if the one key fieild value is not present there in second internal table but its there in first internal table still it will read that row with 2nd internal values having zeroes. Normally what does not happen in inner join case if the key field value will same in both the case ,then that will fetch only those rows.
    Changed Code
    loop at itab1 into wa1.
    read table itab2 into wa2 witk key field1 = wa1-field1 binary search.
      if sy-subrc = 0.
    move: wa1-field1 to wa_final-field1,
               wa1-field2 to wa_final-field2,
               wa1-field3 to wa_final-field3.
      move : wa2-field2 to wa_final-field4,
                 wa2-field3 to wa_final-field5,
                 wa2-field4 to wa_final-field6.
    append wa_final to it_final.
    endif.
    Clear : wa1, wa2, wa_final.
    endloop.
    In this case the values will read to final internal table if both key field matches.
    With Regards
    Deepa

  • INNER JOIN with FOR ALL ENTRIES IN Performance ?

    I am using following the following <b>Select using Inner join with For All Entries in.</b>
          SELECT kebeln kebelp kvbeln kvbelp
            FROM ekkn AS k INNER JOIN ekbe AS b ON kebeln = bebeln
                                               AND kebelp = bebelp
            INTO TABLE gi_purchase
             FOR ALL ENTRIES
             IN gi_sales
          WHERE k~mandt EQ sy-mandt
            AND k~vbeln EQ gi_sales-vbeln
            AND k~vbelp EQ gi_sales-posnr
            AND b~budat EQ p_date.
    If i am not doing inner join then I will have to do 2 select with for all entries in on ekkn and ekbe tables and then compare them.
    <b>I want to know which one has better performance
    Inner join with for all entries in
                    or
    2 Selects with for all entries in</b>

    the join is almost aways faster:
    <a href="/people/rob.burbank/blog/2007/03/19/joins-vs-for-all-entries--which-performs-better">JOINS vs. FOR ALL ENTRIES - Which Performs Better?</a>
    <a href="http://blogs.ittoolbox.com/sap/db2/archives/for-all-entries-vs-db2-join-8912">FOR ALL ENTRIES vs DB2 JOIN</a>
    Rob

  • Inner Join with For All Entries - Performance ?

    I am using following the following <b>Select using Inner join with For All Entries in.</b>
          SELECT kebeln kebelp kvbeln kvbelp
            FROM ekkn AS k INNER JOIN ekbe AS b ON kebeln = bebeln
                                               AND kebelp = bebelp
            INTO TABLE gi_purchase
             FOR ALL ENTRIES
             IN gi_sales
          WHERE k~mandt EQ sy-mandt
            AND k~vbeln EQ gi_sales-vbeln
            AND k~vbelp EQ gi_sales-posnr
            AND b~budat EQ p_date.
    If i am not doing inner join then I will have to do 2 select with for all entries in on ekkn and ekbe tables and then compare them.
    <b>I want to know which one has better performance
    Inner join with for all entries in
                    or
    2 Selects with for all entries in</b><b></b>

    An Inner Join with for all entries should be done if you add this....
    IF NOT gi_sales[] IS INITIAL.
    SELECT k~ebeln k~ebelp k~vbeln k~vbelp
    FROM ekkn AS k INNER JOIN ekbe AS b ON k~ebeln = b~ebeln
    AND k~ebelp = b~ebelp
    INTO TABLE gi_purchase
    FOR ALL ENTRIES
    IN gi_sales
    WHERE k~mandt EQ sy-mandt
    AND k~vbeln EQ gi_sales-vbeln
    AND k~vbelp EQ gi_sales-posnr
    AND b~budat EQ p_date.
    ENDIF.
    Also, while you use an index or the complete key for the SELECT, your not going to suffer from lack of performance -;)
    Greetings,
    Blag.

  • Query Question: Combine outer join with max() statement

    Hi Folks,
    I have been banging my head on this for a while now and am asking from some advice.
    I have a Project Completion Report that pulls information from a number of tables:
    1) Program (PK: ID)
    2) Project (PK: ID, FK: Program_ID constrained to Program.ID)
    3) Project_Monthly (PK: ID, FK: Project_ID constrained to Project.ID)
    4) Status_Report (PK: ID, FK: Project_ID constrained to Project.ID)
    The Status_Report table has a Record_Date field and holds a BLOB with the status reports for all the projects. In the best of all possible worlds, every project would have a status report, but, obviously, some have no status reports.
    I have been asked to extend the Project Completion Report. It currently shows:
    1) Program Number (Program.Program_Number)
    2) Program Name (Program.Program_Name)
    3) Project (Project.Project_Name)
    4) Projected Completion Date (Project_Monthly.Current_Date_Production)
    We would like to add the latest status report. So I started with something that works in another report, which is to get the ID from the Status_Report table for the record that has the maximum date value in the Record_date that matches the Project in the current Program. But this other query is looking at one program at a time. The Project Completion Report is showing all programs with projects that complete within a certain time frame.
    The requirement is to show all projects that will complete within a time frame and to display the ID of the most recent status report (I use javascript to turn this into a downloadable link). However, whenever I try something like:
    AND Status_Report.Record_Date =
    (select MAX(Status_Report.record_date) from Status_Report
    where Status_Report.ID = Project.ID)I loose all projects that have no status reports associated with them.
    How can I keep all the projects that fulfill the criteria of completing within a specified time frame and add the ID, if it exists, of the most recent status report?
    I thank you for your time and assistance. Please let me know how I can clarify the problem more clearly.
    Yours,
    Petie

    I had thought I finally solved it. Alas, it wasn't yet to be.
    I created a view with the following select statement:
    select p.program_number, p.program_name,
    pj.id as pj_id, pj.project_name, pjm.in_prod_current, pjm.record_date as pjm_record_date,
    prs.id as prs_ID, prs.record_date as prs_record_date
    from program p, project pj, project_monthly pjm,
    project_status_report prs
    where p.id = pj.program_id
    and pj.id = pjm.project_id
    and pj.id = prs.project_id (+);Then, I selected from the view and the project_status_report table, performed an outer join on those (filtered to only the current month's project records) and filtered by the maximum date for each project.
    Here is the resulting query:
    select pc.program_number, pc.pj_id, pc.project_name, pc.in_prod_current,
    pc.pjm_record_date, pc.prs_id
    from project_completion pc,
    project_status_report prs
    where trunc(pjm_record_date, 'mon') = trunc(sysdate,'mon')
    and prs.id = pc.prs_id (+)
    and pc.prs_record_date =
    (select max(mprs.record_date)
    from project_status_report mprs
    where mprs.project_id = pc.pj_id)However, I am still not getting the projects for which there are no status reports.
    Verflixt und zugenaeht!
    It seems that I want to compare based upon the record_date of the status report, but get the ID back. Can I use DECODE for that? Can I get the MAX(prs.record_date), but return only the prs.ID? Because if I include the ID in the select statement, I get too many matches, but if I don't then I don't have the ID, which is needed.
    Any suggestions?
    Thanks, Petie
    Message was edited by:
    Petie

  • What is syntax for inner join of Select statement with  Secondary Index

    Hi Genies,
                    Here i have created the Secondary index for table COEP and also used for select query but there is no improvement in runtime . taking same time for select query without index as well as with index. my code is
    SELECT *  INTO CORRESPONDING FIELDS OF TABLE ITAB_COEP FROM COEP AS A
                 INNER JOIN COBK  AS B ON ( A~BELNR = B~BELNR AND A~KOKRS = B~KOKRS AND A~GJAHR = B~GJAHR )
                 WHERE A~OBJNR = OBJNR
                 AND A~GJAHR = CURRY
                 AND B~GJAHR = CURRY
                 AND A~RBEST = ''
                 AND A~BEKNZ = 'S'
                 AND B~BELNR NE WA_COBK-BELNR
                 AND B~AWREF_REV = ''
                 AND B~AWORG_REV = ''
                 AND B~KOKRS = WA_COIOB-KOKRS
                 AND B~BLDAT BETWEEN START_DATE AND END_DATE
                 AND B~BLART IN ('SA','KA','KR','AB')  %_HINTS ORACLE 'INDEX("COEP" "COEP~ZBU")'.
    index name is ZBU.
    please any one let me know is there any syntax mistakes? but while checking and during the runtime it does'nt shows any error.
    Regards,
            Thangam.P

    Hi Genies,
                    Here i have created the Secondary index for table COEP and also used for select query but there is no improvement in runtime . taking same time for select query without index as well as with index. my code is
    SELECT *  INTO CORRESPONDING FIELDS OF TABLE ITAB_COEP FROM COEP AS A
                 INNER JOIN COBK  AS B ON ( A~BELNR = B~BELNR AND A~KOKRS = B~KOKRS AND A~GJAHR = B~GJAHR )
                 WHERE A~OBJNR = OBJNR
                 AND A~GJAHR = CURRY
                 AND B~GJAHR = CURRY
                 AND A~RBEST = ''
                 AND A~BEKNZ = 'S'
                 AND B~BELNR NE WA_COBK-BELNR
                 AND B~AWREF_REV = ''
                 AND B~AWORG_REV = ''
                 AND B~KOKRS = WA_COIOB-KOKRS
                 AND B~BLDAT BETWEEN START_DATE AND END_DATE
                 AND B~BLART IN ('SA','KA','KR','AB')  %_HINTS ORACLE 'INDEX("COEP" "COEP~ZBU")'.
    index name is ZBU.
    please any one let me know is there any syntax mistakes? but while checking and during the runtime it does'nt shows any error.
    Regards,
            Thangam.P

  • INNER join with dynamic table name ?

    Hi,
    I have a problem with this statement.
    DATA: g_dso_bic_dofr TYPE tabname.
    SELECT t1~/bic/ziparomr
           t2~/bic/zifremom
    INTO (wa_rater_paromr-/bic/ziparomr,
           wa_rater_paromr-/bic/zifremom)
    *   FROM /bic/azd0bfr5100 AS t1     "equivalent to the dynamic statement below
    " this is the  problem
        FROM (g_dso_bic_dofr )AS t1
        INNER JOIN /bic/pzifremom AS t2
          ON t1~/bic/ziparomr = t2~/bic/ziparomr
    " --- to here
        WHERE t1~/bic/zikom     =  v_kommune
    The compile check doesnt work when i use the variable table name.
    I get
    "Wrong expression "INNER" in FROM clause. WHERE condition."
    Can anyone help me.
    Br Rasmus.

    " this is the  problem
    *    FROM (g_dso_bic_dofr )AS t1   "<<--- check spaces in here
        FROM (g_dso_bic_dofr) AS t1    "<<--- and here
        INNER JOIN /bic/pzifremom AS t2
          ON t1~/bic/ziparomr = t2~/bic/ziparomr
    " --- to here
    I think there's the problem, the space behind the parenthesis.
    Regards

  • Problem in Accession inner join with [Microsoft][SQLServer 2000 Driver

    Hi
    Can you tell me what can be cause my sql statement is correct gives result but
    if i used below statement in my JSP ,cause error
    1)
    ps1=connection.prepareStatement("select RQM_IN_RQSTN_ID_PK,RQD_IN_ITM_ID_FKPK,RQD_VC_ITM_DSCRPTN,RQD_IN_STTS_ID_FK,C.SMT_VC_STTS_NM,a.RQM_DT_PRPSD_DLVRY_DT"+
    "from PAS_RQM_RQSTN_MSTR a "+
    "inner join PAS_RQD_RQSTN_DTLS b on a.RQM_IN_RQSTN_ID_PK = b.RQD_IN_RQSTN_ID_FKPK and RQD_IN_STTS_ID_FK = 4"+
    "INNER JOIN PAS_SMT_STTS_MSTR C ON C.SMT_IN_STTS_ID_PK=B.RQD_IN_STTS_ID_FK" +
    "where a.RQM_VC_DTRTMNT_INVLV='Technology' ");
    2) Error Generate
    java.sql.SQLException
    [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]Line 1: Incorrect syntax near 'a'.
    What can be the cause
    Thanks

    ps1=connection.prepareStatement("select
    RQM_IN_RQSTN_ID_PK,RQD_IN_ITM_ID_FKPK,RQD_VC_ITM_DSCRP
    TN,RQD_IN_STTS_ID_FK,C.SMT_VC_STTS_NM,a.RQM_DT_PRPSD_D
    LVRY_DT"+
    "from PAS_RQM_RQSTN_MSTR a "+
    "inner join PAS_RQD_RQSTN_DTLS b on
    a.RQM_IN_RQSTN_ID_PK = b.RQD_IN_RQSTN_ID_FKPK and
    RQD_IN_STTS_ID_FK = 4"+
    "INNER JOIN PAS_SMT_STTS_MSTR C ON
    C.SMT_IN_STTS_ID_PK=B.RQD_IN_STTS_ID_FK" +
    "where a.RQM_VC_DTRTMNT_INVLV='Technology' ");This is a problem due to not providing spaces where they are needed. See the places where I have highlighted.
    A better way is to generate the String first [use StringBuffer as a good practice for appending instead of String "+"] and print it out to check if it is correct.
    ***Annie***

  • Inner join with for-all entries

    Why is the below inner join-for all entries does not result in expected output ?
    REPORT  ZTST3                                   .
    tables : ztst1,ztst2 .
    * table ztst1 has 4 fields : mandt,ebeln,ebelp,etenr,char4. ; ztst2 has
    * 3 fields : mandt,ebeln,ebelp,matnr
    *Entries in ztst1
    * EBELN       EBELP   ETERN  CHAR4
    * 5000000000  00010    0001    abc
    * 5000000000  00010    0002    cbd
    * 5000000000  00010    0003    efg
    *Entries in ztst2
    * EBELN       EBELP   matnr
    * 5000000000  00010    matabc
    *expected itab after inner join
    * EBELN       EBELP   CHAR4  MAtnr
    * 5000000000  00010   abc    matabc
    * 5000000000  00010   cbd    matabc
    * 5000000000  00010   efg    matabc
    data : begin of itab1 occurs 0,
           ebeln type ebeln,
           ebelp type ebelp,
           end of itab1.
    data : begin of itab occurs 0,
           ebeln type ebeln,
           ebelp type ebelp,
           char4 type char4,
           matnr type matnr,
           end of itab.
    start-of-selection.
    itab1-ebeln = '5000000000'.
    itab1-ebelp = '00010'.
    append itab1.
    select ztst1~ebeln
           ztst1~ebelp
           ztst1~char4
           ztst2~matnr into corresponding fields of table itab
    from ztst1 inner join ztst2
    on ztst1~ebeln = ztst2~ebeln and
       ztst1~ebeln = ztst2~ebelp
    for all entries in itab1
    where
    ztst1~ebeln eq itab1-ebeln and
    ztst1~ebelp eq itab1-ebelp .
    * why does it return no entries;
    break-point.

    For example in the bellow case
    *Entries in ztst1
    EBELN       EBELP   ETERN  CHAR4
    5000000000  00010    0001    abc
    5000000000  00010    0002    cbd
    5000000000  00010    0003    efg
    5000000002  00020    0003    efg
    5000000002  00020    0003    efg
    *Entries in ztst2
    EBELN       EBELP   matnr
    5000000000  00010    matabc
    5000000002  00020    abc
    may it will return you 2 records yes, than I think you have under stand the working of u201Cfor all entriesu201D ?
    And the following case I think it will work fine.
    *Entries in ztst1
    key  EBELN       EBELP   ETERN  CHAR4
    1       5000000000  00010    0001    abc
    2       5000000000  00010    0002    cbd
    3       5000000000  00010    0003    efg
    4       5000000002  00020    0003    efg
    5       5000000002  00020    0003    efg
    *Entries in ztst2
    EBELN       EBELP   matnr
    5000000000  00010    matabc
    5000000002  00020    abc
    select  ztst1~key
    ztst1~ebeln
           ztst1~ebelp
           ztst1~char4
           ztst2~matnr into corresponding fields of table itab
    from ztst1 inner join ztst2
    on ztst1~ebeln = ztst2~ebeln and
       ztst1~ebelp = ztst2~ebelp
    for all entries in itab1
    where
    ztst1~ebeln eq itab1-ebeln and
    ztst1~ebelp eq itab1-ebelp .
    Sorry, I donu2019t have system with me right now and I am sending you with out testing but I am sure that it is working the same way.
    Please Reply if any problem
    Kind Regards,
    Faisal

  • 11g outer join with case statement - strange results.

    Hello All,
    I am experiencing a strange issues in 11g while using case statement.
    I am not able to reproduce this using sample data. Not sure what is wrong.
    I am not narrowing it to say the usage of case statemnt is giving wrong results, but that is my observation when I am doing trail and error testing.
    Here are the details.
    My Version
    SQL> select * from v$version;
    BANNER
    Oracle Database 11g Enterprise Edition Release 11.2.0.2.0 - 64bit Production
    PL/SQL Release 11.2.0.2.0 - Production
    CORE    11.2.0.2.0      Production
    TNS for Linux: Version 11.2.0.2.0 - Production
    NLSRTL Version 11.2.0.2.0 - Production
    SQL> My Query
    SELECT *
      FROM (SELECT DISTINCT mf.schedule,
                            mf.cptcode,
                            NVL (mf.modifier, '00') modifier2,
                            CASE
                               WHEN mf.feetype = 'H'
                                AND mf.multiplier IS NOT NULL THEN
                                  '      0.00'
                               WHEN (mf.feetype != 'H'
                                  OR  mf.feetype IS NULL)
                                AND mf.rbrvsvalue IS NOT NULL
                                AND mf.multiplier IS NOT NULL THEN
                                  LPAD ( TRIM (TO_CHAR ( (mf.rbrvsvalue * mf.multiplier) / 100, 9999999.99)), 10)
                               ELSE
                                  NULL
                            END
                               fee
              FROM provider.mpifee mf, mpi_udffee.udffeeactivecptcodes vld
             WHERE mf.schedule = 'SAPG1'
               AND mf.cptcode = vld.cptcode
               AND NVL (mf.modifier, 'NULL') = NVL (vld.modifier, 'NULL')) inline_fee,
           (SELECT arc.schedule col1, arc.procedurecode col2, NVL (arc.modifier, '00') AS col3
              FROM mpi_udffee.udffeeancfeedata arc
             WHERE monthofextract = '201202'
               AND arc.schedule = 'SAPG1'
               AND TRUNC (SYSDATE - 10) BETWEEN arc.recordeffectivedate AND arc.recordterminationdate) inline_data
    WHERE inline_fee.schedule = inline_data.col1(+)
       AND inline_fee.cptcode = inline_data.col2(+)
       AND inline_fee.modifier2 = inline_data.col3(+);
    In the above query the inline view inline_data returns zero rows. but NVL is still getting applied for col3 and I am getting 00 in the results( strange ).
    Results:
    SCHEDULE        CPTCO MODIFIER2  FEE             COL1  COL2  COL3
    SAPG1           49590 00             667.32                  00
    SAPG1           49611 00             781.03                  00
    SAPG1           49905 00             443.79                  00
    SAPG1           50205 00             883.56                  00
    SAPG1           50220 00            1315.15                  00
    SAPG1           50230 00            1638.74                  00
    SAPG1           50234 00            1666.16                  00
    SAPG1           50250 00            1566.14                  00
    SAPG1           50327 00             262.04                  00
    SAPG1           50541 00            1183.31                  00
    SAPG1           50620 00            1156.88                  00
    SAPG1           50650 00            1321.96                  00
    497 rows selected.
    Just the inline view inline_data,
    SQL> SELECT arc.schedule col1, arc.procedurecode col2, NVL (arc.modifier, '00') AS col3
      2            FROM mpi_udffee.udffeeancfeedata arc
      3           WHERE monthofextract = '201202'
      4             AND arc.schedule = 'SAPG1'
      5             AND TRUNC (SYSDATE - 10) BETWEEN arc.recordeffectivedate AND arc.recordterminationdate;
    no rows selectedMuch unusual thing is when I just remove the case statement from the inline view "inline_fee", I am getting right results,
    SELECT *
      FROM (SELECT DISTINCT mf.schedule,
                            mf.cptcode,
                            NVL (mf.modifier, '00') modifier2          <-- Removed Case statement here
              FROM provider.mpifee mf, mpi_udffee.udffeeactivecptcodes vld
             WHERE mf.schedule = 'SAPG1'
               AND mf.cptcode = vld.cptcode
               AND NVL (mf.modifier, 'NULL') = NVL (vld.modifier, 'NULL')) inline_fee,
           (SELECT arc.schedule col1, arc.procedurecode col2, NVL (arc.modifier, '00') AS col3
              FROM mpi_udffee.udffeeancfeedata arc
             WHERE monthofextract = '201202'
               AND arc.schedule = 'SAPG1'
               AND TRUNC (SYSDATE - 10) BETWEEN arc.recordeffectivedate AND arc.recordterminationdate) inline_data
    WHERE inline_fee.schedule = inline_data.col1(+)
       AND inline_fee.cptcode = inline_data.col2(+)
       AND inline_fee.modifier2 = inline_data.col3(+);
    SCHEDULE        CPTCO MODIFIER2  COL1  COL2  COL3
    SAPG1           46730 00
    SAPG1           46735 00
    SAPG1           46748 00
    SAPG1           46760 00
    SAPG1           46942 00
    SAPG1           46945 00
    SAPG1           47015 00
    SAPG1           47125 00
    SAPG1           47350 00
    SAPG1           47505 00
    SAPG1           47553 00interestingly explain plan for both the statements are exactly same,
    SELECT STATEMENT  ALL_ROWSCost: 138  Bytes: 1,078,274  Cardinality: 11,471                                
         9 HASH JOIN RIGHT OUTER  Cost: 138  Bytes: 1,078,274  Cardinality: 11,471                           
              2 PARTITION RANGE EMPTY  Cost: 2  Bytes: 150  Cardinality: 3                      
                   1 TABLE ACCESS FULL TABLE MPI_UDFFEE.UDFFEEANCFEEDATA Cost: 2  Bytes: 150  Cardinality: 3                 
              8 VIEW MPI_UDFFEE. Cost: 135  Bytes: 504,724  Cardinality: 11,471                      
                   7 HASH UNIQUE  Cost: 135  Bytes: 539,137  Cardinality: 11,471                 
                        6 HASH JOIN  Cost: 134  Bytes: 539,137  Cardinality: 11,471            
                             3 TABLE ACCESS FULL TABLE MPI_UDFFEE.UDFFEEACTIVECPTCODES Cost: 13  Bytes: 177,345  Cardinality: 25,335       
                             5 PARTITION LIST SINGLE  Cost: 120  Bytes: 600,600  Cardinality: 15,015  Partition #: 8       
                                  4 INDEX RANGE SCAN INDEX (UNIQUE) REPRICE.PK_MPIFEE Cost: 120  Bytes: 600,600  Cardinality: 15,015  Partition #: 9  Partitions accessed #11Is there anything wrong with the query? If not have anyone come across this issue or posted it as a bug or is there a patch?
    Update:
    when I set the parameter "_complex_view_merging"=false I am getting the right results even with case statement. But I don want to do some thing unsupported.
    Are there any other viable solutions?
    I appreciate the help.
    Thanks,
    G.
    Edited by: Ganesh Srivatsav on Apr 10, 2012 12:37 PM

    Hi Tubby,
    Right, the query transformation is going wrong. Following is from trace,
    SELECT "INLINE_FEE"."SCHEDULE" "SCHEDULE",
           "INLINE_FEE"."CPTCODE" "CPTCODE",
           "INLINE_FEE"."MODIFIER2" "MODIFIER2",
           "INLINE_FEE"."FEE" "FEE",
           "ARC"."SCHEDULE" "COL1",
           "ARC"."PROCEDURECODE" "COL2",
           CASE
              WHEN "ARC".ROWID IS NOT NULL THEN NVL ("ARC"."MODIFIER", '00')
              ELSE NULL
           END
              "COL3"
      FROM (SELECT DISTINCT "MF"."SCHEDULE" "SCHEDULE",
                            "MF"."CPTCODE" "CPTCODE",
                            NVL ("MF"."MODIFIER", :B2) "MODIFIER2",
                            CASE
                               WHEN ("MF"."FEETYPE" = :B3
                                 AND "MF"."MULTIPLIER" IS NOT NULL) THEN
                                  :B4
                               WHEN ( ("MF"."FEETYPE" <> :B5
                                    OR  "MF"."FEETYPE" IS NULL)
                                 AND "MF"."RBRVSVALUE" IS NOT NULL
                                 AND "MF"."MULTIPLIER" IS NOT NULL) THEN
                                  LPAD ( TRIM (TO_CHAR ( "MF"."RBRVSVALUE" * "MF"."MULTIPLIER" / :B6, :B7)), :B8)
                               ELSE
                                  NULL
                            END
                               "FEE"
              FROM "PROVIDER"."MPIFEE" "MF", "MPI_UDFFEE"."UDFFEEACTIVECPTCODES" "VLD"
             WHERE "MF"."SCHEDULE" = 'SAPG1'
               AND "MF"."CPTCODE" = "VLD"."CPTCODE"
               AND NVL ("MF"."MODIFIER", 'NULL') = NVL ("VLD"."MODIFIER", 'NULL')) "INLINE_FEE",
           "MPI_UDFFEE"."UDFFEEANCFEEDATA" "ARC"
    WHERE "INLINE_FEE"."SCHEDULE" = "ARC"."SCHEDULE"(+)
       AND "INLINE_FEE"."CPTCODE" = "ARC"."PROCEDURECODE"(+)
       AND "INLINE_FEE"."MODIFIER2" = CASE
                                         WHEN ("ARC".ROWID(+) IS NOT NULL) THEN NVL ("ARC"."MODIFIER"(+), '00')
                                         ELSE NULL
                                      END
       AND "ARC"."MONTHOFEXTRACT"(+) = '201202'
       AND "ARC"."SCHEDULE"(+) = 'SAPG1'
       AND TRUNC (SYSDATE-10) >= "ARC"."RECORDEFFECTIVEDATE"(+)
       AND TRUNC (SYSDATE-10) <= "ARC"."RECORDTERMINATIONDATE"(+)Does this refer to a specific bug?
    Thanks,
    G.

  • Can we use inner joins with for all entries?

    Hi,
        Can we use innerjoin on two tables MARA and MAKT against the materials in
        the  internal table.
        If so ,please let me know whether there is performance issue.Because if there is
        bad performance issue or something else like thise means,my project manager
        wont allow to include.
        So can one let me know about this.
    Thanks,
    Balaji

    Hi Arunkumar,
                               I think you are not clear.My question is can I use innerjoin with
    for all entries.For example below is my code.
    SELECT A~MATNR
             B~MAKTX
             A~MTART
             A~MATKL
             FROM MARA AS A INNER JOIN MAKT AS B
             ON AMATNR = BMATNR
             INTO TABLE IT_MARA_MAKT
             FOR ALL ENTRIES IN IT_MATNR
             WHERE A~MATNR = IT_MATNR-MATNR
             AND   A~EXTWG = P_EXTWG
             AND   A~SPART = P_SPART.
    Can we use like this for all entries along with innerjoins.
    Thanks,
    Balaji

  • Inner join with data also from non having join column

    I have an Dim table which has to be joined with Fact Table using inner join, but also display the Dimension value " MasterGroup " inspite of data present or not in the fact table.
    My Query:  .. This is an part of my stored procedure with more tables
    SELECT a.SalesAmount, b.HeaderGroup ,c.CityName
    FROM dbo.FactItem a
    inner join dbo.[DimHeader] b
    on a.sid = b.sid
    left join dbo.DimCity c
    on a.cityid = c.id
    My DimHeader Table will have data like
    MasterGroup
    ChildGroup
    etc...
    which is an attribute.
    There might not be any relevent fact data, but still i want the dimension data to display the "MasterGroup"
    ShanmugaRaj

    hello ,
    usually this is not possible when the column you want to display when the matches not found from a table having a distinct values which are hard to declared static..
    you can incorporate something like this if you cant change the join type ..
    select distinct HeaderGroup from dbo.[DimHeader] tbl_outerleft join (SELECT a.sid,a.SalesAmount, b.HeaderGroup ,c.CityName
    FROM dbo.FactItem a
    inner join dbo.[DimHeader] b
    on a.sid = b.sid
    left join dbo.DimCity c
    on a.cityid = c.id) tbl_Main on tbl_Main.sid =tbl_outer.sid
    Dilip Patil..

  • HT201413 im having problem with my ipod. it cannot be recognize with my laptop. how to fix these? i already tried ur suggestions here but still didnt work. and also it cannot connect to itunes. plese I really need a help.

    my ipod is not recognized by my latop and even not connecting to itunes. I also tried to connect to other laptop and it works. I think the problem is with the software, how to fix this? a big thanks.

    Update:  I tried the "Toshiba Recovery Wizard" after everything else either fizzled out or hung up. After going all the way thru the recovery process (up to 100%), I finally got an error message.....it didn't work. And now, when I fire up the computer, I don't even get to that menu with the recovery options....the only thing I can boot into is the screens with the various ways to run your OS (in "safe mode", "safe mode with networking", etc).
    I'm not a techie, but I'm guessing at this point, the part of my hard drive that got damaged in the fall was, at the very least, the partition with the recovery data. Couple that with the fact that this cheapo Toshiba laptop didn't even come with recovery disk (or ANY kind of disk, even basic installation software!), I'm screwed: I don't see any way to get a workable computer now without some kind of disk to boot from. So NOW my concern is more about spending the $$ for a new OS and THEN finding out the hard drive has other problems too...is broken in some other way to boot.
    How to check this? As I said, I did run "chkdsk" back when I could get into the recovery menu and run the fix-it programs. It didn't note any problems. Thinking of taking this opportunity to upgrade to Windows 7 from Vista (which I never liked), but I have to know that the computer is otherwise ok....how to be sure? Help!!

  • How to reduce these code using oop in AS3

    i have 8 movie clip and i use it as a navigation buttons and when clicked it should go to different links. i use these codes but i think these codes are not pure oop so can any one help me to reduce programming using oop but works in same way as it is doing right now.
    the codes are follows
    function buttonmode(link:MovieClip):void{
    link.buttonMode = true;
    buttonmode(rec1_mc);
    buttonmode(rec2_mc);
    buttonmode(rec3_mc);
    buttonmode(rec4_mc);
    buttonmode(rec5_mc);
    buttonmode(rec6_mc);
    buttonmode(rec7_mc);
    buttonmode(rec8_mc);
    function navigate1(e:MouseEvent):void{
    navigateToURL(new URLRequest("http://www.ebpearls.com" ),"_self");
    function navigate2(e:MouseEvent):void{
    trace("Button 2 is Clicked");
    function navigate3(e:MouseEvent):void{
    trace("Button 3 is Clicked");
    function navigate4(e:MouseEvent):void{
    trace("Button 4 is Clicked");
    function navigate5(e:MouseEvent):void{
    trace("Button 5 is Clicked");
    function navigate6(e:MouseEvent):void{
    trace("Button 6 is Clicked");
    function navigate7(e:MouseEvent):void{
    trace("Button 7 is Clicked");
    function navigate8(e:MouseEvent):void{
    trace("Button 8 is Clicked");
    rec1_mc.addEventListener(MouseEvent.CLICK,navigate1);
    rec2_mc.addEventListener(MouseEvent.CLICK,navigate2);
    rec3_mc.addEventListener(MouseEvent.CLICK,navigate3);
    rec4_mc.addEventListener(MouseEvent.CLICK,navigate4);
    rec5_mc.addEventListener(MouseEvent.CLICK,navigate5);
    rec6_mc.addEventListener(MouseEvent.CLICK,navigate6);
    rec7_mc.addEventListener(MouseEvent.CLICK,navigate7);
    rec8_mc.addEventListener(MouseEvent.CLICK,navigate8);

    I think the more OO way is like this:
    1. Create AbstractButton class and save it in a file called AbstractButton.as in the same folder the FLA file resides:
    package
        import flash.display.MovieClip;
        import flash.events.MouseEvent;
        public class AbstractButton extends MovieClip
            public function AbstractButton()
                super();
                this.stop();
                this.buttonMode = true;
                this.addEventListener(MouseEvent.ROLL_OVER, onRollOver);
                this.addEventListener(MouseEvent.ROLL_OUT, onRollOut);
                this.addEventListener(MouseEvent.CLICK, onClick);
            protected function onRollOver(e:MouseEvent):void
                // rollOver behaviour, e.g. this.gotoAndStop(2);
            protected function onRollOut(e:MouseEvent):void
                 // rollOut behaviour, e.g.  this.gotoAndStop(1);
            protected function onClick(e:MouseEvent = null):void
                var data:Object = new Object();
                data.id = this.name;
                var evt:NavEvent = new NavEvent(data, NavEvent.NAV_EVENT, true, true);
                dispatchEvent(evt);           
    2. Link button symbol in your library to the AbstractButton class: right click on it (in library), click Properties, in Linkage section mark Export for ActionScript box and put AbstractButton as Class, Base class field leave empty. If you use more than one button symbols form your library, do the same thing with them.
    3. Create NavEvent class, save it in NavEvent.as file in the same  folder:
    package
         import flash.events.Event;
        public class NavEvent extends Event
             public static const NAV_EVENT:String = "NavEvent";
             public var data:Object;
             public function NavEvent(data:Object, type:String,  bubbles:Boolean, cancelable:Boolean)
                 this.data = data;
                 super(NAV_EVENT, true, true);
    4. Drag your buttons on the stage, give them instance names as you did.
    5. On your main timeline (or in document class if you're using it) use the following code to controll button clicks. Here e.data.id carries the instance name of clicked button.
    this.root.addEventListener(NavEvent.NAV_EVENT, handleNavEvent);
    function handleNavEvent(e:NavEvent):void
        switch (e.data.id)
            case "rec1_mc":          
                navigateToURL(new URLRequest("http://www.ebpearls.com" ),"_self");
                break;
            case "rec2_mc":
                trace("2 clicked");
                break;
            case "rec3_mc":
                trace("3 clicked");
                break;
              // and so on       

Maybe you are looking for