How to re-write this big SELECT Query with INNER JOINs?

Hi Experts
I have a performance killer SELECT query with an inner join of 3 tables u2013 VBAP, VBAK and VBEP together, which populates records to an internal table INT_COLL_ORD. Based on these records selected, in another SELECT query, records are fetched from VBUK table to the internal table INT_VBUK.
SELECT A~VBELN A~POSNR A~MATNR A~KWMENG A~KBMENG A~ERDAT A~ERZET A~PSTYV D~AUART E~ETTYP E~EDATU
INTO TABLE INT_TAB_RES
FROM VBAP AS A INNER JOIN VBAK AS D
ON D~VBELN EQ A~VBELN AND D~MANDT EQ A~MANDT
INNER JOIN VBEP AS E
ON E~VBELN EQ A~VBELN AND E~POSNR EQ A~POSNR AND E~MANDT EQ A~MANDT
WHERE  A~VBELN IN s_VBELN AND
       D~auart in s_auart AND
       D~vkorg in s_vkorg AND
       D~vbtyp eq 'C'     AND
       ( ( matnr LIKE c_prefix_sp AND zz_msposnr NE 0 AND kbmeng EQ 0 )
       OR ( matnr LIKE c_prefix_fp AND kwmeng NE A~kbmeng ) ) AND
       A~ABGRU EQ SPACE AND
       A~MTVFP IN R_MTVFP AND
       A~PRCTR IN R_PRCT AND
       E~ETENR EQ '1'.
SORT INT_COLL_ORD BY VBELN POSNR ETTYP.
DELETE ADJACENT DUPLICATES FROM INT_TAB_RES COMPARING VBELN POSNR.
CHECK NOT INT_TAB_RES [] IS INITIAL.
SELECT VBELN UVALL CMGST INTO TABLE INT_VBUK
FROM VBUK FOR ALL ENTRIES IN INT_TAB_RES
WHERE VBELN = INT_TAB_RES-VBELN AND UVALL NE 'A'.
Now, the requirement is:
I want to split this query. Like, first join VBAK and VBUK first. With this selection, go to the inner join of VBAP and VBEP (on key VBELN) to get the results. How can I re-write this Query?
Please help.
Thx n Rgds

Hi Nagraj
As of your suggestion, I have re-written the query as below:
* Declarations
TYPES: BEGIN OF TYP_COLL_ORD,
        VBELN  LIKE VBAK-VBELN,
        POSNR  LIKE VBUP-POSNR,
        MATNR  LIKE VBAP-MATNR,
        KWMENG LIKE VBAP-KWMENG,
        KBMENG LIKE VBAP-KBMENG,
        ERDAT  LIKE VBAK-ERDAT,
        ERZET  LIKE VBAK-ERZET,
        PSTYV  LIKE VBAP-PSTYV,
        AUART  LIKE VBAK-AUART, u201Calready exists in type
        ETTYP  LIKE VBEP-ETTYP,
        EDATU  LIKE VBEP-EDATU.
TYPES: END OF TYP_COLL_ORD.
DATA: INT_COLL_ORD TYPE TABLE OF TYP_COLL_ORD WITH HEADER LINE.
TYPES: BEGIN OF TYP_VBUK,
        AUART  LIKE VBAK-AUART, u201Chave added this field
        VBELN  LIKE VBUK-VBELN,
        UVALL  LIKE VBUK-UVALL,
        CMGST  LIKE VBUK-CMGST.
TYPES: END OF TYP_VBUK.
DATA: INT_VBUK TYPE TABLE OF TYP_VBUK WITH HEADER LINE.
*QUERY#1 u2013 for VBAK & VBUK Join
SELECT A~AUART B~VBELN B~UVALL B~CMGST
INTO TABLE INT_VBUK
FROM VBAK AS A INNER JOIN VBUK AS B
ON A~VBELN EQ B~VBELN
WHERE A~VBELN IN s_VBELN AND
A~auart in s_auart AND
A~vkorg in s_vkorg AND
A~vbtyp eq 'C' AND
B~UVALL NE 'A'.
IF NOT INT_VBUK[] IS INITIAL.
SORT INT_VBUK BY VBELN.
DELETE ADJACENT DUPLICATES FROM INT_VBUK COMPARING VBELN.
*QUERY#2 u2013 for VBAP & VBEP Join
SELECT A~VBELN A~POSNR A~MATNR A~KWMENG A~KBMENG A~ERDAT A~ERZET A~PSTYV B~ETTYP B~EDATU
INTO TABLE INT_COLL_ORD
FROM VBAP AS A INNER JOIN VBEP AS B
ON B~VBELN EQ A~VBELN AND B~POSNR EQ A~POSNR AND B~MANDT EQ A~MANDT
FOR ALL ENTRIES IN INT_VBUK
WHERE A~VBELN = INT_VBUK-VBELN AND
( ( matnr LIKE c_prefix_sp AND zz_msposnr NE 0 AND kbmeng EQ 0 )
OR ( matnr LIKE c_prefix_fp AND kwmeng NE A~kbmeng ) ) AND
A~ABGRU EQ SPACE AND
A~MTVFP IN R_MTVFP AND
A~PRCTR IN R_PRCT AND
B~ETENR EQ '1'.
ENDIF.
  SORT INT_COLL_ORD BY  VBELN POSNR ETTYP.
  DELETE ADJACENT DUPLICATES FROM INT_COLL_ORD
    COMPARING VBELN POSNR.
  CHECK NOT INT_COLL_ORD[] IS INITIAL.
  LOOP AT INT_COLL_ORD.
    CLEAR: L_MTART,L_ATPPR,L_ETTYP.
    IF L_PREVIOUS_ETTYP NE INT_COLL_ORD-ETTYP OR
      L_PREVIOUS_AUART NE INT_COLL_ORD-AUART.
      READ TABLE INT_OVRCTL WITH KEY AUART = INT_COLL_ORD-AUART ETTYP = INT_COLL_ORD-ETTYP.
      CHECK SY-SUBRC NE 0.
Now, the issue is:
Please note that declaration for INT_COLL_ORD has a field AUART, which is used in further parts of program (see the statement just above)
But, since neither VBAP nor VBEP contains AUART field, it cannot be fetched through the QUERY#2. So this value is not populated into INT_COLL_ORD through SELECT Query.
Since this field is used in later part of program & that the internal table has no value for this field, it dumps!!
How to include this value into the INT_COLL_ORD?
Plz suggest....

Similar Messages

  • Select Query with Inner Join

    Dear Experts,
    I have writen a inner join code with MKPF and MSEG which taking too much time, while I have used index.
    Indexes are:
    MSEG
    MATNR
    WERKS
    LGORT
    BWART
    SOBKZ
    MKPF
    BUDAT
    MBLNR
    My Select Query is :
      SELECT B~MATNR
                   B~MAT_KDAUF
                   B~MAT_KDPOS
                   B~BWART
                   B~MENGE
                   B~MEINS
                   B~AUFNR
        INTO TABLE IT_MSEG
        FROM MKPF AS A
          INNER JOIN MSEG AS B
             ON AMBLNR EQ BMBLNR
            AND AMJAHR EQ BMJAHR
        WHERE A~BUDAT IN BUDAT
          AND B~MATNR IN MATNR
          AND B~WERKS IN WERKS
          AND B~LGORT IN LGORT
          AND B~BWART IN BWART.

    hi,
    you can use  for all entries  it will work faster then joins
    About it:*
    FOR ALL ENTRIES WHERE
    Syntax
    ... FOR ALL ENTRIES IN itab WHERE ... col operator itab-comp ...
    Effect
    If the addition FOR ALL ENTRIES is specified before the language element WHERE, then the components comp of the internal table itab can be used as operands when comparing with relational operators.
    The internal table itab must have a structured line type and the component comp must be compatible with the column col.
    The logical expression sql_cond of the WHERE condition can comprise various logical expressions by using AND and OR. However, if FOR ALL ENTRIES is specified, there must be at least one Comparison with a column of the internal table itab, which can be specified either statistically or dynamically (Release 6.40 and higher). In a statement with a SELECTstatement with FOR ALL ENTRIES, the addition ORDER BY can only be used with the addition PRIMARY KEY.
    The whole logical expression sql_cond is evaluated for each individual line of the internal table itab. The resulting set of the SELECT statement is the union of the resulting sets from the individual evaluations. Duplicate lines are automatically removed from the resulting set. If the internal table itab is empty, the whole WHERE statement is ignored and all lines in the database are put in the resulting set.
    Notes
    In Release 6.10 and higher, the same internal table can be specified after FOR ALL ENTRIES and after INTO.
    The addition FOR ALL ENTRIES is only possible before WHERE conditions of the SELECT statement.
    Example
    Exporting all flight data for a specified departure city. The relevant airlines and flight numbers are first put in an internal table entry_tab, which is evaluated in the WHERE condition of the subsquent SELECT statement.
    PARAMETERS p_city TYPE spfli-cityfrom.
    TYPES: BEGIN OF entry_tab_type,
             carrid TYPE spfli-carrid,
             connid TYPE spfli-connid,
           END OF entry_tab_type.
    DATA: entry_tab   TYPE TABLE OF entry_tab_type,
          sflight_tab TYPE SORTED TABLE OF sflight
                           WITH UNIQUE KEY carrid connid fldate.
    SELECT carrid connid
           FROM spfli
           INTO CORRESPONDING FIELDS OF TABLE entry_tab
           WHERE cityfrom = p_city.
    SELECT carrid connid fldate
           FROM sflight
           INTO CORRESPONDING FIELDS OF TABLE sflight_tab
           FOR ALL ENTRIES IN entry_tab
           WHERE carrid = entry_tab-carrid AND
                 connid = entry_tab-connid.
    hope it will help you
    Rahul sharma
    Edited by: RAHUL SHARMA on Sep 11, 2008 8:10 AM

  • Absolute dynamic select query with dynamic join and where

    Has anyone ever tried creating an absolutely dynamic SELECT query with dynamic Join and Where conditions.
    I have a requirement of creating such a query in an Utility Class, and i have written the code. But its throwing my sysntax errors.
    Please let me know where am I going wrong OR is it really possible to create such a dynamic Query??
        SELECT (FIELDS) INTO TABLE IT_TABLES
          FROM ( (ME->TABLE1)  inner join ( me->table2 )
          on ( on_condition ) )
          WHERE (me->where_fields).
    Ags.

    It worked for me in a following way:
    select * into corresponding fields of table <result_table>
            from (join_string)
            where (l_where).
    Where the contents of join_string were dynamically build using concatenation. So it will be something like
    concatenate ME->TABLE1 'as a INNER JOIN' me->table2 'as b ON (' into join_string separated by space.
    <...>
    add here matching/reference colums, something like
    concatenate 'a~' me->TABLE1_JOIN_COL into temp1.
    concatenate 'b~' me->TABLE2_JOIN_COL into temp2.
    concatenate join_string temp1 '=' temp2 into join_string separated by space.
    <...>
    concatenate join_string ')' into join_string separated by space.
    And then use similar approach for l_where variable.

  • Trying to convert SELECT query to Update query with INNER JOINS

    Assalam O Alaikum!
    I've tried to convert this query of mine but failed.
    I wish to use it for update datasource in data GridView. I'm fetching data with it but converting it to update is not helping giving multiple errors.
    I tried to share the pic but they don't let me do so. Its actually a gridView with check boxes. check the item and update it..
    Here is the query. Please help me with that.
    <pre>
    SELECT [rightsId], [saveRights], [updateRights],
    [viewRights], [deleteRights], [printRights],
    [processRights], [verifyRights], [unProcessRights],
    [unVerifyRights], CONVERT(VARCHAR(100),tblGroup.groupId)as groupId, convert(varchar(100),tblmenu.[menuId])as menuid
    FROM [tblRights] inner join tblMenu ON
    tblMenu.menuId=tblRights.menuId INNER JOIN
    tblGroup ON tblGroup.groupId=tblRights.rightsId
    </pre>

    code is fine the above query works fine with the fetching(select) but when I try to write it with update it doesn't. Here is the asp code. I'm doing nothing with c# or vb.
      <asp:GridView ID="GridView1" runat="server" AllowPaging="True"
                DataSourceID="ratGrid" AutoGenerateColumns="False"
                CssClass="GridViewStyle" Width="100%" AllowSorting="True" AutoGenerateEditButton="True" BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2">
                <Columns>
                    <asp:BoundField DataField="rightsId" HeaderText="rightsId" ItemStyle-Width="75px" SortExpression="rightsId" Visible="False">
                    <ItemStyle Width="75px" />
                    </asp:BoundField>
                    <asp:CheckBoxField DataField="saveRights" HeaderText="Save" SortExpression="saveRights" />
                    <asp:CheckBoxField DataField="updateRights" HeaderText="Update" SortExpression="updateRights" />
                    <asp:CheckBoxField DataField="viewRights" HeaderText="View" SortExpression="viewRights" />
                    <asp:CheckBoxField DataField="deleteRights" HeaderText="Delete" SortExpression="deleteRights" />
                    <asp:CheckBoxField DataField="printRights" HeaderText="Print" SortExpression="printRights" />
                    <asp:CheckBoxField DataField="processRights" HeaderText="Process" SortExpression="processRights" />
                    <asp:CheckBoxField DataField="verifyRights" HeaderText="Verify" SortExpression="verifyRights" />
                    <asp:CheckBoxField DataField="unProcessRights" HeaderText="UnProcess" SortExpression="unProcessRights" />
                    <asp:CheckBoxField DataField="unVerifyRights" HeaderText="UnVerify" SortExpression="unVerifyRights" />
                    <asp:BoundField DataField="groupId" HeaderText="groupId" ReadOnly="True" SortExpression="groupId" Visible="False" />
                    <asp:BoundField DataField="menuid" HeaderText="menuid" SortExpression="menuid" ReadOnly="True" Visible="False" />
                </Columns>
                <RowStyle CssClass="RowStyle" BackColor="#FFF7E7" ForeColor="#8C4510" />
                <PagerStyle CssClass="PagerStyle" ForeColor="#8C4510" HorizontalAlign="Center" />
                <SelectedRowStyle CssClass="SelectedRowStyle" BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
                <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
                <HeaderStyle CssClass="HeaderStyle" BackColor="#A55129" Font-Bold="True" ForeColor="White" />
                <AlternatingRowStyle CssClass="AltRowStyle" />
                <SortedAscendingCellStyle BackColor="#FFF1D4" />
                <SortedAscendingHeaderStyle BackColor="#B95C30" />
                <SortedDescendingCellStyle BackColor="#F1E5CE" />
                <SortedDescendingHeaderStyle BackColor="#93451F" />
            </asp:GridView>
            <asp:SqlDataSource ID="ratGrid"
                 runat="server"
                ConnectionString="<%$ ConnectionStrings:Conn_Str %>"
                SelectCommand="SELECT [rightsId], [saveRights], [updateRights],
    [viewRights], [deleteRights], [printRights],
    [processRights], [verifyRights], [unProcessRights],
    [unVerifyRights], CONVERT(VARCHAR(100),tblGroup.groupId)as groupId, convert(varchar(100),tblmenu.[menuId])as menuid
    FROM [tblRights] inner join tblMenu ON
    tblMenu.menuId=tblRights.menuId INNER JOIN
    tblGroup ON tblGroup.groupId=tblRights.rightsId"
               FilterExpression="menuId like '{0}%' and [groupId] like '{1}%'" >
                            <FilterParameters>
                <asp:ControlParameter ControlID="ddlmenu" Name="menu"
                        PropertyName="SelectedValue" Type="String" />
                <asp:ControlParameter ControlID="ddlgroup" Name="Country"
                        PropertyName="SelectedValue" Type="String" />
                </FilterParameters>
            </asp:SqlDataSource>
    Your table needs key(s) and you need to assign the key as DataKeys property for the GridView to make your UpDate and Delete work without coding.

  • Select query for inner join

    Hi all,
    give me the query with innerjoin
    Based on the input values of Material number (MARC-MATNR) from selection screen check and get the Special Procurement type details from MARC-SOBSL and Material description details from MAKT-MAKTX.
    basha....
    Moderator message - instead of asking, you should try to write the code yourself and get back to the forum if you have a specific question.
    Edited by: Rob Burbank on Apr 19, 2009 4:46 PM

    Hi,
    Please Test the following Sample Code it is according to your requirement hope will solve out your problem,
    TABLES: marc.
    SELECt-OPTIONS somatnr for marc-matnr.
    TYPES: BEGIN OF ty_matnr,
      matnr LIKE marc-matnr,
      sobsl LIKE marc-sobsl,
      maktx LIKE makt-maktx,
      END OF ty_matnr.
    DATA: it_matnr TYPE STANDARD TABLE OF ty_matnr WITH HEADER LINE.
    SELECT marc~matnr marc~sobsl makt~maktx
      into CORRESPONDING FIELDS OF TABLE it_matnr
      from marc INNER JOIN makt on ( marc~matnr eq makt~matnr )
      WHERE marc~matnr in somatnr.
    Best Regards,
    Faisal

  • Issue when suming the field in Select query using inner join

    Hi All,
    SELECT A~OI_SHNUM
           A~FORWAGENT        
           A~ROUTE
           A~SHTYP
           A~DTSHP_EACT
           A~/BIC/GTCLICENS
           A~/BIC/GTCADD04
           A~COMP_CODE
           SUM( C~GRS_WGT_DL )
           C~UNIT_OF_WT
      INTO TABLE I_LAYONE
      FROM ( ( /BIC/ANTCD000200 AS A
      INNER JOIN /BIC/AGSSD000700 AS B ON BOI_SHNUM = AOI_SHNUM )
      INNER JOIN /BIC/AGSSD000600 AS C ON CDELIV_NUMB = BDELIV_NUMB ).
    I need to sum the field C~GRS_WGT_DL
    While compiling it show the error given below
    The field "C~UNIT_OF_WT" from the SELECT list is is missing in the
    GROUP BY clause. is missing in the GROUP BY clause. is missing in the
    GROUP BY clause. is missing in the GROUP BY clause. is missing in the
    GROUP BY clause. is "C~UNIT_OF_W
    with regards,
    Thambe

    Hi,
    Try the following SQL statement. Hope it helps you.
    SELECT A~OI_SHNUM
    A~FORWAGENT
    A~ROUTE
    A~SHTYP
    A~DTSHP_EACT
    A~/BIC/GTCLICENS
    A~/BIC/GTCADD04
    A~COMP_CODE
    SUM( C~GRS_WGT_DL )
    C~UNIT_OF_WT
    INTO TABLE I_LAYONE
    FROM ( ( /BIC/ANTCD000200 AS A
    INNER JOIN /BIC/AGSSD000700 AS B ON BOI_SHNUM = AOI_SHNUM )
    INNER JOIN /BIC/AGSSD000600 AS C ON CDELIV_NUMB = BDELIV_NUMB )
    GROUP BY
    A~OI_SHNUM
    A~FORWAGENT
    A~ROUTE
    A~SHTYP
    A~DTSHP_EACT
    A~/BIC/GTCLICENS
    A~/BIC/GTCADD04
    A~COMP_CODE.
    Murthy.

  • Select quer with inner join

    table
    can any please modify the query
    thanking in advance
    BEGIN OF t_initord OCCURS 0,
    vbeln LIKE vbak-vbeln, " Sales Order
    posnr LIKE vbap-posnr, " Sales Order Line Item
    auart LIKE vbak-auart, " Sales Order Type
    bezei LIKE tvakt-bezei, " Sales Order Type Text
    billdo Like vbrk-vbeln, "billing document number
    matnr LIKE vbfa-matnr, " Material
    maktx LIKE makt-maktx, " Material Description
    bstnk LIKE vbak-bstnk, " Customer PO number
    bukrs LIKE vbak-bukrs_vf, " Company Code..
    vkorg LIKE vbak-vkorg, " Sales Org
    vtweg LIKE vbak-vtweg, " Distribution Channel
    spart LIKE vbak-spart, " Division
    vkgrp LIKE vbak-vkgrp, " Sales Group
    augru LIKE vbak-augru, " Order Reason
    abgru LIKE vbap-abgru, " Reson for rejection
    kwmeng LIKE vbap-kwmeng, " Order Line quantit
    deliv LIKE likp-vbeln, " Delivery Number
    delit LIKE lips-posnr, " Delivery Number
    rfmng LIKE vbfa-rfmng, " Delivery Qty..
    vbtyp_n LIKE vbfa-vbtyp_n, " R12 Defect 8938.
    mrnkz LIKE vbkd-mrnkz, " Subsequent Invoicing
    docurr LIKE vbak-waerk, " Document Currency
    netwr LIKE vbap-netwr, " Line Item Amt in Doc curr
    ocdate LIKE vbak-aedat, " Order change date
    objnr LIKE vbap-objnr, " Object number
    rad LIKE sy-datum, " Requested Arrival Date
    submi LIKE ekko-submi, " Collective number
    plant1 LIKE vbap-werks, " Supplying Plant
    ernam LIKE vbak-ernam, " Order Created by
    erdat LIKE vbak-erdat, " Order Created on
    lifnr LIKE ekko-lifnr, " Vendor
    soldto LIKE vbak-kunnr, " Sold-to Party
    shipto LIKE vbak-kunnr, " Ship-to Party
    etenr LIKE vbep-etenr, " Sales Order schedule line
    bmeng LIKE vbep-bmeng, " Order Sch Line quantity
    vrkme LIKE vbap-zieme, " Sales Order UOM
    cad LIKE sy-datum, " Confirmed Arrival Date
    ktokk LIKE lfa1-ktokk, " Vendor account group
    docurramt LIKE vbap-netwr, " Document Currency Amount
    locurr LIKE vbak-waerk, " Local Currency
    locurramt LIKE vbap-netwr, " Local Currency Amount
    werks1 LIKE vbap-werks, " Supplying Plant
    werks_name1 LIKE t001w-name1, " Plant description
    lifnr_name1 LIKE lfa1-name1, " Name of the Vendor
    stat TYPE g_stat_typ, " Status Code
    lstatus TYPE g_stext_typ, " Last Status
    soappr TYPE tj30t-txt30, " Sales Order Approval
    bdnum LIKE vbfa-vbeln, " Billing doc number
    bdlin LIKE vbrp-posnr, " Billing Item
    uecha_b LIKE vbrp-uecha, " Item of Batch split
    uecha_d LIKE lips-uecha, " Item of Batch split
    END OF t_initord.
    This is the internal table
    to get the details wrote a select query
    can any one please correct the select query
    SELECT t1vbeln t2posnr t1auart t4bezei
    t2matnr t5maktx
    t1bstnk t1bukrs_vf t1vkorg t1vtweg t1spart t1vkgrp
    t1augru t2abgru t2kwmeng t8vbeln
    t8posnn t8rfmng t8vbtyp_n t7mrnkz t2waerk t2netwr
    t1aedat t1objnr t1vdatu t1submi t2werks t1ernam t1~erdat
    t6lifnr t1kunnr t9~kunnr
    INTO TABLE t_initord
    FROM vbak AS t1
    INNER JOIN vbap AS t2
    ON t1vbeln EQ t2vbeln
    LEFT OUTER JOIN tvakt AS t4
    ON t1auart EQ t4auart
    AND t4~spras EQ c_en
    LEFT OUTER JOIN makt AS t5
    ON t2matnr EQ t5matnr
    AND t5~spras EQ c_en
    LEFT OUTER JOIN vbpa AS t9
    ON t1vbeln EQ t9vbeln
    AND t9~parvw EQ c_we
    LEFT OUTER JOIN ekko AS t6
    ON t1submi EQ t6ebeln
    LEFT OUTER JOIN vbkd AS t7
    ON t1vbeln EQ t7vbeln AND
    t7~posnr EQ c_initposnr
    LEFT OUTER JOIN vbfa AS t8
    ON t2vbeln EQ t8vbelv AND
    t2posnr EQ t8posnv
    WHERE t1~vbeln IN s_slord
    AND t1~vkbur IN s_sloff
    AND t1~vkgrp IN s_slgrp
    AND t1~kunnr IN s_custag
    AND t1~auart IN s_ortyp
    AND t1~vkorg IN s_slorg
    AND t1~vtweg IN s_disch
    AND t1~spart IN s_divi
    AND t1~erdat IN s_date
    AND t1~bukrs_vf IN s_ccode
    AND t2~ernam IN s_slcre.
    i need to modify the above query to get the additional information into the above table
    billing doument number
    document currency
    document currency amount
    billed quantity
    sold to country
    ship to country

    You can also refer to this code... for making your code better..
    SELECT MATNR
           WERKS
           DISMM
           DISPO
           BESKZ INTO TABLE I_MARC FROM MARC
                 WHERE WERKS IN S_WERKS  AND BESKZ = 'F' AND DISMM <> 'ND'.
      IF NOT I_MARC IS INITIAL.
      SELECT MATNR
             MAKTX INTO TABLE I_MAKT FROM MAKT
                   FOR ALL ENTRIES IN I_MARC
                   WHERE MATNR = I_MARC-MATNR.
      ENDIF.
    REFRESH I_MDPS.
    REFRESH I_MDEZ.
    REFRESH I_MT61D.
    REFRESH I_MDPS2.
    REFRESH I_MDEZ2.
    LOOP AT I_MARC INTO WA_MARC.
    CALL FUNCTION 'MD_STOCK_REQUIREMENTS_LIST_API'
      EXPORTING
        MATNR                          = WA_MARC-MATNR
        WERKS                          = WA_MARC-WERKS
    TABLES
       MDEZX                           = I_MDEZ
    EXCEPTIONS
       MATERIAL_PLANT_NOT_FOUND       = 1
       PLANT_NOT_FOUND                = 2
       OTHERS                         = 3
    LOOP AT I_MDEZ.
       I_MDEZ2-MATNR = WA_MARC-MATNR.
       I_MDEZ2-WERKS = WA_MARC-WERKS.
       MOVE-CORRESPONDING I_MDEZ TO I_MDEZ2.
       APPEND I_MDEZ2.
    ENDLOOP.
    CLEAR I_MDEZ.
    CLEAR I_MDEZ2.
      ENDLOOP.
    Deleting teh records from the internal table which are
    nither purchase order nor purchase requisition.
    if pur_ord = 'X' and Pur_req = 'X'.
      delete i_mdez2 where delkz <> 'BA' and delkz <> 'BE'.
    endif.
    if pur_ord = 'X' and Pur_req <> 'X'.
      delete i_mdez2 where delkz <> 'BE'.
    endif.
    if pur_req = 'X' and pur_ord <> 'X'.
      delete i_mdez2 where delkz <> 'BA'.
    endif.
    Populating the final internal table to be displayed.
    Sort i_mdez2 by matnr werks.
    SORT I_MAKT BY MATNR.
      LOOP AT I_MDEZ2 WHERE LIFNR IN S_LIFNR.
              wa_final-werks = i_mdez2-werks.
              WA_FINAL-LIFNR = I_MDEZ2-LIFNR.
              WA_FINAL-EXTRA = I_MDEZ2-EXTRA.
              WA_FINAL-MNG01 = I_MDEZ2-MNG01.
              WA_FINAL-AUSKT = I_MDEZ2-AUSKT.
              WA_FINAL-UMDAT = I_MDEZ2-UMDAT.
              WA_FINAL-WERKS = I_MDEZ2-WERKS.
              WA_FINAL-MATNR = I_MDEZ2-MATNR.
              wa_final-delkz = i_mdez2-delkz.
    For Message to be displayed with the Purchase Order and Purchase Requisition.
              PERFORM TEXT_EXCEPTION.
              READ TABLE I_MAKT INTO WA_MAKT WITH KEY MATNR = WA_FINAL-MATNR BINARY SEARCH.
              IF SY-SUBRC = 0.
                WA_FINAL-MAKTX = WA_MAKT-MAKTX.
              ENDIF.
           APPEND WA_FINAL TO I_FINAL .
      ENDLOOP.
        IF SY-SUBRC <> 0.
           MESSAGE I058.
           leave program.
        ENDIF.
    Selecting the Purchase Organisation from the EKKO table.
    if not i_final is initial.
      SELECT EBELN EKORG LIFNR INTO TABLE I_EKKO FROM EKKO
             FOR ALL ENTRIES IN I_FINAL
             WHERE LIFNR = I_FINAL-LIFNR.
    Selecting the name of the vendor from the LFA1 table.
      select lifnr name1 into table i_lfa1 from lfa1
             for all entries in i_final
             where lifnr = i_final-lifnr.
    Selecting the current date from the eban table.
       select matnr lfdat into table i_eban from eban
            for all entries in i_final
            where matnr = i_final-matnr." and bnfpo = i_ekpo-bnfpo.
    endif.
    if not i_ekko is initial.
    Selecting the Unit Price from EKPO for finding the Extended amount.
      SELECT EBELN ebelp NETPR MATNR  banfn bnfpo INTO TABLE I_EKPO FROM EKPO
             FOR ALL ENTRIES IN I_EKKO
             WHERE EBELN = I_EKKO-EBELN.
    endif.
    if not i_ekpo is initial.
    Selecting the current date from the Eket table.
    select ebeln ebelp eindt into table i_eket from eket
           for all entries in i_ekpo
           where ebeln = i_ekpo-ebeln and ebelp = i_ekpo-ebelp.
    endif.
    *Sorting the internal tables.
    SORT I_EKKO BY LIFNR.
    sort i_lfa1 by lifnr.
    SORT I_EKPO BY MATNR.
    sort i_eket by ebeln ebelp.
    sort i_eban by matnr.
      LOOP AT I_FINAL INTO WA_FINAL.
        READ TABLE I_EKKO INTO WA_EKKO WITH KEY LIFNR = WA_FINAL-LIFNR.
           WA_FINAL-EKORG = WA_EKKO-EKORG.
          wa_final-ebeln = wa_ekko-ebeln.
         read table i_lfa1 into wa_lfa1 with key lifnr = wa_final-lifnr.
           wa_final-name1 = wa_lfa1-name1.
           READ TABLE I_EKPO INTO WA_EKPO WITH KEY MATNR = WA_FINAL-MATNR.
             wa_final-ebelp = wa_ekpo-ebelp.
             WA_FINAL-NETPR = WA_EKPO-NETPR.
           if wa_final-delkz = 'BA'.
             read table i_eban into wa_eban with key matnr = wa_final-matnr.
             wa_final-dat00 = wa_eban-lfdat.
           endif.
           if wa_final-delkz = 'BE'.
             read table i_eket into wa_eket with key ebeln = wa_final-ebeln ebelp = wa_final-ebelp.
              wa_final-dat00 = wa_eket-eindt.
           endif.
           WA_FINAL-EXTND_AMOUNT = WA_FINAL-MNG01 * WA_FINAL-NETPR.
        MODIFY I_FINAL FROM WA_FINAL TRANSPORTING EKORG name1 dat00 EXTND_AMOUNT.
      ENDLOOP.
    In this I have taken many internal tables... according to the database table I am extracting from .. then I am bringing all the record in the final internal table which will be containing teh records from all the internal table that i have used..
    Regards,
    Jayant
    <b>Please award if helpful</b>

  • Query with INNER joins

    Hi
    How can we optimize instead of inner joins. or any other idea to improve it.
    Thanks in advance!
    Regards,
    LKRao

    What is 'it'?
    Read {message:id=9360002} and {message:id=9360003} , and adjust your post accordingly.
    Without a query, execution plan, database version, index information, etc. etc., I wouldn't know how to be of any help here...perhaps other volunteers have a crystal ball, but I forgot mine accidentally this morning...

  • Syntax errors in update query with inner joins and sub query.

    Below is the query:
    UPDATE sp_CFQ_Coord_Corrections 
    INNER JOIN (CFQ_Coord_Corrections 
    INNER JOIN CFQ_Referrals ON CFQ_Coord_Corrections.CorrID = CFQ_Referrals.RecID) 
    ON sp_CFQ_Coord_Corrections.ID = CFQ_Referrals.RecID 
    SET CFQ_Coord_Corrections.MatchFound = 1, 
    CFQ_Coord_Corrections.RecTblID = [CFQ_Referrals].[RecTblID], 
    sp_CFQ_Coord_Corrections.MatchFound = 1
    WHERE (((CFQ_Coord_Corrections.MatchFound)=0) 
    AND ((sp_CFQ_Coord_Corrections.MatchFound)=0) 
    AND ((CFQ_Coord_Corrections.RecImported)=1) 
    AND ((CFQ_Referrals.RecFileName)='COORDCORR_SPOINT') 
    AND ((CFQ_Referrals.RecCombKey)='No.Match') 
    AND ((sp_CFQ_Coord_Corrections.RecImported)=1));
    Error messages seen when executed:
    Msg 156, Level 15, State 1, Line 3
    Incorrect syntax near the keyword 'INNER'.
    Msg 102, Level 15, State 1, Line 10
    Incorrect syntax near 'CFQ_Coord_Corrections'.
    Please help.....

    Below is the query:
    UPDATE sp_CFQ_Coord_Corrections 
    INNER JOIN (CFQ_Coord_Corrections 
    INNER JOIN CFQ_Referrals ON CFQ_Coord_Corrections.CorrID = CFQ_Referrals.RecID) 
    ON sp_CFQ_Coord_Corrections.ID = CFQ_Referrals.RecID 
    SET CFQ_Coord_Corrections.MatchFound = 1, 
    CFQ_Coord_Corrections.RecTblID = [CFQ_Referrals].[RecTblID], 
    sp_CFQ_Coord_Corrections.MatchFound = 1
    WHERE (((CFQ_Coord_Corrections.MatchFound)=0) 
    AND ((sp_CFQ_Coord_Corrections.MatchFound)=0) 
    AND ((CFQ_Coord_Corrections.RecImported)=1) 
    AND ((CFQ_Referrals.RecFileName)='COORDCORR_SPOINT') 
    AND ((CFQ_Referrals.RecCombKey)='No.Match') 
    AND ((sp_CFQ_Coord_Corrections.RecImported)=1));
    Error messages seen when executed:
    Msg 156, Level 15, State 1, Line 3
    Incorrect syntax near the keyword 'INNER'.
    Msg 102, Level 15, State 1, Line 10
    Incorrect syntax near 'CFQ_Coord_Corrections'.
    Please help.....
    sp_CFQ_Coord_Corrections is a table and not a stored procedure.
    are these both tables "sp_CFQ_Coord_Corrections" and "CFQ_Coord_Corrections" different ??

  • Update Query with Inner Joins

    Dear Experts,
    Update Employee set desicode = o.dcod from employee e inner join operator o on
    e.employee_id = o.employe_id
    waiting for suggestions,

    The other thread from yesterday that Satyaki mentioned Update.
    William:
    The SQL Server syntax from the OP is their version of an updateable join view. One interesting quirk of the implementation is that it does not require a unique key in the joined table, and will not error even if there are multiple rows that could update a row in the main table. I am still trying to figure out how it decides which row to use for the update, or if it actually updates multiple times and last update wins.
    John

  • Select with inner join

    hi friends,
    can we use where clause in select statement with inner join.
    i am using following code which gives error (The column name "WERKS" has two meanings . .).
      SELECT AMATNR ACHARG AERSDA BMENGE B~MEINS
          FROM MCHA AS A INNER JOIN CHVW AS B
          ON ACHARG = BCHARG
          INTO TABLE T_OUTPUT1
          WHERE WERKS = PLANT AND CHARG = P_CHARG.
    thanks.

    Yes u can  use where clause in select statement with inner join.
    SELECT AMATNR ACHARG AERSDA BMENGE B~MEINS
    FROM MCHA AS A INNER JOIN CHVW AS B
    ON ACHARG = BCHARG
    INTO TABLE T_OUTPUT1
    WHERE WERKS = PLANT AND CHARG = P_CHARG.
    " mention  the table name in where condition also like awerks or bwerks.
    egards
    Rajendra

  • How to write XSJS Select Query with input parameters

    Hello Experts,
    I am creating a xsjs file and in that file I am trying to write a Select Query based on a Calculation View
    I have tried it the following way:
    var query = 'SELECT TOP 100 \"Name\", \"Address\", \"City\", \"Country\" FROM \"_SYS_BIC\".\"Test.HL/AddressView\"'
        + 'WITH PARAMETERS(\'PLACEHOLDER\' = (\'$$P_Name$$\', \' Akhil \'),'
      + '\'PLACEHOLDER\' = (\'$$P_City$$\', \' Lucknow \'))';
    But it gives me the "Mixed spaces and tabs error".
    How should I write XSJS Select Query with input parameters?
    Regards,
    Rohit

    >But it gives me the "Mixed spaces and tabs error".
    Mixed spaces and tabs has nothing to do with the syntax of the statement. You used both spaces and the tab in the content - which JSLint doesn't like.  Remove the beginning spaces of each line and use only one or the other.
    The actual syntax of your statement doesn't look right.  The problem is that you are escaping the \ when you don't need to if you are using ' instead of " for your string.  You escape with \" in the first line but then escape with \' in the 2nd and 3rd line.  That is going to cause serious parsing problems with the command.

  • How do I write this SQL command in Oracle

    Hi all
    I wriote this SQ L statement in Ms SQL Server. How do I write this sql command in Oracle?
    ALTER VIEW dbo.ConsumptionAS SELECT TOP 100 PERCENT ID,
    CONVERT(decimal(10, 2), SUM(CASE WHEN YEAR_MONTH = '200710' AND NbrDaysUsed != 0 THEN (QtyUsed/ Days_Usage) * 748.05 ELSE 0 END)) AS Oct2007,
    CONVERT(decimal(10, 2), SUM(CASE WHEN YEAR_MONTH = '200711' AND NbrDaysUsed != 0 THEN (QtyUsed/ Days_Usage) * 748.05 ELSE 0 END)) AS Nov2007,
    CONVERT(decimal(10, 2), SUM(CASE WHEN YEAR_MONTH = '200712' AND NbrDaysUsed != 0 THEN (QtyUsed/ Days_Usage) * 748.05 ELSE 0 END)) AS Dec2007,
    CONVERT(decimal(10, 2), SUM(CASE WHEN YEAR_MONTH = '200801' AND NbrDaysUsed != 0 THEN (QtyUsed/ Days_Usage) * 748.05 ELSE 0 END)) AS Jan2008,
    CONVERT(decimal(10, 2), SUM(CASE WHEN YEAR_MONTH = '200802' AND NbrDaysUsed != 0 THEN (QtyUsed/ Days_Usage) * 748.05 ELSE 0 END)) AS Feb2008,
    CONVERT(decimal(10, 2), SUM(CASE WHEN YEAR_MONTH = '200803' AND NbrDaysUsed != 0 THEN (QtyUsed/ Days_Usage) * 748.05 ELSE 0 END)) AS Mar2008,
    CONVERT(decimal(10, 2), SUM(CASE WHEN YEAR_MONTH = '200804' AND NbrDaysUsed != 0 THEN (QtyUsed/ Days_Usage) * 748.05 ELSE 0 END)) AS Apr2008,
    CONVERT(decimal(10, 2), SUM(CASE WHEN YEAR_MONTH = '200805' AND NbrDaysUsed != 0 THEN (QtyUsed/ Days_Usage) * 748.05 ELSE 0 END)) AS May2008,
    CONVERT(decimal(10, 2), SUM(CASE WHEN YEAR_MONTH = '200806' AND NbrDaysUsed != 0 THEN (QtyUsed/ Days_Usage) * 748.05 ELSE 0 END)) AS Jun2008 ,
    CONVERT(decimal(10, 2), SUM(CASE WHEN YEAR_MONTH = '200807' AND NbrDaysUsed != 0 THEN (QtyUsed/ Days_Usage) * 748.05 ELSE 0 END)) AS Jul2008 ,
    CONVERT(decimal(10, 2), SUM(CASE WHEN YEAR_MONTH = '200808' AND NbrDaysUsed != 0 THEN (QtyUsed/ NbrDaysUsed) * 748.05 ELSE 0 END)) AS Aug2008,
    CONVERT(decimal(10, 2), SUM(CASE WHEN YEAR_MONTH = '200809' AND NbrDaysUsed != 0 THEN (QtyUsed NbrDaysUsed) * 748.05 ELSE 0 END)) AS Sep2008
    FROM dbo.MasterConsumption WHERE YEAR_MONTH >= '200710' AND YEAR_MONTH <= '200809' GROUP BY ID ORDER BY ID
    I am very interested in this part:
    SUM(CASE WHEN YEAR_MONTH = '200710' AND NbrDaysUsed != 0 THEN (QtyUsed/ Days_Usage) * 748.05 ELSE 0 END)) AS Oct2007
    thanks
    Edited by: user631364 on Oct 27, 2008 8:25 AM
    Edited by: user631364 on Oct 27, 2008 8:26 AM
    Edited by: user631364 on Oct 27, 2008 8:27 AM

    Thank you!!
    Now let me aslk the second part of my question.
    This sql command:
    ALTER VIEW dbo.ConsumptionAS SELECT TOP 100 PERCENT ID,
    CONVERT(decimal(10, 2), SUM(CASE WHEN YEAR_MONTH = '200710' AND NbrDaysUsed != 0 THEN (QtyUsed/ Days_Usage) * 748.05 ELSE 0 END)) AS Oct2007,
    CONVERT(decimal(10, 2), SUM(CASE WHEN YEAR_MONTH = '200711' AND NbrDaysUsed != 0 THEN (QtyUsed/ Days_Usage) * 748.05 ELSE 0 END)) AS Nov2007,
    CONVERT(decimal(10, 2), SUM(CASE WHEN YEAR_MONTH = '200712' AND NbrDaysUsed != 0 THEN (QtyUsed/ Days_Usage) * 748.05 ELSE 0 END)) AS Dec2007,
    CONVERT(decimal(10, 2), SUM(CASE WHEN YEAR_MONTH = '200801' AND NbrDaysUsed != 0 THEN (QtyUsed/ Days_Usage) * 748.05 ELSE 0 END)) AS Jan2008,
    CONVERT(decimal(10, 2), SUM(CASE WHEN YEAR_MONTH = '200802' AND NbrDaysUsed != 0 THEN (QtyUsed/ Days_Usage) * 748.05 ELSE 0 END)) AS Feb2008,
    CONVERT(decimal(10, 2), SUM(CASE WHEN YEAR_MONTH = '200803' AND NbrDaysUsed != 0 THEN (QtyUsed/ Days_Usage) * 748.05 ELSE 0 END)) AS Mar2008,
    CONVERT(decimal(10, 2), SUM(CASE WHEN YEAR_MONTH = '200804' AND NbrDaysUsed != 0 THEN (QtyUsed/ Days_Usage) * 748.05 ELSE 0 END)) AS Apr2008,
    CONVERT(decimal(10, 2), SUM(CASE WHEN YEAR_MONTH = '200805' AND NbrDaysUsed != 0 THEN (QtyUsed/ Days_Usage) * 748.05 ELSE 0 END)) AS May2008,
    CONVERT(decimal(10, 2), SUM(CASE WHEN YEAR_MONTH = '200806' AND NbrDaysUsed != 0 THEN (QtyUsed/ Days_Usage) * 748.05 ELSE 0 END)) AS Jun2008 ,
    CONVERT(decimal(10, 2), SUM(CASE WHEN YEAR_MONTH = '200807' AND NbrDaysUsed != 0 THEN (QtyUsed/ Days_Usage) * 748.05 ELSE 0 END)) AS Jul2008 ,
    CONVERT(decimal(10, 2), SUM(CASE WHEN YEAR_MONTH = '200808' AND NbrDaysUsed != 0 THEN (QtyUsed/ NbrDaysUsed) * 748.05 ELSE 0 END)) AS Aug2008,
    CONVERT(decimal(10, 2), SUM(CASE WHEN YEAR_MONTH = '200809' AND NbrDaysUsed != 0 THEN (QtyUsed NbrDaysUsed) * 748.05 ELSE 0 END)) AS Sep2008
    FROM dbo.MasterConsumption WHERE YEAR_MONTH >= '200710' AND YEAR_MONTH <= '200809' GROUP BY ID ORDER BY ID
    was created with this query in SQL Server and then I saved it in a store procedure, that I scheduled to run montlhy
    SET ANSI_NULLS ON
    DECLARE @SQLString NVARCHAR(4000)
    /* Build the SQL string once.*/
    SET @SQLString = 'ALTER VIEW dbo.Consumption AS SELECT TOP 100 PERCENT ID, CONVERT(decimal(10, 2), SUM(CASE WHEN YEAR_MONTH = ' +
    "'" + dbo.CONLastMonth_fn(getdate(), month(getdate()) - 12) +
    "'" +
    ' AND NbrDaysUsed != 0 THEN (QtyUsed/ NbrDaysUsed) * 748.05 ELSE 0 END)) AS ' +
    dbo.CONMonthInEnglish(getdate(), month(getdate()) - 12) +
    … (GOES FROM current month -12 to current month -1)
    , CONVERT(decimal(10, 2), SUM(CASE WHEN YEAR_MONTH = ' +
    "'" + dbo.CONLastMonth_fn(getdate(), month(getdate()) - 1) +"'" +
    ' AND NbrDaysUsed != 0 THEN (QtyUsed/ NbrDaysUsed) * 748.05 ELSE 0 END)) AS ' +
    dbo.CONMonthInEnglish(getdate(), month(getdate()) - 1) +
    ' FROM dbo.MasterConsumption WHERE YEAR_MONTH >= ' +
    "'" + dbo.CONLastMonth_fn (getdate(), month(getdate())-12 ) +"'" +
    ' AND YEAR_MONTH <= ' +
    "'" + dbo.CONLastMonth_fn (getdate(), month(getdate())-1 ) +"'" +
    ' GROUP BY ID ORDER BY ID '
    EXEC sp_executesql @SQLString
    Is that something that can be done in Oracle in the same way?
    Do you use another approach?
    please advice
    Edited by: user631364 on Oct 27, 2008 10:19 AM
    Edited by: user631364 on Oct 27, 2008 10:21 AM
    Edited by: user631364 on Oct 27, 2008 10:21 AM
    Edited by: user631364 on Oct 27, 2008 10:22 AM
    Edited by: user631364 on Oct 27, 2008 10:23 AM
    Edited by: user631364 on Oct 27, 2008 10:23 AM
    Edited by: user631364 on Oct 27, 2008 10:24 AM

  • I'm no longer able to select the file size when emailing a photo. How can I solve this? iPhone 4S with iOS 7.1.1

    I'm no longer able to select the file size when emailing a photo. How can I solve this? iPhone 4S with iOS 7.1.1. Thanks a lot.

    Yeah - I tried that, several times; each time I get "iTunes is restoring the software on this iPhone" and after about 30 minutes the screen on the phone lights up again with the same screen I had before.  After that ... nothing.

  • I need to add a single field from with_item table . need to write select query with reference to company code , account doc no , fiscal year

    I need to add a single field from with_item table . need to write select query with reference to company code , account doc no , fiscal year

    Hi Arun ,
    Can you explain little bit more ??
    what is account doc no? 
    what are the transactions should be displayed in your output??
    -Rajesh N

Maybe you are looking for

  • What is business systems and services?

    Hi Experts, What is business systems and services and where are they prefered? and tell me the diff between them... Regards, Mahakrishnan T.

  • Help! I can't see my comments from the form I created

    Below is the form I created When I fill out the form in Firefox and access it through my email client that I set up in my email_form.php, I get the email, I see the name and the email address that I put in the form but I don't see the comments that I

  • My Photoshop isn't showing up in my Applications Folder OR Finder

    I odered my software from my school, and they sent me a disk. Everything download and is easily found in my Applications BUT Photoshop, and Creative Suite (which is what I odered) can't NOT have photoshop on there. It has to be on my computer somewhe

  • Hotspot problem: Only Apple products work, Why???

    Hi, I am having issues with the personal hot spot on my iPhone 4s. Firstly it was not there when I got the phone!! EE sent me a text, read and delete. Turn phone off and then on. It was there, Great!!! Why is it that I can only connect Apple products

  • Osx mountain lion purchase problem

    Hey , i have a problem purchasing os x mountain lion from apple store. Whenever i hit Buy button, and after entering my password, it redirects me to the billing info page. by the way, i dont have a payment method set. The method i will use for the pu