How to avoid select query in loop

Hi All,
I have to select entries from table iclpay looping it_dfkkop and delete the entries in it_dfkkop_tmp which are not present in iclpay.
For this If use below code its working fine, but there is performance issue
    CLEAR: it_iclpay[],it_iclpay.
    LOOP AT it_dfkkop_tmp.
      SELECT claim documentref paycat status FROM iclpay
                APPENDING CORRESPONDING FIELDS OF TABLE it_iclpay
                        WHERE
                              active = c_active AND
                              claim = it_dfkkop_tmp-claim AND
                              documentref = it_dfkkop_tmp-opbel.
      IF sy-subrc NE 0.
        DELETE it_dfkkop_tmp.
      ENDIF.
    ENDLOOP.
If I write the code like below its not working
    SELECT claim documentref paycat status FROM iclpay
            INTO TABLE it_iclpay
                    FOR ALL ENTRIES IN it_dfkkop_tmp WHERE
                          active = c_active AND
                          claim = it_dfkkop_tmp-claim AND
                          documentref = it_dfkkop_tmp-opbel.
Can anyone help on this.

It did not dump but did not get final(required) output.
I got the solution now.
Thanks a lot for responding.
I have written the below code
SELECT claim
           documentref
           paycat
           status
           FROM
           iclpay
           APPENDING CORRESPONDING FIELDS OF TABLE it_iclpay
           FOR ALL ENTRIES IN it_dfkkop_tmp WHERE
                              active = c_active AND
                              claim = it_dfkkop_tmp-claim AND
                              documentref = it_dfkkop_tmp-opbel.
    SORT it_dfkkop_tmp BY claim opbel.
    LOOP AT it_dfkkop_tmp.
      READ TABLE it_iclpay INTO  wa_iclpay WITH KEY
                          claim        = it_dfkkop_tmp-claim
                          documentref  = it_dfkkop_tmp-opbel.
      IF sy-subrc NE 0.
        DELETE it_dfkkop_tmp.
      ENDIF.
    ENDLOOP.

Similar Messages

  • How to avoid selection-screen?

    Hi,
    i have this short report:
    TABLES: MARA.
    SELECT-OPTIONS: S_MATNR FOR MARA-MATNR.
    AT SELECTION-SCREEN.
      IF SY-BATCH = 'X'.
    * how to avoid selection screen
      ENDIF.
    START-OF-SELECTION.
      SELECT * FROM MARA WHERE MATNR IN S_MATNR.
        WRITE: / MARA-MATNR.
      ENDSELECT.
    END-OF-SELECTION.
    in batch i don't want the selection-screen. Is it possible? How can i do this?
    Thanks.
    regards, Dieter

    Dieter Gröhn wrote:
    > Hi,
    >
    > i have this short report:
    >
    >
    > TABLES: MARA.
    > SELECT-OPTIONS: S_MATNR FOR MARA-MATNR.
    > *
    > AT SELECTION-SCREEN.
    >   IF SY-BATCH = 'X'.
    > * how to avoid selection screen
    >   ENDIF.
    > *
    > ************************************************************************
    > START-OF-SELECTION.
    > *
    >   SELECT * FROM MARA WHERE MATNR IN S_MATNR.
    >     WRITE: / MARA-MATNR.
    >   ENDSELECT.
    > *
    > END-OF-SELECTION.
    >
    >
    > in batch i don't want the selection-screen. Is it possible? How can i do this?
    >
    > Thanks.
    >
    > regards, Dieter
    Please check the value of the variable sy-binpt, I believe the value of the variable sy-binpt will be set to 'X'  if the program is called from a bath input, hope this helps.

  • Urgent - I have written select query between loop and endloop, Ple help out

    Can any one help out me on this select query. I have written select query between loop and endloop. When I execute the program it will take too much time in this query. Please help me out. Its very urgent.
    LOOP AT l_i_invoices ASSIGNING <l_invoices>.
          CLEAR alv_wa.
          alv_wa-bukrs = <l_invoices>-bukrs.
          alv_wa-gsber = <l_invoices>-gsber.
          CLEAR l_instid.
          CONCATENATE <l_invoices>-belnr <l_invoices>-gjahr INTO l_instid.
          SELECT top_wi_id FROM sww_wi2obj INTO TABLE l_i_swwwihead
                  WHERE catid   = c_catid_business_object
                    AND instid  = l_instid
                    AND typeid  = c_typeid_invoice
                    AND removed = space
                    AND ( wi_rh_task = c_task_buyer_message
                       OR wi_rh_task = c_task_buyer_message2 ).
          IF sy-subrc = 0.
            <l_invoices>-flag = 'X'.
          ELSE.
            <l_invoices>-flag = ' '.
          ENDIF.
          MODIFY l_i_invoices FROM <l_invoices> TRANSPORTING flag
                                WHERE belnr = <l_invoices>-belnr
                                   AND gjahr = <l_invoices>-gjahr.
          APPEND alv_wa TO i_alv.
        ENDLOOP.
    Thanks in advance.

    Here is a way to solve this problem.
    Choose somewhere before this loop processing to use that select. Therefore, you'll need use FOR ALL ENTRIES <that_loop_table> clause, and in the WHERE condition you need to specify that same fields.
    This way, you will have an internal table with all data you'll need to check.
    Then, inside your loop statement, use the READ TABLE command with the clause WITH KEY field = value, to read that record and use the value found.
    Like this sample:
      SELECT bukrs lifnr umsks umskz augdt augbl zuonr gjahr belnr buzei
             waers xblnr blart gsber ebeln zfbdt zbd1t zlsch
      INTO TABLE tg_bsak
      FROM bsak
      FOR ALL ENTRIES IN tl_bkpf_sel
      WHERE bukrs EQ tl_bkpf_sel-bukrs AND
            lifnr IN s_lifnr AND
            augbl EQ tl_bkpf_sel-belnr.
    LOOP AT tg_bseg INTO wa_bseg.
        READ TABLE tg_bsak INTO wa_bsak WITH KEY bukrs = wa_bseg-bukrs
                                                 gjahr = wa_bseg-gjahr
                                                 belnr = wa_bseg-belnr
                                                 BINARY SEARCH.
    if sy-subrc = 0.
    * do something
    endif.
    ENDLOOP.

  • How to assign table name for select query in loop.

    Hi friends my requirement is count the no of records of all the database table which comes into an internal table ."Check the below coding". Iam  fetching the tables from dd03l table into an internal table .plz give me a sujjesion how to assign a table name in select query  in a loop.
      SELECT tabname FROM dd09l
                INTO TABLE i_dd09l
                WHERE protokoll = 'X'.
      IF sy-subrc = 0.
        SORT i_dd09l BY tabname.
      ENDIF.
      LOOP AT i_dd09l.
        SELECT COUNT(*) INTO val FROM i_dd09l-tabname.
        IF sy-subrc = 0.
          i_dd09l-count = val.
        ENDIF.
        MODIFY i_dd09l INDEX sy-index.
        CLEAR val.
      ENDLOOP.
    error : 'I_DD09L-TABNAME' is not defined in the abap dictionary as a table.

    data: dy_table type ref to data,
          dy_line type ref to data,
          xfc type lvc_s_fcat,
          ifc type lvc_t_fcat,
          dy_field type ref to data.
    LOOP AT i_dd09l.
    perform get_structure using i_dd09l-tabname .
    CREATE DATA dy_table TYPE TABLE OF (_dd09l-tab_name).
        UNASSIGN <dyn_table>.
        ASSIGN dy_table->* TO <dyn_table>.
    SELECT COUNT(*) INTO val FROM <dyn_table>.
    IF sy-subrc = 0.
    i_dd09l-count = val.
    ENDIF.
    MODIFY i_dd09l INDEX sy-index.
    CLEAR val.
    ENDLOOP.
    form get_structure using p_table.
      data : idetails type abap_compdescr_tab,
      xdetails type abap_compdescr.
      data : ref_table_des type ref to cl_abap_structdescr.
    Get the structure of the table.
      ref_table_des ?=
      cl_abap_typedescr=>describe_by_name( p_table ).
      idetails = ref_table_des->components.
      loop at idetails into xdetails.
        clear xfc.
        xfc-fieldname = xdetails-name .
        xfc-datatype = xdetails-type_kind.
        xfc-inttype = xdetails-type_kind.
        xfc-intlen = xdetails-length.
        xfc-decimals = xdetails-decimals.
        append xfc to ifc.
      endloop.
    endform.                    "get_structure
    Try like this hope it will work.
    Regards,
    madan.

  • How to avoid select with in a loop.

    Hi friends,
    can any tell how to avoid the select with in a loop for the following code, since it badly effecting my performance. if possible with code
    SELECT matnr werks
          FROM marc
          INTO TABLE it_marc
          FOR ALL ENTRIES IN it_ausp_1
          WHERE matnr = it_ausp_1-objek AND
             werks = p_werks
            AND mmsta LE 60.
        SORT it_marc BY matnr.
        CLEAR wa_ausp_1.
        CLEAR wa_tabix.
        LOOP AT it_ausp_1 INTO wa_ausp_1.
          wa_tabix = sy-tabix.
          READ TABLE it_marc INTO wa_marc WITH
            KEY matnr = wa_ausp_1-objek
            BINARY SEARCH.
          IF sy-subrc EQ 0.
          ELSE.
            DELETE it_ausp_1 INDEX wa_tabix.
          ENDIF.
        ENDLOOP.                      " LOOP AT it_ausp_1 INTO wa_ausp_1.
        APPEND LINES OF it_ausp_1 TO it_tal.
      IF it_tal[] IS NOT INITIAL.
        LOOP AT t_plan_order INTO fs_plan_order.
          SELECT r~rsnum
                 r~rspos
                 r~matnr
                 r~nomng
                 r~meins
                 r~plnum
                 r~bdter
                 r~ewahr
                 r~alprf
                 r~posnr
                 r~baugr
                 m~dispo
              INTO TABLE t_resb
              FROM resb AS r
              INNER JOIN marc AS m
              ON rbaugr = mmatnr
             AND rwerks = mwerks
             FOR ALL ENTRIES IN it_tal
             WHERE r~rsnum = fs_plan_order-rsnum
               AND r~matnr = it_tal-objek
               AND r~xloek = ' '
               AND r~werks = p_werks.
          APPEND LINES OF t_resb TO t_tacl.
        ENDLOOP.

    SELECT r~rsnum r~rspos r~matnr  r~nomng r~meins r~plnum r~bdter r~ewahr r~alprf r~posnr r~baugr m~dispo
              INTO TABLE t_resb
              FROM resb AS r
              INNER JOIN marc AS m
             ON r~baugr = m~matnr
             AND r~werks = m~werks
             FOR ALL ENTRIES IN it_tal
             WHERE r~rsnum = fs_plan_order-rsnum
             AND r~matnr = it_tal-objek
             AND r~xloek = ' '
             AND r~werks = p_werks.
    The logic of this select is completely weird. Whcih order of the db-tables should be used, which indexes?
    On RESB matnr, werks and xloek are in one index, but rsnum not.
    Are the conditions in fs_plan_order and it_tal connected or independent?
    It is probably much faster, to leave the  fs_plan_order condition away and check it on the result.
    SELECT r~rsnum r~rspos r~matnr  r~nomng r~meins r~plnum r~bdter r~ewahr r~alprf r~posnr r~baugr m~dispo
              INTO TABLE t_resb
              FROM resb AS r
              INNER JOIN marc AS m
             ON   m~matnr  = r~baugr
             AND m~werks =  r~werks
             FOR ALL ENTRIES IN it_tal
             WHERE         AND r~matnr = it_tal-objek
             AND r~xloek = ' '
             AND r~werks = p_werks.
    sort fs_plan_order by rsnum.
    loop at it_tal in wa.
       read table fs_plan_order
              with key rsnum = wa-rsnum
              binary search.
       if sy-subrc eq 0.
         append ...
       endif.
    endloop.
    Siegfried

  • How to alter Select Query while altering selectionscreen for same output?

    Hi,
    i have to change the selection screen fields with some new fields , but i dont know how to change the selection query accordingly, because i need the same output with the same fields but with a different set of fields in the selection screen.
    The original code is
    TYPES: BEGIN OF ty_output,
           mblnr TYPE mseg-mblnr,
           mjahr TYPE mseg-mjahr,
           zeile TYPE mseg-zeile,
           bwart TYPE mseg-bwart,
           werks TYPE mseg-werks,
           lgort TYPE mseg-lgort,
           ebeln TYPE mseg-ebeln,
           ebelp TYPE mseg-ebelp,
           lfbnr TYPE mseg-lfbnr,
           lfpos TYPE mseg-lfpos,
           sjahr TYPE mseg-sjahr,
           dmbtr TYPE mseg-dmbtr,
           menge TYPE mseg-menge,
           matnr TYPE mseg-matnr,
           mtart TYPE mara-mtart,
           matkl TYPE mara-matkl,
           bismt TYPE mara-bismt,
           meins TYPE mara-meins,
           bedat TYPE ekko-bedat,
           waers TYPE ekko-waers,
           wkurs TYPE ekko-wkurs,
           verkf TYPE ekko-verkf,
          EFFWR TYPE EKPO-EFFWR,
           tax TYPE ekpo-effwr,
           maktx TYPE makt-maktx,
           budat TYPE mkpf-budat,
           usnam TYPE mkpf-usnam,
           m_menge TYPE ekpo-menge,
           ablad TYPE mseg-ablad,
           wempf TYPE mseg-wempf,
           END OF ty_output.
    TYPES: BEGIN OF ty_mseg_103,
           mblnr TYPE mseg-mblnr,
           mjahr TYPE mseg-mjahr,
           zeile TYPE mseg-zeile,
           bwart TYPE mseg-bwart,
           werks TYPE mseg-werks,
           lgort TYPE mseg-lgort,
           ebeln TYPE mseg-ebeln,
           ebelp TYPE mseg-ebelp,
           lfbnr TYPE mseg-lfbnr,
           lfpos TYPE mseg-lfpos,
           sjahr TYPE mseg-sjahr,
           dmbtr TYPE mseg-dmbtr,
           menge TYPE mseg-menge,
           matnr TYPE mseg-matnr,
           mtart TYPE mara-mtart,
           matkl TYPE mara-matkl,
           bismt TYPE mara-bismt,
           meins TYPE mara-meins,
           bedat TYPE ekko-bedat,
           waers TYPE ekko-waers,
           wkurs TYPE ekko-wkurs,
           verkf TYPE ekko-verkf,
          EFFWR TYPE EKPO-EFFWR,
           tax TYPE ekpo-effwr,
           maktx TYPE makt-maktx,
           budat TYPE mkpf-budat,
           usnam TYPE mkpf-usnam,
           m_menge TYPE ekpo-menge,
           ablad TYPE mseg-ablad,
           wempf TYPE mseg-wempf,
           END OF ty_mseg_103.
    TYPES: BEGIN OF ty_mseg_105,
           mblnr TYPE mseg-mblnr,
           mjahr TYPE mseg-mjahr,
           zeile TYPE mseg-zeile,
           bwart TYPE mseg-bwart,
           werks TYPE mseg-werks,
           lgort TYPE mseg-lgort,
           ebeln TYPE mseg-ebeln,
           ebelp TYPE mseg-ebelp,
           lfbnr TYPE mseg-lfbnr,
           lfpos TYPE mseg-lfpos,
           sjahr TYPE mseg-sjahr,
           dmbtr TYPE mseg-dmbtr,
           menge TYPE mseg-menge,
           matnr TYPE mseg-matnr,
           mtart TYPE mara-mtart,
           matkl TYPE mara-matkl,
           bismt TYPE mara-bismt,
           meins TYPE mara-meins,
           bedat TYPE ekko-bedat,
           waers TYPE ekko-waers,
           wkurs TYPE ekko-wkurs,
           verkf TYPE ekko-verkf,
           effwr TYPE ekpo-effwr,
           tax TYPE ekpo-effwr,
           maktx TYPE makt-maktx,
           budat TYPE mkpf-budat,
           ablad TYPE mseg-ablad,
           wempf TYPE mseg-wempf,
           END OF ty_mseg_105.
    DATA: Begin with W_                                                 *
    DATA : it_fcat TYPE slis_t_fieldcat_alv. " alv field catalog
    DATA  : listhead TYPE slis_t_listheader WITH HEADER LINE.
    DATA: event TYPE slis_t_event WITH HEADER LINE.
    INTERNAL TABLES: Begin with IT_                                     *
    DATA : it_output TYPE STANDARD TABLE OF ty_output WITH KEY mblnr mjahr,
           it_mseg_103 TYPE STANDARD TABLE OF ty_mseg_103,
           it_mseg_105 TYPE STANDARD TABLE OF ty_mseg_103,
           it_mseg_106 TYPE STANDARD TABLE OF ty_mseg_103,          " LNTDEV1 ADDED
           it_mara TYPE STANDARD TABLE OF ty_mara,
           it_ekko TYPE STANDARD TABLE OF ty_ekko,
           it_ekpo TYPE STANDARD TABLE OF ty_ekpo,
           it_makt TYPE STANDARD TABLE OF ty_makt,
           it_mkpf TYPE STANDARD TABLE OF ty_mkpf WITH KEY mblnr.
    WORKAREAS: Begin with WA_                                           *
    DATA : wa_fcat LIKE LINE OF it_fcat.
    DATA : wa_output LIKE LINE OF it_output,
           wa_mara LIKE LINE OF it_mara,
           wa_ekko LIKE LINE OF it_ekko,
           wa_ekpo LIKE LINE OF it_ekpo,
           wa_makt LIKE LINE OF it_makt,
           wa_mkpf LIKE LINE OF it_mkpf,
           wa_mseg_103 LIKE LINE OF it_mseg_103,
           wa_mseg_105 LIKE LINE OF it_mseg_105,
           wa_mseg_106 LIKE LINE OF it_mseg_106.                        " LNTDEV1 ADDED
    DATA : w_menge LIKE ekpo-menge.
    FIELD-SYMBOLS: Begin with FS_                                       *
    PARAMETERS: Begin with PR_                                          *
    SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME.
    PARAMETERS  : pr_werks LIKE mseg-werks OBLIGATORY.
    SELECT-OPTIONS : so_budat FOR mkpf-budat.
    SELECTION-SCREEN END OF BLOCK b1.
    SELECT-OPTIONS: Begin with SO_                                      *
    SELECTION-SCREEN BEGIN OF BLOCK b2 WITH FRAME.
    SELECT-OPTIONS : so_mtart FOR mara-mtart.
    SELECT-OPTIONS : so_matkl FOR mara-matkl.
    SELECT-OPTIONS : so_matnr FOR mara-matnr.
    SELECT-OPTIONS : so_bismt FOR mara-bismt.
    SELECT-OPTIONS : so_ebeln FOR mseg-ebeln.
    SELECT-OPTIONS : so_verkf FOR ekko-verkf.
    SELECT-OPTIONS : so_bedat FOR ekko-bedat.
    SELECT-OPTIONS : so_mblnr FOR mseg-mblnr.
    SELECTION-SCREEN END OF BLOCK b2.
    *AT SELECTION-SCREEN.
    *SO_BUDAT-HIGH = SO_BUDAT-HIGH + 1.
    Query for getting all the Document Segment: Material data from MSEG *
    Table which having Movement Types 103 and put into it_mseg table,   *
    Filtering by reference document and material document number        *
    Filtering by selection criteria (Material Docu No, Purchase order   *
    and Plant)                                                          *
    START-OF-SELECTION.
      SELECT pmblnr smblnr p~mjahr zeile bwart lfbnr werks lgort ebeln
             ebelp lfpos sjahr dmbtr usnam menge sbudat pmatnr q~mtart
             pablad pwempf
             FROM mseg AS p INNER JOIN mara AS q
               ON pmatnr = qmatnr
               JOIN mkpf AS s ON smblnr EQ pmblnr
               INTO CORRESPONDING FIELDS OF
             TABLE it_mseg_103
             WHERE bwart EQ 103
             AND p~matnr IN so_matnr
             AND werks EQ pr_werks
             AND ebeln IN so_ebeln
             AND q~mtart IN so_mtart
             AND p~mblnr IN so_mblnr
             AND s~budat IN so_budat.
      IF sy-subrc = 0.
      ENDIF.
      SELECT pmblnr smblnr p~mjahr zeile bwart lfbnr werks lgort ebeln
             ebelp lfpos sjahr dmbtr usnam menge sbudat pmatnr q~mtart
             pablad pwempf
             FROM mseg AS p INNER JOIN mara AS q
               ON pmatnr = qmatnr
               JOIN mkpf AS s ON smblnr EQ pmblnr
               INTO CORRESPONDING FIELDS OF
             TABLE it_mseg_105
          WHERE bwart EQ 105 OR bwart EQ 104 OR bwart EQ 124 OR bwart EQ 124
             AND p~matnr IN so_matnr
             AND werks EQ pr_werks
             AND ebeln IN so_ebeln
             AND q~mtart IN so_mtart
             AND p~mblnr IN so_mblnr
             AND s~budat IN so_budat.
      IF sy-subrc = 0.
      ENDIF.
      LOOP AT IT_MSEG_103 INTO WA_MSEG_103.
        READ TABLE IT_MSEG_105 INTO WA_MSEG_105 WITH KEY LFBNR =
                                               WA_MSEG_103-MBLNR.
          IF SY-SUBRC <> 0.
            APPEND WA_MSEG_103 TO IT_OUTPUT.
          ENDIF.
       ENDLOOP.
      SELECT pmblnr smblnr p~mjahr zeile bwart lfbnr werks lgort ebeln  
             ebelp lfpos sjahr dmbtr usnam menge sbudat pmatnr q~mtart
             pablad pwempf
             FROM mseg AS p INNER JOIN mara AS q
               ON pmatnr = qmatnr
               JOIN mkpf AS s ON smblnr EQ pmblnr
               INTO CORRESPONDING FIELDS OF
             TABLE it_mseg_106
             WHERE bwart EQ 106
             AND p~matnr IN so_matnr
             AND werks EQ pr_werks
             AND ebeln IN so_ebeln
             AND q~mtart IN so_mtart
             AND p~mblnr IN so_mblnr
             AND s~budat IN so_budat.
      LOOP AT it_mseg_103 INTO wa_mseg_103.
        READ TABLE it_mseg_106 INTO wa_mseg_106 WITH KEY
                                           lfbnr = wa_mseg_103-mblnr.
        IF sy-subrc = 0.
          APPEND wa_mseg_106 TO it_output.
          clear wa_mseg_106.
        ENDIF.
      ENDLOOP.                        
    Present selection criteria is
    1. Plant
    2. Posting Date
    3. Material Type
    4. Material Group
    5. Material No.
    6. Old Material No.
    7. PO Order No.
    8. Old PO No.
    9. PO Date
    10. Material Document No.
    New selection criteria is
    1. PO no.
    2. PO date
    3. PO value (Ex: >=10000 <=50000)
    4. Purch org
    5. Doc type
    6. Vendor
    7. Material
    8. Account assignment category
    9. Order

    Hi,
    i have to change the selection screen fields with some new fields , but i dont know how to change the selection query accordingly, because i need the same output with the same fields but with a different set of fields in the selection screen.
    The original code is
    TYPES: BEGIN OF ty_output, mblnr TYPE mseg-mblnr, mjahr TYPE mseg-mjahr, zeile TYPE mseg-zeile, bwart TYPE mseg-bwart, werks TYPE mseg-werks, lgort TYPE mseg-lgort, ebeln TYPE mseg-ebeln, ebelp TYPE mseg-ebelp, lfbnr TYPE mseg-lfbnr, lfpos TYPE mseg-lfpos, sjahr TYPE mseg-sjahr, dmbtr TYPE mseg-dmbtr, menge TYPE mseg-menge, matnr TYPE mseg-matnr, mtart TYPE mara-mtart, matkl TYPE mara-matkl, bismt TYPE mara-bismt, meins TYPE mara-meins, bedat TYPE ekko-bedat, waers TYPE ekko-waers, wkurs TYPE ekko-wkurs, verkf TYPE ekko-verkf, * EFFWR TYPE EKPO-EFFWR, tax TYPE ekpo-effwr, maktx TYPE makt-maktx, budat TYPE mkpf-budat, usnam TYPE mkpf-usnam, m_menge TYPE ekpo-menge, ablad TYPE mseg-ablad, wempf TYPE mseg-wempf, END OF ty_output. TYPES: BEGIN OF ty_mseg_103, mblnr TYPE mseg-mblnr, mjahr TYPE mseg-mjahr, zeile TYPE mseg-zeile, bwart TYPE mseg-bwart, werks TYPE mseg-werks, lgort TYPE mseg-lgort, ebeln TYPE mseg-ebeln, ebelp TYPE mseg-ebelp, lfbnr TYPE mseg-lfbnr, lfpos TYPE mseg-lfpos, sjahr TYPE mseg-sjahr, dmbtr TYPE mseg-dmbtr, menge TYPE mseg-menge, matnr TYPE mseg-matnr, mtart TYPE mara-mtart, matkl TYPE mara-matkl, bismt TYPE mara-bismt, meins TYPE mara-meins, bedat TYPE ekko-bedat, waers TYPE ekko-waers, wkurs TYPE ekko-wkurs, verkf TYPE ekko-verkf, * EFFWR TYPE EKPO-EFFWR, tax TYPE ekpo-effwr, maktx TYPE makt-maktx, budat TYPE mkpf-budat, usnam TYPE mkpf-usnam, m_menge TYPE ekpo-menge, ablad TYPE mseg-ablad, wempf TYPE mseg-wempf, END OF ty_mseg_103. TYPES: BEGIN OF ty_mseg_105, mblnr TYPE mseg-mblnr, mjahr TYPE mseg-mjahr, zeile TYPE mseg-zeile, bwart TYPE mseg-bwart, werks TYPE mseg-werks, lgort TYPE mseg-lgort, ebeln TYPE mseg-ebeln, ebelp TYPE mseg-ebelp, lfbnr TYPE mseg-lfbnr, lfpos TYPE mseg-lfpos, sjahr TYPE mseg-sjahr, dmbtr TYPE mseg-dmbtr, menge TYPE mseg-menge, matnr TYPE mseg-matnr, mtart TYPE mara-mtart, matkl TYPE mara-matkl, bismt TYPE mara-bismt, meins TYPE mara-meins, bedat TYPE ekko-bedat, waers TYPE ekko-waers, wkurs TYPE ekko-wkurs, verkf TYPE ekko-verkf, effwr TYPE ekpo-effwr, tax TYPE ekpo-effwr, maktx TYPE makt-maktx, budat TYPE mkpf-budat, ablad TYPE mseg-ablad, wempf TYPE mseg-wempf, END OF ty_mseg_105. *----------------------------------------------------------------------* * DATA: Begin with W_ * *----------------------------------------------------------------------* DATA : it_fcat TYPE slis_t_fieldcat_alv. " alv field catalog DATA : listhead TYPE slis_t_listheader WITH HEADER LINE. DATA: event TYPE slis_t_event WITH HEADER LINE. *----------------------------------------------------------------------* * INTERNAL TABLES: Begin with IT_ * *----------------------------------------------------------------------* DATA : it_output TYPE STANDARD TABLE OF ty_output WITH KEY mblnr mjahr, it_mseg_103 TYPE STANDARD TABLE OF ty_mseg_103, it_mseg_105 TYPE STANDARD TABLE OF ty_mseg_103, it_mseg_106 TYPE STANDARD TABLE OF ty_mseg_103, " LNTDEV1 ADDED it_mara TYPE STANDARD TABLE OF ty_mara, it_ekko TYPE STANDARD TABLE OF ty_ekko, it_ekpo TYPE STANDARD TABLE OF ty_ekpo, it_makt TYPE STANDARD TABLE OF ty_makt, it_mkpf TYPE STANDARD TABLE OF ty_mkpf WITH KEY mblnr. *----------------------------------------------------------------------* * WORKAREAS: Begin with WA_ * *----------------------------------------------------------------------* DATA : wa_fcat LIKE LINE OF it_fcat. DATA : wa_output LIKE LINE OF it_output, wa_mara LIKE LINE OF it_mara, wa_ekko LIKE LINE OF it_ekko, wa_ekpo LIKE LINE OF it_ekpo, wa_makt LIKE LINE OF it_makt, wa_mkpf LIKE LINE OF it_mkpf, wa_mseg_103 LIKE LINE OF it_mseg_103, wa_mseg_105 LIKE LINE OF it_mseg_105, wa_mseg_106 LIKE LINE OF it_mseg_106. " LNTDEV1 ADDED DATA : w_menge LIKE ekpo-menge. *----------------------------------------------------------------------* * FIELD-SYMBOLS: Begin with FS_ * *----------------------------------------------------------------------* *----------------------------------------------------------------------* * PARAMETERS: Begin with PR_ * *----------------------------------------------------------------------* SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME. PARAMETERS : pr_werks LIKE mseg-werks OBLIGATORY. SELECT-OPTIONS : so_budat FOR mkpf-budat. SELECTION-SCREEN END OF BLOCK b1. *----------------------------------------------------------------------* * SELECT-OPTIONS: Begin with SO_ * *----------------------------------------------------------------------* SELECTION-SCREEN BEGIN OF BLOCK b2 WITH FRAME. SELECT-OPTIONS : so_mtart FOR mara-mtart. SELECT-OPTIONS : so_matkl FOR mara-matkl. SELECT-OPTIONS : so_matnr FOR mara-matnr. SELECT-OPTIONS : so_bismt FOR mara-bismt. SELECT-OPTIONS : so_ebeln FOR mseg-ebeln. SELECT-OPTIONS : so_verkf FOR ekko-verkf. SELECT-OPTIONS : so_bedat FOR ekko-bedat. SELECT-OPTIONS : so_mblnr FOR mseg-mblnr. SELECTION-SCREEN END OF BLOCK b2. *AT SELECTION-SCREEN. * *SO_BUDAT-HIGH = SO_BUDAT-HIGH + 1. *---------------------------------------------------------------------* * Query for getting all the Document Segment: Material data from MSEG * * Table which having Movement Types 103 and put into it_mseg table, * * Filtering by reference document and material document number * * Filtering by selection criteria (Material Docu No, Purchase order * * and Plant) * *---------------------------------------------------------------------* *---------------------------------------------------------------------* START-OF-SELECTION. *---------------------------------------------------------------------* SELECT p~mblnr s~mblnr p~mjahr zeile bwart lfbnr werks lgort ebeln ebelp lfpos sjahr dmbtr usnam menge s~budat p~matnr q~mtart p~ablad p~wempf FROM mseg AS p INNER JOIN mara AS q ON p~matnr = q~matnr JOIN mkpf AS s ON s~mblnr EQ p~mblnr INTO CORRESPONDING FIELDS OF TABLE it_mseg_103 WHERE bwart EQ 103 AND p~matnr IN so_matnr AND werks EQ pr_werks AND ebeln IN so_ebeln AND q~mtart IN so_mtart AND p~mblnr IN so_mblnr AND s~budat IN so_budat. ** IF sy-subrc = 0. ENDIF. SELECT p~mblnr s~mblnr p~mjahr zeile bwart lfbnr werks lgort ebeln ebelp lfpos sjahr dmbtr usnam menge s~budat p~matnr q~mtart p~ablad p~wempf FROM mseg AS p INNER JOIN mara AS q ON p~matnr = q~matnr JOIN mkpf AS s ON s~mblnr EQ p~mblnr INTO CORRESPONDING FIELDS OF TABLE it_mseg_105 WHERE bwart EQ 105 OR bwart EQ 104 OR bwart EQ 124 OR bwart EQ 124 AND p~matnr IN so_matnr AND werks EQ pr_werks AND ebeln IN so_ebeln AND q~mtart IN so_mtart AND p~mblnr IN so_mblnr AND s~budat IN so_budat. IF sy-subrc = 0. ENDIF. LOOP AT IT_MSEG_103 INTO WA_MSEG_103. READ TABLE IT_MSEG_105 INTO WA_MSEG_105 WITH KEY LFBNR = WA_MSEG_103-MBLNR. IF SY-SUBRC 0. APPEND WA_MSEG_103 TO IT_OUTPUT. ENDIF. ENDLOOP. SELECT p~mblnr s~mblnr p~mjahr zeile bwart lfbnr werks lgort ebeln ebelp lfpos sjahr dmbtr usnam menge s~budat p~matnr q~mtart p~ablad p~wempf FROM mseg AS p INNER JOIN mara AS q ON p~matnr = q~matnr JOIN mkpf AS s ON s~mblnr EQ p~mblnr INTO CORRESPONDING FIELDS OF TABLE it_mseg_106 WHERE bwart EQ 106 AND p~matnr IN so_matnr AND werks EQ pr_werks AND ebeln IN so_ebeln AND q~mtart IN so_mtart AND p~mblnr IN so_mblnr AND s~budat IN so_budat. LOOP AT it_mseg_103 INTO wa_mseg_103. READ TABLE it_mseg_106 INTO wa_mseg_106 WITH KEY lfbnr = wa_mseg_103-mblnr. IF sy-subrc = 0. APPEND wa_mseg_106 TO it_output. clear wa_mseg_106. ENDIF. ENDLOOP.
    Present selection criteria is 1. Plant 2. Posting Date 3. Material Type 4. Material Group 5. Material No. 6. Old Material No. 7. PO Order No. 8. Old PO No. 9. PO Date 10. Material Document No. New selection criteria is 1. PO no. 2. PO date 3. PO value (Ex: >=10000 <=50000) 4. Purch org 5. Doc type 6. Vendor 7. Material 8. Account assignment category 9. Order
    I hope the code is readable now.
    Thanks in advance.

  • How to avoid 'select for update'

    Hi,
    we are using the bc4j framework of jdev 3.2.3. We have a View which collects data from several tables in different database schemes, so we naturally have to use the 'union clause'. But if we try to make an update on a row (via 'setAttribute'), we got a DMLException ORA-02014 telling us that we should not use 'select for update' on views whith unions.
    Since i cannot avoid using a union clause, is there a way to avoid the 'select for update' in the bc4j framework?
    Please help, its urgent!
    Thanx,
    Dietmar

    SELECT FOR UPDATE is used for our implementation or row-level locking.
    If you are using pessimistic locking mode, this will occur the first time any attribute is modified.
    If you are using optimistic locking mode, it will be deferred until post/commit time.
    If you are using "none" locking mode, it will not happen in your application may hang indefinitely if another session has locked the row.
    Are you asking how to avoid locking?
    Do you mean to be updating this view with a union over multiple databases?

  • How to run select query from one SID to fetch record from another SID?

    I have two SID, one is AAA and another is BBB and the connection name is A1 and B1 respectively. I want to run one select query from A1 connection to fetch record B1. Can anyone help me how I can connect from one SID to another SID?

    Hi 869357,
    You can also use the copy command. (Works with limited types)
    try:
    help copyRegards,
    Turloch

  • How to write selection Query for the following requirment.

    Hi All,
    I am new to ABAP, I need a help ,
    I need to select all plants(WERKS) from MARC at Plant/Material level,
    then I need to take all sales organozation(VKORG) from T001w,
    then I need the company code(BUKRS) from TVKO based on VKORG,
    then I need the currency key(WAERS) from T001 based on BUKRS,
    Can any one help me in writing selection Query for the same?
    Thanks All,
    Debrup.

    Hi,
    Its easy for you if you learn SELECT with JOIN to complete your task. So SEARCH the forum with SELECT statement and you will get a lot of examples using which you can write your own.
    If you struck up anywhere revert back.
    Regards
    Karthik D

  • How to store select query output in arrays.

    i created a varray type variables and now want to assign some data though select query output in pl/sql code as well as in reports 6i.

    You're in the wrong forum (this one is for issues with the SQL Developer tool). You were in the right one where you posted first, but don't reuse unrelated threads as you did.
    Regards,
    K.

  • Need help in logic change to avoid select statement inside loop .

    Hello all ,
    Please see the below code . Here i got the shipment number in internal table  IT_VTTK . So For this shipment number i want to get the details from Vekp table in to internal table  IT_VEKP  . There is no need for me to use For all entries . I have passed the value of it_vekp into work area wa_vekp and getting the details in to IT_vekp using loop.
    But since we should not use the select statement inside the loop , Can anybody please tell me how to achieve this functionality .
    Instead of passing into work area wa_vttk if i directly use the internal table I'm getting the following error .
    "IT_VTTK" is a table without a header line and therefore has no component called "TKNUM".
    Hence i have used select statement inside a loop . Please tell me is there any other way to avoid this ?
    types:begin of ty_likp,
            vbeln type likp-vbeln,
           end of ty_likp,
           begin of ty_vttk,
            tknum type vttk-tknum,
            end of ty_vttk.
    DATA: IT_VEKP TYPE STANDARD TABLE OF TY_VEKP,
          WA_VEKP TYPE TY_VEKP.
      data:it_likp type standard table of ty_likp,
           wa_likp type ty_likp,
           it_likp1 type standard table of ty_likp,
           it_vttk type standard table of ty_vttk,
           wa_vttk type ty_vttk.
      data:w_tknum type vttk-tknum.
    if not it_likp is initial .
        select vbeln
                from
                 vbfa
          into table it_vttk
           for all entries in it_likp where vbelv = it_likp-vbeln and vbtyp_n = '8'.
      endif .
    IF IT_VTTK[] IS NOT INiTIAL.
        loop at it_vttk into wa_vttk.
        SELECT venum
               EXIDV
               EXIDV2
               BRGEW
               NTGEW
               GEWEI
               TARAG
               GEWEI
               VHART
               INHALT
             FROM VEKP INTO TABLE IT_VEKP where VPOBJKEY = wa_vttk-tknum  and vpobj = '4'.
          endloop.
          endif.

    Hello,
    Why would you not like to use FOR ALL ENTRIES in the second select? Is it becasue of the length or type mismatch error between the fields VPOBJKEY and wa_vttk-tknum ?
    Vikranth

  • How to write select query for all the user tables in database

    Can any one tell me how to select the columns from all the user tables in a database
    Here I had 3columns as input...
    1.phone no
    2.memberid
    3.sub no.
    I have to select call time,record,agn from all the tables in a database...all database tables have the same column names but some may have additional columns..
    Eg: select call time, record,agn from ah_t_table where phone no= 6186759765,memberid=j34563298
    Query has to execute not only for this table but for all user tables in the database..all tables will start with ah_t
    I am trying for this query since 30days...
    Help me please....any kind of help is appreciated.....

    Hi,
    user13113704 wrote:
    ... i need to include the symbol (') for the numbers(values) to get selected..
    eg: phone no= '6284056879'To include a single-quote in a string literal, use 2 or them in a row, as shown below.
    Starting in Oracle 10, you can also use Q-notation:
    http://download.oracle.com/docs/cd/E11882_01/server.112/e17118/sql_elements003.htm#i42617
    ...and also can you tell me how to execute the output of this script. What front end are you using? If it's SQL*Plus, then you can SPOOL the query to a file, and then execute that file, like this:
    -- Suppress SQL*Plus features that interfere with raw output
    SET     FEEDBACK     OFF
    SET     PAGESIZE     0
    -- Run preliminary query to generate main query
    SPOOL     c:\my_sql_dir\all_ah_t.sql
    SELECT       'select call time, record, agn from '
    ||       owner
    ||       '.'
    ||       table_name
    ||       ' where phone_no = ''6186759765'' and memberid = j34563298'
    ||       CASE
               WHEN ROW_NUMBER () OVER ( ORDER BY  owner          DESC
                              ,        table_name      DESC
                              ) = 1
               THEN  ';'
               ELSE  ' UNION ALL'
           END     AS txt
    FROM       all_tables
    WHERE       SUBSTR (table_name, 1, 4)     = 'AH_T'
    ORDER BY  owner
    ,       table_name
    SPOOL     OFF
    -- Restore SQL*Plus features that interfere with raw output (if desired)
    SET     FEEDBACK     ON
    SET     PAGESIZE     50
    -- Run main query:
    @c:\my_sql_dir\all_ah_t.sql
    so that i form a temporary view for this script as a table(or store the result in a temp table) and my problem will be solved..Sorry, I don't understand. What is a "temporary view"?

  • How  to write select query for this

    Hi,
    I had a html form and in the for i had drop down box and it needs to select multiple values from the drop down box. When i select multiple values then i have to write the SQL select statement  query .
    When i try to write the select statement and trying to run i am getting error.
    select * from Table
    where emo_no = '1,2,3'
    this is how i write query please suggest me how  to write query for selecting multiple values from the drop down box.
    Thanks

    select * from Table
    where emo_no in ( 1,2,3)
    for integer values
    select * from Table
    where emo_no in ('1','2','3')
    for characters
    If we talk about large scale applications that may have millions of records, I would suggest this.
    declare @t table (v int)
    insert into t (v) values (1)
    insert into t (v) valves (2)
    insert into t (v) values (3)
    select *
    from table
         inner join @t t on table.emo_no = t.v
    Using "in" for a where clause is not so bad for filtering on a few values, but if you are filtering a lot of rows and a lot of values (emo_no) the performance degrades quickly for some reasons beyond the scope of this.
    This is just one solution, I'll through this out as well, instead of an in memory (@t) table, doing a disk based temp table (#t) and creating an index on the column "v".
    create table #t (v int)
    insert into #t (v) values (1)
    insert into #t (v) valves (2)
    insert into #t (v) values (3)
    create index ix_t on #t (v)
    select *
    from table
         inner join #t t on table.emo_no = t.v
    drop table #t
    Pardon any syntax errors and careful using a drop statement.
    Sometimes in memory tables work better than disk temp tables, it takes some testing and trial and error depending on your datasets to determine the best solution.
    Probably too much info  ;-)
    Byron Mann
    [email protected]
    [email protected]
    Software Architect
    hosting.com | hostmysite.com
    http://www.hostmysite.com/?utm_source=bb

  • How to improve 'select query with subselects'?

    Oracle Database 11g Release 11.1.0.7.0 - 64bit Production
    PL/SQL Release 11.1.0.7.0Hi there,
    I'm a C# developer so I really have troubles with understanding PL/SQL. I use a lot of subselects in my queries because I find it easy to understand, however, I need to learn to make use of the power of queries. I made this little library sample to illustrate the problem I have in a smaller scale. I have multiple subselects looking in the same table, running through the same amount of columns to obtain data. This is a waste of performance and resources.
    Idea of the query:
    Information about a book
    - Location of this book
    - Number of unique readers of this book
    - Number of days it was rent
    - Number of reservation of this book
    Test data
    with
    book as (
    select 1 as id, 'reference0001' as reference, 'titel0001' as title, 'description0001' as description, 'author0001' as author, 1 as locid from dual union all
    select 2 as id, 'reference0002' as reference, 'titel0002' as title, 'description0002' as description, 'author0002' as author, 1 as locid from dual union all
    select 3 as id, 'reference0003' as reference, 'titel0003' as title, 'description0003' as description, 'author0003' as author, 1 as locid from dual union all
    select 4 as id, 'reference0004' as reference, 'titel0004' as title, 'description0004' as description, 'author0004' as author, 2 as locid from dual union all
    select 5 as id, 'reference0005' as reference, 'titel0005' as title, 'description0005' as description, 'author0005' as author, 2 as locid from dual union all
    select 6 as id, 'reference0006' as reference, 'titel0006' as title, 'description0006' as description, 'author0006' as author, 2 as locid from dual union all
    select 7 as id, 'reference0007' as reference, 'titel0007' as title, 'description0007' as description, 'author0007' as author, 3 as locid from dual union all
    select 8 as id, 'reference0008' as reference, 'titel0008' as title, 'description0008' as description, 'author0008' as author, 3 as locid from dual
    location as (
    select 1 as id, '100.1' as roomnumber from dual union all
    select 2 as id, '100.2' as roomnumber from dual union all
    select 3 as id, '100.3' as roomnumber from dual
    client as (
    select 1 as id, 'client001' as name from dual union all
    select 2 as id, 'client002' as name from dual union all
    select 3 as id, 'client003' as name from dual
    book_history as (
    select 1 as bookid, 1 as clientid, 10 as days, to_date('06-06-2012') as rentdate from dual union all
    select 3 as bookid, 1 as clientid, 5 as days, to_date('06-06-2012') as rentdate  from dual union all
    select 2 as bookid, 2 as clientid, 5 as days, to_date('07-06-2012') as rentdate  from dual union all
    select 1 as bookid, 2 as clientid, 5 as days, to_date('07-06-2012') as rentdate  from dual union all
    select 2 as bookid, 1 as clientid, 5 as days, to_date('08-06-2012') as rentdate  from dual union all
    select 2 as bookid, 1 as clientid, 5 as days, to_date('08-06-2012') as rentdate  from dual union all
    select 3 as bookid, 1 as clientid, 3 as days, to_date('09-06-2012') as rentdate  from dual
    reservation_history as (
    select 1 as bookid, 1 as clientid, to_date('04-06-2012') as reservationdate from dual union all
    select 3 as bookid, 1 as clientid, to_date('04-06-2012') as reservationdate  from dual union all
    select 2 as bookid, 2 as clientid, to_date('05-06-2012') as reservationdate  from dual union all
    select 1 as bookid, 2 as clientid, to_date('05-06-2012') as reservationdate  from dual union all
    select 2 as bookid, 1 as clientid, to_date('06-06-2012') as reservationdate  from dual union all
    select 2 as bookid, 1 as clientid, to_date('06-06-2012') as reservationdate  from dual union all
    select 3 as bookid, 1 as clientid, to_date('07-06-2012') as reservationdate  from dual
    )Normally my queries look like this
    select  b.id,
            b.reference,
            b.title,
            b.description,
            b.author,
            l.roomnumber,
               select count(distinct bh.clientid)
               from   book_history bh
               where  bh.bookid = b.id
               and    bh.rentdate >= to_date('01-01-2012')
               and    bh.rentdate &lt; to_date('01-01-2013')
             ) as different_readers,
            nvl((
               select sum(bh.days)
               from   book_history bh
               where  bh.bookid = b.id
               and    bh.rentdate >= to_date('01-01-2012')
               and    bh.rentdate &lt; to_date('01-01-2013')
             ),0) as days_of_rent,
              select count(1)
              from reservation_history rh
              where rh.bookid = b.id
              and   rh.reservationdate >= to_date('01-01-2012')
              and   rh.reservationdate &lt; to_date('01-01-2013')
            ) as times_reserved
    from    book b
    join    location l
      on     b.locid = l.id  I tried to improve it to this
      select b.id,
              b.reference,
              b.title,
              b.description,
              b.author,
           l.roomnumber,
           nvl(t1.clients, 0) as different_readers,
           nvl(t1.days, 0) as days_of_rent,
              select count(1)
              from reservation_history rh
              where rh.bookid = b.id
              and   rh.reservationdate >= to_date('01-01-2012')
              and   rh.reservationdate &lt; to_date('01-01-2013')
            ) as times_reserved
    from   book b
    left join location l
           on b.locid = l.id
    left join (select bh.bookid as bookid,
                      count(distinct bh.clientid) as clients,
                      sum(bh.days) as days
               from   book_history bh
               where  bh.rentdate >= to_date('01-01-2012')
               and    bh.rentdate &lt; to_date('01-01-2013')
               group by bh.bookid
               ) t1
            on t1.bookid = b.id  I am told that I should try to move out the group by from the last left join or try to use partitioning.
    Now this is a bit too much for my understanding. When I use partitioning
    select distinct
           b.id,
              b.reference,
              b.title,
              b.description,
              b.author,
           l.roomnumber,
           nvl(count(distinct bh.clientid) over (partition by bh.bookid), 0) as different_readers,
           nvl(sum(bh.days) over (partition by bh.bookid),0) as days_of_rent,
              select count(1)
              from reservation_history rh
              where rh.bookid = b.id
              and   rh.reservationdate >= to_date('01-01-2012')
              and   rh.reservationdate &lt; to_date('01-01-2013')
            ) as times_reserved
    from   book b
    left join location l
           on b.locid = l.id
    left join book_history bh
           on b.id = bh.bookid
          and bh.rentdate >= to_date('01-01-2012')
          and bh.rentdate &lt; to_date('01-01-2013')I suppressed same result rows by using distinct, however I am told to avoid using distinct as this slows down a lot.
    Is there any other solution I can use to achieve the same?
    I hope my question is clear enough to be answered! Thanks in advance for your time!
    Kind regards,
    Metroickha
    Edited by: 858378 on 6-feb-2013 2:37

    Thanks for providing the data in a usable format, that really helps.
    858378 wrote:
    I am told that I should try to move out the group by from the last left join or try to use partitioning.
    Now this is a bit too much for my understanding. When I use partitioning
    select b.id,
              b.reference,
              b.title,
              b.description,
              b.author,
    l.roomnumber,
    count(distinct bh.clientid) over (partition by bh.bookid) as different_readers,
    nvl(sum(bh.days) over (partition by bh.bookid),0) as days_of_rent
    from   book b
    left join location l
    on b.locid = l.id
    left join book_history bh
    on b.id = bh.bookid
    and bh.rentdate >= '01-01-2012'
    and bh.rentdate &lt; '01-01-2013'I get multiple rows with the same values (as expected). So what do I need to add to remove the multiple same rows?Use a DISTINCT...
    SQL> ed
    Wrote file afiedt.buf
      1  with book as (
      2      select 1 as id, 'reference0001' as reference, 'titel0001' as title, 'description0001' as description, 'author0001' as author, 1 as locid from dual union all
      3      select 2 as id, 'reference0002' as reference, 'titel0002' as title, 'description0002' as description, 'author0002' as author, 1 as locid from dual union all
      4      select 3 as id, 'reference0003' as reference, 'titel0003' as title, 'description0003' as description, 'author0003' as author, 1 as locid from dual union all
      5      select 4 as id, 'reference0004' as reference, 'titel0004' as title, 'description0004' as description, 'author0004' as author, 2 as locid from dual union all
      6      select 5 as id, 'reference0005' as reference, 'titel0005' as title, 'description0005' as description, 'author0005' as author, 2 as locid from dual union all
      7      select 6 as id, 'reference0006' as reference, 'titel0006' as title, 'description0006' as description, 'author0006' as author, 2 as locid from dual union all
      8      select 7 as id, 'reference0007' as reference, 'titel0007' as title, 'description0007' as description, 'author0007' as author, 3 as locid from dual union all
      9      select 8 as id, 'reference0008' as reference, 'titel0008' as title, 'description0008' as description, 'author0008' as author, 3 as locid from dual
    10      ),
    11    location as (
    12      select 1 as id, '100.1' as roomnumber from dual union all
    13      select 2 as id, '100.2' as roomnumber from dual union all
    14      select 3 as id, '100.3' as roomnumber from dual
    15      ),
    16    client as (
    17      select 1 as id, 'client001' as name from dual union all
    18      select 2 as id, 'client002' as name from dual union all
    19      select 3 as id, 'client003' as name from dual
    20      ),
    21    book_history as (
    22      select 1 as bookid, 1 as clientid, 10 as days, to_date('06-06-2012','DD-MM-YYYY') as rentdate from dual union all
    23      select 3 as bookid, 1 as clientid, 5 as days, to_date('06-06-2012','DD-MM-YYYY') as rentdate  from dual union all
    24      select 2 as bookid, 2 as clientid, 5 as days, to_date('07-06-2012','DD-MM-YYYY') as rentdate  from dual union all
    25      select 1 as bookid, 2 as clientid, 5 as days, to_date('07-06-2012','DD-MM-YYYY') as rentdate  from dual union all
    26      select 2 as bookid, 1 as clientid, 5 as days, to_date('08-06-2012','DD-MM-YYYY') as rentdate  from dual union all
    27      select 2 as bookid, 1 as clientid, 5 as days, to_date('08-06-2012','DD-MM-YYYY') as rentdate  from dual union all
    28      select 3 as bookid, 1 as clientid, 3 as days, to_date('09-06-2012','DD-MM-YYYY') as rentdate  from dual
    29      )
    30  select  distinct
    31          b.id
    32         ,b.reference
    33         ,b.title
    34         ,b.description
    35         ,b.author
    36         ,l.roomnumber
    37         ,count(distinct bh.clientid) over (partition by b.id) as different_readers
    38         ,nvl(sum(bh.days) over (partition by b.id),0) as days_of_rent
    39  from    book b
    40          join location l on (b.locid = l.id)
    41          left outer join book_history bh on (   bh.bookid = b.id
    42                                             and bh.rentdate >= to_date('01-01-2012','DD-MM-YYYY')
    43                                             and bh.rentdate < to_date('01-01-2013','DD-MM-YYYY')
    44                                             )
    45* order by 1
    SQL> /
            ID REFERENCE     TITLE     DESCRIPTION     AUTHOR     ROOMN DIFFERENT_READERS DAYS_OF_RENT
             1 reference0001 titel0001 description0001 author0001 100.1                 2           15
             2 reference0002 titel0002 description0002 author0002 100.1                 2           15
             3 reference0003 titel0003 description0003 author0003 100.1                 1            8
             4 reference0004 titel0004 description0004 author0004 100.2                 0            0
             5 reference0005 titel0005 description0005 author0005 100.2                 0            0
             6 reference0006 titel0006 description0006 author0006 100.2                 0            0
             7 reference0007 titel0007 description0007 author0007 100.3                 0            0
             8 reference0008 titel0008 description0008 author0008 100.3                 0            0
    8 rows selected.
    SQL>also ensure you treat dates as dates and specify a format mask when using TO_DATE.

  • How to avoid the query using the index?

    select *from cstb_cut ;
    cust_id      cust_name
    10001        xxxx
    10002        yyyy
    10003        zzzz
    Please find the above table and 10000 records are there in that table and also one index was there on cust_id.
    but my query don't want to use the index?how it will be done?

    Hi,
    DEVI suvarna wrote:
    select *from cstb_cut ;
    cust_id      cust_name
    10001        xxxx
    10002        yyyy
    10003        zzzz
    Please find the above table and 10000 records are there in that table and also one index was there on cust_id.
    but my query don't want to use the index?how it will be done?
    The index doesn't help for this query, so it's not used.
    The main purpose of indexes is the help find a small number (like 1) of rows quickly.  You're asking for all rows; getting each one through the index would be slower than just getting all of them directly.
    Sometimes (but not here) the index can be used instead of the table, when the index contains all the data you need from the table.  An index on cust_id doesn't tell you anything about any other column, such as cust_name.  Since you asked to have all columns displayed, it has to fetch data from the table, not the index.

Maybe you are looking for

  • How do I change the location of my songs?

    I've done this in the past, and its worked. But this time, using iTunes 10, it didn't. Has something changed? I had almost 1000 songs on an external drive. Got a new mac with plenty of local disc space, so I copied all the files over to my Music/iTun

  • Script Works in Formcalc but not Javascript

    I have two radio button lists laid out in two columns to select garment sizes (small, medium, large). The "M" side is MEN and the "W" side is WOMEN.  The Radio button lists are named MENS and WOMEN respectively. ITEM    QTY.       COST       ITEM   

  • MIRO take long time when enter invoice for PO with GR-based invoice

    Hi, In my client system, system take long time for extracting PO data while booking invoice via. MIRO for the purchase order which have GR-based IV is marked. However system take few second for extracting PO data when enter invoice via. MIRO for the

  • Using BOm Function modules

    Hi, I have to create/change BOM for multiple plants and i am using below code, but it is not working. I think i am not passing the parameter values properly. Can anyone help me in how to use the below function modules in loop(i mean for multiple plan

  • RSRT Question....

    Hi, This is related to BW 3.5 on Solaris and Oracle. I Go to RSRT and enter the query name then I choose "Technical Information" button. It displays a list of detail information about query. The last information is "Optimization Information" and it h