Regarding SELECT query

Dear experts,
Is there a way for the following SELECT query to be improved?
The problem that I'm seeing here is that the same table (l_item_tab) is being queried twice in the SELECT query (due to the "table joins", a and b). How can I improve the data retrieval here? And I also can't change the SELECT query to be out of the loop - ledger is a select-option and multiple values are possible.
* Get Ledger
  SELECT * FROM t881 INTO TABLE lt_t881
    WHERE rldnr IN s_rldnr.
  LOOP AT lt_t881.
*   Get FI-SL user-defined item table based on ledger
    PERFORM get_sl_item_tab USING lt_t881-rldnr CHANGING l_item_tab.
  " L_ITEM_TAB is populated here
*   Get SL line items
    SELECT * APPENDING CORRESPONDING FIELDS OF TABLE gt_glu1
    FROM (l_item_tab) AS a
    WHERE
        rldnr         IN s_rldnr             " Ledger
    AND rbukrs        IN s_bukrs             " Company code
    AND ryyrkeg_wwsub IN s_wwsub             " Subsystem
    AND racct         IN s_racct             " Account no
    AND ryymac        IN s_yymac             " Management area
    AND rtcur         IN s_rtcur             " Trx currency
    AND docnr         IN s_docnr             " Doc. number
    AND docty         IN s_docty             " Doc. type
    AND docct         EQ c_docct_l           " Doc. category (L = Local)
    AND ryear         IN s_ryear             " Fiscal year
    AND budat         IN s_budat             " Posting date
    AND yystodt       IN s_stodt             " Reversal date
    AND yystgrd       IN s_stgrd             " Reversal reason
    AND yyintref      IN s_intref            " Interface ID
    AND NOT exists
      ( SELECT * FROM (l_item_tab) AS b
         WHERE
             b~rldnr    = a~rldnr     AND
             b~docnr    = a~docnr     AND
             b~rbukrs   = a~rbukrs    AND
       ( ( ( b~docct    = c_docct_y
          OR b~docct    = c_docct_x ) AND
             b~refryear = a~ryear )   OR
         ( ( b~docct    = c_docct_u
          OR b~docct    = c_docct_t ) AND
             b~ryear    = a~ryear ) )
  ENDLOOP.
Edited by: Rob Burbank on Jun 23, 2010 12:33 PM

>
Siegfried Boes wrote:
> > Will certainly try this out too..
> maybe you should think twice .... The usage of a subselect is that the result set is not transferred to the application server it is only needed
> during the selection.
>
> You should anser the following questions:
> + who wrote the code? you or? I get the impression that you don't know what is intended.
> + SQL Trace, what are the numbers for repeated executions, (go to summary by SQL statement), duration, execution, records
> + how many different tables are accessed, l_item_tab is dynamic
> + what knid of tables are accessed?
>
> Siegfried
Hi Siegfried,
- The codes are currently existing ones and they were not written by me too. I just do know that the first / main SELECT statement in the query is for retrieving FI special ledger line item data records, while the second / sub SELECT statement is to ensure that the line item data records are not already reversed, and not a reversal.
- In the summarized SQL trace of a sample test run: executions = 1, identical executions = 0, duration = 700247324 (almost 100% of the total processing durations), records = 0 (there should be more records returned in an actual production run)
- Only one table, ZZGLV4A (custom) is accessed based on the selection screen variant. ZZGLV4A is an FI special ledger line item table and its data volume: 455 mil. data records. Note: The codes are written for a report that runs in the background, and the selection screen variant is used for the executions too. The table accessed (l_item_tab is ZZGLV4A in this case) depends on the ledger inputted here - only one ledger is specified for the current selection screen variant.
- Table accessed - FI special ledger line item data.
I tried tuning the query a little further by just properly specifying the WHERE fields - only a minimal improvement is observed, an average of about 6% of runtime improvement only (tested via SE30 in the development box for ZZGLV4A but its table volume is 4 mil. data records here only). This obviously works only for the table ZZGLV4A for now, I'm afraid.
Any other ideas on how such subqueries can be improved generally (maybe secondary indexes)? The subquery is certainly re-hitting the same table at least twice.
Thanks for the inputs once again!

Similar Messages

  • Regarding select query Issue

    Hi ABAPERS,
          I had written one written one 'select single' query to retrieve the data from PRPS table.
    but what the data was selecting for OBJNR (Object number)field based on PSPNR(WBS element) .
    That OBJNR values in the report is totally different from the database table OBJNR  values.How its possible
    I am not getting.So can anyone please help me regarding this...
    Thanks and Regards
    Deepa

    Hi deepa,
    Pass the values to the select query where clause as you can see the values in SE16 display.(This is external format)
    If a table field as conversion routine, then SAP dosplay some thing on screen and store some thing else in data base.
    To check conversion exit go to domain of that field. Here you can see the routine used.
    For example you can check domain MATNR. Externally it display with leading zeros. But internally it stores with out zeros.
    Thanks,
    Vinod.

  • Regarding "select query" on performance basis..

    Hi Experts,
    I am creating an ALV report,so that i have to collect all data in 1 internal table.
    I have to collect data from 3 standard tables i.e.T157E,MSEG,MKPF.
    For all entries of T157E, i have to fetch data from MSEG.
    But in where condition i have to give POSTING DATE n this field is in MKPF.
    Also Database performance analysis is around 98%.
    How can i give better performance, please help..
    Below is my coding...
    (second select query is taking too much time,how can i rectify)
    SELECT BWART
                 GRUND
        FROM T157D
        INTO CORRESPONDING FIELDS OF TABLE IT_MOV_TYPE.
    SELECT MATNR
             MJAHR
             MBLNR
             BWART
             WERKS
             LGORT
             CHARG
             GRUND
             MENGE
             MEINS
        FROM MSEG
        INTO CORRESPONDING FIELDS OF TABLE IT_MSEG
        FOR ALL ENTRIES IN IT_MOV_TYPE
       WHERE GRUND = IT_MOV_TYPE-GRUND
         AND BWART = IT_MOV_TYPE-BWART
         AND WERKS = PLANT
         AND LGORT IN ST_L.
      SORT IT_MSEG BY MBLNR.
      LOOP AT IT_MSEG.
        AT NEW MBLNR.
          SELECT SINGLE BUDAT
                   FROM MKPF
                   INTO V_BUDAT
                  WHERE MBLNR = IT_MSEG-MBLNR
                    AND MJAHR = IT_MSEG-MJAHR.
          SELECT SINGLE MAKTX
                   FROM MAKT
                   INTO V_MAKTX
                  WHERE MATNR = IT_MSEG-MATNR.
          SELECT SINGLE GRTXT
                   FROM T157E
                   INTO V_GRTXT
                  WHERE GRUND = IT_MSEG-GRUND.
        ENDAT.
        IT_MSEG-BUDAT = V_BUDAT.
        IT_MSEG-MAKTX = V_MAKTX.
        IT_MSEG-GRTXT = V_GRTXT.
        MODIFY IT_MSEG.
      ENDLOOP.
    Regards,
    Ankur

    Before you use
    FOR ALL ENTRIES IN
    Do the following :
    1. Sort the Internal table used in the 'FOR ALL ENTRIES IN' clause.
    2. Make sure they do not contain duplicates.
    3. Last and most importantly include a primary key field(Unique entry) in the select list. The reason, the system gets unique field records based on the where condition. It summarizes the data based on the Data type and field list in the target internal table.
    One should be sure the internal table, used in the FOR ALL ENTRIES clause is not empty. If the table is empty, ALL ENTRIES will be selected. (Actually, all WHERE clauses are neglected). Of course, you should also be sure you select on INDEX fields. Otherwise the use of FOR ALL ENTRIES increases the runtime instead of improving it.

  • Need clarification regarding select query

    Hi,
    I need a little clarification regrding a Select senario
    I want to select data from table which have been minupulated between a certian date like between 1-DEC-10 to 31-DEC-10 and note that table does not have any time/date column. I've applied the following query to do this.
    select * from TABLE_NAME where sysdate between to_date('01-DEC-10') AND to_date('31-DEC-10');
    Would it work fine because I've tried it against a table and it returned me nothing however DML occur between time period.
    Regards,
    Abbasi

    Abbasi wrote:
    Hi,
    I need a little clarification regrding a Select senario
    I want to select data from table which have been minupulated between a certian date like between 1-DEC-10 to 31-DEC-10 and note that table does not have any time/date column. I've applied the following query to do this.
    select * from TABLE_NAME where sysdate between to_date('01-DEC-10') AND to_date('31-DEC-10');
    Would it work fine because I've tried it against a table and it returned me nothing however DML occur between time period.
    Regards,
    AbbasiAFAIK without log mining and auditing this is not possible.
    http://download.oracle.com/docs/cd/B19306_01/server.102/b14215/logminer.htm

  • Regarding select query with code

    Hi Experts ,
    i m taking two transparent table "VBAK , VBAP" WITH CLUSTER TABLE "KONV"
    In this Table we create inner join between VBAK ,ABAP  to itab and make  itab2 with some field of KONV.
    ===========================================================
    *& Report  Z_VBAP_REPORT                                               *
    REPORT  Z_VBAP_REPORT    .
    TABLES:VBAP,VBAK,KONV.
    DATA KNUMV LIKE VBAK-KNUMV.
    DATA:BEGIN OF ITAB OCCURS 0,
         VBELN LIKE VBAK-VBELN,
         POSNR LIKE VBAP-POSNR,
         MATNR LIKE VBAP-MATNR,
         NETPR LIKE VBAP-NETPR,
         BNAME LIKE VBAK-BNAME,
         ERDAT LIKE VBAK-ERDAT,
         KBETR LIKE KONV-KBETR,
         KNTYP LIKE KONV-KNTYP,
         KNUMV LIKE VBAK-KNUMV,
         END OF ITAB.
    DATA ITAB2 LIKE ITAB OCCURS 0 WITH HEADER LINE.
    PARAMETER SALE_DOC LIKE VBAK-VBELN.
    SELECT-OPTIONS D_DATE FOR VBAK-ERDAT.
    SELECT BVBELN APOSNR AMATNR ANETPR BBNAME BERDAT INTO
    CORRESPONDING FIELDS OF TABLE ITAB FROM VBAP AS A INNER JOIN VBAK
    AS B ON ( BVBELN = AVBELN ) WHERE B~ERDAT IN D_DATE AND
    A~VBELN =  SALE_DOC.
    SELECT  KBETR KNTYP  FROM KONV INTO CORRESPONDING FIELDS OF TABLE ITAB2
    WHERE KNUMV = KNUMV .
    EXPORT ITAB TO MEMORY ID 'ASHISH'.
    EXPORT ITAB2 TO MEMORY ID 'ASHISH1'.
    SUBMIT Z_ABAP2 VIA SELECTION-SCREEN AND RETURN.
    ///////Code for report Z_ABAP2
    *& Report  Z_ABAP2                                                     *
    REPORT  Z_ABAP2   .
    TABLES:VBAP,VBAK,KONV.
    DATA:BEGIN OF ITAB OCCURS 0,
         VBELN LIKE VBAK-VBELN,
         POSNR LIKE VBAP-POSNR,
         MATNR LIKE VBAP-MATNR,
         NETPR LIKE VBAP-NETPR,
         BNAME LIKE VBAK-BNAME,
         ERDAT LIKE VBAK-ERDAT,
         KBETR LIKE KONV-KBETR,
         KNTYP LIKE KONV-KNTYP,
         KNUMV LIKE VBAK-KNUMV,
         END OF ITAB.
    DATA ITAB2 LIKE ITAB OCCURS 0 WITH HEADER LINE.
    LOOP AT ITAB.
    WRITE : / ITAB-VBELN,
               ITAB-POSNR, 25
               ITAB-MATNR,
               ITAB-NETPR,
               ITAB-BNAME,
               ITAB-ERDAT,
               ITAB-KBETR,
               ITAB-KNTYP.
    ENDLOOP.
    LOOP AT ITAB2.
    WRITE :/   ITAB2-KBETR,
               ITAB2-KNTYP.
               ENDLOOP.
    INITIALIZATION .
      IMPORT ITAB FROM MEMORY ID 'ASHISH'.
      IMPORT ITAB2 FROM MEMORY ID 'ASHISH1'.
    =========================================================
    thanks & regards ,
    Ashish

    Hi
    after the first select use for all entries of this ITAB and fetch data from KONV as below, declare I_FINAL and move the ITAB and ITAB2 data into it and display its output
    SELECT BVBELN APOSNR AMATNR ANETPR BBNAME BERDAT <b>B~KNUMV</b> INTO
    CORRESPONDING FIELDS OF TABLE ITAB FROM VBAP AS A INNER JOIN VBAK
    AS B ON ( BVBELN = AVBELN ) WHERE B~ERDAT IN D_DATE AND
    A~VBELN = SALE_DOC.
    <b>IF NOT ITAB[] IS INITIAL.
    SELECT KNUMV KPOSN KBETR KNTYP FROM KONV INTO CORRESPONDING FIELDS OF TABLE ITAB2 for all entries in ITAB
    WHERE KNUMV = ITAB-KNUMV and KPOSN = ITAB-POSNR .</b>
    Loop at itab.
      move-corresponding itab to i_final.
      read table itab2 with key knumv = itab-knumv and kposn = itab-posnr.
      if sy-subrc  = 0.
        move-corresponding itab2 to i_final.
      endif.
    append i_final.
    clear i_final.
    endloop.
    Loop at i_final.
       write: / I_final (write all the fields)
    endloop.
    <b>Reward points for useful Answers</b>
    Regards
    Anji

  • Issue regarding Select Query

    Hello experts,
    I want a join on table ekpo & mbew(for valuation class).
    But its not working.
    if valclass is initial.
    select  aebeln aebelp atxz01 amatnr abukrs awerks a~menge
    a~mtart
    b~bklas
    from  ekpo  as a inner join  mbew as b on bmatnr eq amatnr
    into corresponding fields of table ekpo_mbew
    where  ( a~matnr eq mat_num ) and
    ( a~bukrs eq compcode ) and
    ( a~werks eq plant ) and
    ( a~mtart eq mat_type ) and
    b~bklas  in ('3001','3045' ,'3006','3055','3065') and
    ( amatnr eq bmatnr ).
    else.
    select  aebeln aebelp atxz01 amatnr abukrs awerks a~menge
    a~mtart
    b~bklas
    from  ekpo  as a inner join  mbew as b on bmatnr eq amatnr
    into corresponding fields of table ekpo_mbew
    where  ( a~matnr eq mat_num ) and
    ( a~bukrs eq compcode ) and
    ( a~werks eq plant ) and
    ( a~mtart eq mat_type ) and
    ( b~bklas  in valclass ) and
    ( amatnr eq bmatnr ).
    endif.
    Plz tell where I m wrng.
    Ravi.

    Hi,
         Try the below code,
    SELECT aebeln aebelp atxz01 amatnr abukrs awerks a~menge
    a~mtart
    b~bklas
    FROM ekpo AS a INNER JOIN mbew AS b ON bmatnr EQ amatnr INTO CORRESPONDING FIELDS OF TABLE ekpo_mbew
    WHERE  a~matnr EQ mat_num AND
                   a~bukrs EQ compcode AND
                   a~werks EQ plant AND
    a~mtart EQ mat_type AND
    b~bklas IN ('3001','3045' ,'3006','3055','3065').
    ELSE.
    SELECT aebeln aebelp atxz01 amatnr abukrs awerks a~menge
    a~mtart
    b~bklas
    FROM ekpo AS a INNER JOIN mbew AS b
    ON bmatnr EQ amatnr
    INTO CORRESPONDING FIELDS OF TABLE ekpo_mbew
    WHERE  a~matnr EQ mat_num AND
    a~bukrs EQ compcode  AND
    a~werks EQ plant AND
    a~mtart eq mat_type AND
    b~bklas IN valclass .
    Note :- ( amatnr eq bmatnr ) this is not necessary as the INNER JOIN is on that condition itself.
    Regards
    Bala Krishna

  • Regarding Select query to select only 1000 records in Internal Table.

    Hello Friends,
    Please explain me to how to Select only 1000 records from data base table?
    For Example
    SELECT *  INTO TABLE ITAB                          OR            SELECT * INTO ITAB 
         FROM EKKO                                                                  FROM EKKO
    WHERE EBLEN IN S_EBLEN.                                            WHERE EBLEN IN S_EBLEN
    (Currently i am using)                                                       UP TO 1000 ROWS.
                                                                                    ENDSELECT.  (I do not want to use)
                                                                                    In this case Internal table may be store more then 1000 records. But i want to select first 1000 records.
    Please explain me.
    Regards
    Amit

    Hi,
    TABLES : ekko.
    selection-screen begin of block b1 with frame title text_t01.
    SELECT-OPTIONS: S_EBELN for ekko-EBELN.
    selection-screen end of block b1.
    DATA itab TYPE STANDARD TABLE OF ekko.
    SELECT *
      FROM ekko
      INTO TABLE itab
      UP TO 1000 ROWS
      WHERE EBELN IN S_EBELN.
    Thanks,
    Sri.

  • Regarding nor of line items selected in per select query..

    hi,
    i want  to display nor of line items selected in per select query for two individual  queries which are linkesd by for all entries..
    regards..
    chetan

    Hi,
    I dont unserstand the problem you are facing. please paste your SELECT query and then explain the problem. then it will be easy for analysis.
    Basically DESCRIBE TABLE itab LINES n.  this statement will put the number of lines the internal table contains
    //Kothand

  • Regarding the select query

    Hi Experts,
    I have a select query as follows:
    select field1 field2  from db1 into table it1.
    Now suppose the it1-field1 contains values a,b and c.
    Now I want to select fields a,b and c from another db table db2.
    Instead of hard coding like this:
    select a b c from db2 into table it2.....
    How can I do this?
    Points will be awarded.
    Regards,
    Soumya.

    Hi Soumya ,
    Pleas use FOR ALL ENTRIES syntax , in this way you can select the records from db2 which are available or selected in db1.
    Like Header And Item table.
    You select records frm header table . then for corres ponding header records you select from detail table db2 .....
    Condition is the field containg a,b,c  is key field in both tables ...
    Reward points if it helps you .
    Regards
    Amber S

  • Basic query regarding work-area and select query

    hi
    dear sdn members,
    thanks too all for solving all my query's up till now
    i am stuck in a problem need help
    1)  why basically work-area has been used ? the sole purpose
    2)  different types of select query ? only coding examples
    note: no links pls
    regards,
    virus

    hi,
    Work Area
    Description for a data object that is particularly useful when working with internal tables or database tables as a source for changing operations or a target for reading operations.
    WORKAREA is a structure that can hold only one record at a time. It is a collection of fields. We use workarea as we cannot directly read from a table. In order to interact with a table we need workarea. When a Select Statement is executed on a table then the first record is read and put into the header of the table and from there put into the header or the workarea(of the same structure as that of the table)of the internal table and then transferred top the body of the internal table or directly displayed from the workarea.
    Each row in a table is a record and each column is a field.
    While adding or retrieving records to / from internal table we have to keep the record temporarily.
    The area where this record is kept is called as work area for the internal table. The area must have the same structure as that of internal table. An internal table consists of a body and an optional header line.
    Header line is a implicit work area for the internal table. It depends on how the internal table is declared that the itab will have the header line or not.
    .g.
    data: begin of itab occurs 10,
    ab type c,
    cd type i,
    end of itab. " this table will have the header line.
    data: wa_itab like itab. " explicit work area for itab
    data: itab1 like itab occurs 10. " table is without header line.
    The header line is a field string with the same structure as a row of the body, but it can only hold a single row.
    It is a buffer used to hold each record before it is added or each record as it is retrieved from the internal table. It is the default work area for the internal table.
    With header line
    SELECT.
    Put the curson on that word and press F1 . You can see the whole documentation for select statements.
    select statements :
    SELECT result
    FROM source
    INTO|APPENDING target
    [[FOR ALL ENTRIES IN itab] WHERE sql_cond]
    Effect
    SELECT is an Open-SQL-statement for reading data from one or several database tables into data objects.
    The select statement reads a result set (whose structure is determined in result ) from the database tables specified in source, and assigns the data from the result set to the data objects specified in target. You can restrict the result set using the WHERE addition. The addition GROUP BY compresses several database rows into a single row of the result set. The addition HAVING restricts the compressed rows. The addition ORDER BY sorts the result set.
    The data objects specified in target must match the result set result. This means that the result set is either assigned to the data objects in one step, or by row, or by packets of rows. In the second and third case, the SELECT statement opens a loop, which which must be closed using ENDSELECT. For every loop pass, the SELECT-statement assigns a row or a packet of rows to the data objects specified in target. If the last row was assigned or if the result set is empty, then SELECT branches to ENDSELECT . A database cursor is opened implicitly to process a SELECT-loop, and is closed again when the loop is ended. You can end the loop using the statements from section leave loops.
    Up to the INTO resp. APPENDING addition, the entries in the SELECTstatement define which data should be read by the database in which form. This requirement is translated in the database interface for the database system´s programming interface and is then passed to the database system. The data are read in packets by the database and are transported to the application server by the database server. On the application server, the data are transferred to the ABAP program´s data objects in accordance with the data specified in the INTO and APPENDING additions.
    System Fields
    The SELECT statement sets the values of the system fields sy-subrc and sy-dbcnt.
    sy-subrc Relevance
    0 The SELECT statement sets sy-subrc to 0 for every pass by value to an ABAP data object. The ENDSELECT statement sets sy-subrc to 0 if at least one row was transferred in the SELECT loop.
    4 The SELECT statement sets sy-subrc to 4 if the result set is empty, that is, if no data was found in the database.
    8 The SELECT statement sets sy-subrc to 8 if the FOR UPDATE addition is used in result, without the primary key being specified fully after WHERE.
    After every value that is transferred to an ABAP data object, the SELECT statement sets sy-dbcnt to the number of rows that were transferred. If the result set is empty, sy-dbcnt is set to 0.
    Notes
    Outside classes, you do not need to specify the target area with INTO or APPENDING if a single database table or a single view is specified statically after FROM, and a table work area dbtab was declared with the TABLES statement for the corresponding database table or view. In this case, the system supplements the SELECT-statement implicitly with the addition INTO dbtab.
    Although the WHERE-condition is optional, you should always specify it for performance reasons, and the result set should not be restricted on the application server.
    SELECT-loops can be nested. For performance reasons, you should check whether a join or a sub-query would be more effective.
    Within a SELECT-loop you cannot execute any statements that lead to a database commit and consequently cause the corresponding database cursor to close.
    SELECT - result
    Syntax
    ... lines columns ... .
    Effect
    The data in result defines whether the resulting set consists of multiple rows (table-like structure) or a single row ( flat structure). It specifies the columns to be read and defines their names in the resulting set. Note that column names from the database table can be changed. For single columns, aggregate expressions can be used to specify aggregates. Identical rows in the resulting set can be excluded, and individual rows can be protected from parallel changes by another program.
    The data in result consists of data for the rows lines and for the columns columns.
    SELECT - lines
    Syntax
    ... { SINGLE }
    | { { } } ... .
    Alternatives:
    1. ... SINGLE
    2. ... { }
    Effect
    The data in lines specifies that the resulting set has either multiple lines or a single line.
    Alternative 1
    ... SINGLE
    Effect
    If SINGLE is specified, the resulting set has a single line. If the remaining additions to the SELECT command select more than one line from the database, the first line that is found is entered into the resulting set. The data objects specified after INTO may not be internal tables, and the APPENDING addition may not be used.
    An exclusive lock can be set for this line using the FOR UPDATE addition when a single line is being read with SINGLE. The SELECT command is used in this case only if all primary key fields in logical expressions linked by AND are checked to make sure they are the same in the WHERE condition. Otherwise, the resulting set is empty and sy-subrc is set to 8. If the lock causes a deadlock, an exception occurs. If the FOR UPDATE addition is used, the SELECT command circumvents SAP buffering.
    Note
    When SINGLE is being specified, the lines to be read should be clearly specified in the WHERE condition, for the sake of efficiency. When the data is read from a database table, the system does this by specifying comparison values for the primary key.
    Alternative 2
    Effect
    If SINGLE is not specified and if columns does not contain only aggregate expressions, the resulting set has multiple lines. All database lines that are selected by the remaining additions of the SELECT command are included in the resulting list. If the ORDER BY addition is not used, the order of the lines in the resulting list is not defined and, if the same SELECT command is executed multiple times, the order may be different each time. A data object specified after INTO can be an internal table and the APPENDING addition can be used. If no internal table is specified after INTO or APPENDING, the SELECT command triggers a loop that has to be closed using ENDSELECT.
    If multiple lines are read without SINGLE, the DISTINCT addition can be used to exclude duplicate lines from the resulting list. If DISTINCT is used, the SELECT command circumvents SAP buffering. DISTINCT cannot be used in the following situations:
    If a column specified in columns has the type STRING, RAWSTRING, LCHAR or LRAW
    If the system tries to access pool or cluster tables and single columns are specified in columns.
    Note
    When specifying DISTINCT, note that you have to carry out sort operations in the database system for this.
    SELECT - columns
    Syntax
    | { {col1|aggregate( col1 )}
    {col2|aggregate( col2 )} ... }
    | (column_syntax) ... .
    Alternatives:
    1. ... *
    2. ... {col1|aggregate( col1 )}
    {col2|aggregate( col2 )} ...
    3. ... (column_syntax)
    Effect
    The input in columns determines which columns are used to build the resulting set.
    Alternative 1
    Effect
    If * is specified, the resulting set is built based on all columns in the database tables or views specified after FROM, in the order given there. The columns in the resulting set take on the name and data type from the database tables or views. Only one data object can be specified after INTO.
    Note
    If multiple database tables are specified after FROM, you cannot prevent multiple columns from getting the same name when you specify *.
    Alternative 2
    ... {col1|aggregate( col1 )}
    {col2|aggregate( col2 )} ...
    Effect
    A list of column labels col1 col2 ... is specified in order to build the resulting list from individual columns. An individual column can be specified directly or as an argument of an aggregate function aggregate. The order in which the column labels are specified is up to you and defines the order of the columns in the resulting list. Only if a column of the type LCHAR or LRAW is listed does the corresponding length field also have to be specified directly before it. An individual column can be specified multiple times.
    The addition AS can be used to define an alternative column name a1 a2 ... with a maximum of fourteen digits in the resulting set for every column label col1 col2 .... The system uses the alternative column name in the additions INTO|APPENDING CORRESPONDING FIELDS and ORDER BY. .
    Column labels
    The following column labels are possible:
    If only a single database table or a single view is specified after FROM, the column labels in the database table - that is, the names of the components comp1 comp2... - can be specified directly for col1 col2 ... in the structure of the ABAP Dictionary.
    If the name of the component occurs in multiple database tables of the FROM addition, but the desired database table or the view dbtab is only specified once after FROM, the names dbtab~comp1 dbtab~comp2 ... have to be specified for col1 col2 .... comp1 comp2 ... are the names of the components in the structure of the ABAP Dictionary.
    If the desired database table or view occurs multiple times after FROM, the names tabalias~comp1 tabalias~comp2 ... have to be specified for col1 col2 .... tabalias is the alternative table name of the database table or view defined after FROM, and comp1 comp2 ... are the names of the components in the structure of the ABAP Dictionary.
    The data type of a single column in the resulting list is the datatype of the corresponding component in the ABAP Dictionary. The corresponding data object after INTO or APPENDING has to be selected accordingly.
    Note
    If multiple database tables are specified after FROM, you can use alternative names when specifying single columns to avoid having multiple columns with the same name.
    Example
    Read specific columns of a single row.
    DATA wa TYPE spfli.
    SELECT SINGLE carrid connid cityfrom cityto
    INTO CORRESPONDING FIELDS OF wa
    FROM spfli
    WHERE carrid EQ 'LH' AND connid EQ '0400'.
    IF sy-subrc EQ 0.
    WRITE: / wa-carrid, wa-connid, wa-cityfrom, wa-cityto.
    ENDIF.
    Alternative 3
    ... (column_syntax)
    Effect
    Instead of static data, a data object column_syntax in brackets can be specified, which, when the command is executed, either contains the syntax shown with the static data, or is initial. The data object column_syntax can be a character-type data object or an internal table with a character-type data type. The syntax in column_syntax, like in the ABAP editor, is not case-sensitive. When specifying an internal table, you can distribute the syntax over multiple rows.
    If column_syntax is initial when the command is executed, columns is implicitly set to * and all columns are read.
    If columns are specificied dynamically without the SINGLE addition, the resulting set is always regarded as having multiple rows.
    Notes
    Before Release 6.10, you could only specify an internal table with a flat character-type row type for column_syntax with a maximum of 72 characters. Also, before Release 6.10, if you used the DISTINCT addition for dynamic access to pool tables or cluster tables, this was ignored, but since release 6.10, this causes a known exception.
    If column_syntax is an internal table with header line, the table body and not the header line is evaluated.
    Example
    Read out how many flights go to and from a city. The SELECT command is implemented only once in a sub-program. The column data, including aggregate function and the data after GROUP BY, is dynamic. Instead of adding the column data to an internal l_columns table, you could just as easily concatenate it in a character-type l_columns field.
    PERFORM my_select USING `CITYFROM`.
    ULINE.
    PERFORM my_select USING `CITYTO`.
    FORM my_select USING l_group TYPE string.
    DATA: l_columns TYPE TABLE OF string,
    l_container TYPE string,
    l_count TYPE i.
    APPEND l_group TO l_columns.
    APPEND `count( * )` TO l_columns.
    SELECT (l_columns)
    FROM spfli
    INTO (l_container, l_count)
    GROUP BY (l_group).
    WRITE: / l_count, l_container.
    ENDSELECT.
    ENDFORM.
    SELECT - aggregate
    Syntax
    ... { MAX( col )
    | MIN( col )
    | AVG( col )
    | SUM( col )
    | COUNT( DISTINCT col )
    | COUNT( * )
    | count(*) } ... .
    Effect
    As many of the specified column labels as you like can be listed in the SELECT command as arguments of the above aggregate expression. In aggregate expressions, a single value is calculated from the values of multiple rows in a column as follows (note that the addition DISTINCT excludes double values from the calculation):
    MAX( col ) Determines the maximum value of the value in the column col in the resulting set or in the current group.
    MIN( col ) Determines the minimum value of the content of the column col in the resulting set or in the current group.
    AVG( col ) Determines the average value of the content of the column col in the resulting set or in the current group. The data type of the column has to be numerical.
    SUM( col ) Determines the sum of the content of the column col in the resulting set or in the current group. The data type of the column has to be numerical.
    COUNT( DISTINCT col ) Determines the number of different values in the column col in the resulting set or in the current group.
    COUNT( * ) (or count(*)) Determines the number of rows in the resulting set or in the current group. No column label is specified in this case.
    If you are using aggregate expressions, all column labels that are not listed as an argument of an aggregate function are listed after the addition GROUP BY. The aggregate functions evaluate the content of the groups defined by GROUP BY in the database system and transfer the result to the combined rows of the resulting set.
    The data type of aggregate expressions with the function MAX, MIN or SUM is the data type of the corresponding column in the ABAP Dictionary. Aggregate expressions with the function AVG have the data type FLTP, and those with COUNT have the data type INT4. The corresponding data object after INTO or APPENDING has to be selected accordingly.
    Note the following points when using aggregate expressions:
    If the addition FOR ALL ENTRIES is used in front of WHERE, or if cluster or pool tables are listed after FROM, no other aggregate expressions apart from COUNT( * ) can be used.
    Columns of the type STRING or RAWSTRING cannot be used with aggregate functions.
    When aggregate expressions are used, the SELECT command makes it unnecessary to use SAP buffering.
    Null values are not included in the calculation for the aggregate functions. The result is a null value only if all the rows in the column in question contain the null value.
    If only aggregate expressions are used after SELECT, the results set has one row and the addition GROUP BY is not necessary. If a non-table type target area is specified after INTO, the command ENDSELECT cannot be used together with the addition SINGLE. If the aggregate expression count( * ) is not being used, an internal table can be specified after INTO, and the first row of this table is filled.
    If aggregate functions are used without GROUP BY being specified at the same time, the resulting set also contains a row if no data is found in the database. If count( * ) is used, the column in question contains the value 0. The columns in the other aggregate functions contain initial values. This row is assigned to the data object specified after INTO, and unless count( * ) is being used exclusively, sy-subrc is set to 0 and sy-dbcnt is set to 1. If count( *) is used exclusively, the addition INTO can be omitted and if no data can be found in the database, sy-subrc is set to 4 and sy-dbcnt is set to 0.
    if helpful reward points

  • Regarding  dynamically assigning the where clause to select query

    hi,
      Please send the code regarding how to dynamically assign the where clause to select query.
    thanks in advance

    SELECT <fileds>
            INTO TABLE itab
            FROM dbase
            WHERE  condition.

  • Regarding performance in select query

    hi to all,
    i wrote select query for fetching duplicate records,like select distinct *.
    so at runtime it fetches all fields in table (FI dbtable).
    but i want to fetch only 8 records,for this i am using
    select distinct
              field1
               field2
    ..... like this ,it wasnt excuted.
    how can i avoid this issue.
    in program not much logic is there,only fetching the data and summation,n display.....
    plz giv me reply asap.
    regards
    satya

    satya wrote:>
    > hi to all,

    >  i wrote select query for fetching duplicate records,like select distinct *.
    > so at runtime it fetches all fields in table (FI dbtable).
    >
    > but i want to fetch only 8 records,for this i am using
    > select distinct
    >           field1
    >            field2
    > ..... like this ,it wasnt excuted.
    >
    > how can i avoid this issue.
    > in program not much logic is there,only fetching the data and summation,n display.....
    >
    > plz giv me reply asap.
    >
    > regards
    > satya
    Hi,
    write like this
    > select field1
    >           field2
    >          .......
    >   from  table
    >   into ( itab-f1,
    >            itab-f2,
    >            ........ )
    <REMOVED BY MODERATOR>
    Edited by: Alvaro Tejada Galindo on Feb 19, 2008 5:14 PM

  • Regarding Data Base Select Query

    Hi All,
              I want to extract the data from data base into my internal table
              by not giving the exact value but the pattern.
              Ex) I want to select the vendor starting with patter '12*'
                    from LFA table in my internal table.
              Which select Query I should use for getting the
              Vendor No. starting with pattern '12*' ?.
              Points will be rewaded.
              Thanks in Advance.
    With Regards
    Jitendra Gujarathi.

    Hi jitendra,
    1. simple
    2.
    select * from lfa1
    into table lfa1
    where lifnr  like '12%'.
    regards,
    amit m .

  • Regarding "select into" query and "no data found" exception

    So i have included the following into my procedure:
    select div_cd into c_div_cd
                   from division_tab d, emp_tab y
                   where d.div_name=y.div_text and y.emp_code=d.emp_code;
    and also an exception
    exception
    when no data found
    -- print something
    The above select query results into "no data found" and the control passes directly to the exception and prints something.
    How do I do the following?
    select div_cd into c_div_cd
                   from division_tab d, emp_tab y
                   where d.div_name=y.div_text and y.emp_code=d.emp_code;
    if c_div_cd is null then
    --enter this employee into some other table without raising an exception
    No need to write a code for an answer. Please just guide me with something I can incorporate or do.

    use explicit cursors
    DECLARE
    c_div_cd division_tab.div_cd%type;
    cursor c_div is
    select div_cd
    from division_tab d, emp_tab y
    where d.div_name=y.div_text and y.emp_code=d.emp_code;
    BEGIN
    open c_div;
    fetch c_div into c_div_cd;
    --You can either use c_div%NOTFOUND or c_div_cd is null in the below if condition to check the data
    -- Note if your select query returns multiple records then you have to do mutiple fetches for getting all records, so in that case your FETCH
    -- should be inside the LOOP statement
    if c_div_cd is null then
    --enter this employee into some other table without raising an exception
    end if;
    close c_div;
    EXCEPTION
    IF c_div%ISOPEN then
    close c_div;
    END IF;
    END;
    Regards
    JJ

  • Query regarding selection screen

    Hi experts,
    I have a requirement that once I get my output on a selection screen, suppose it has just 1 row and 4 columns. If I click on each of these 4 columns, a new transaction/report etc. needs to be called. How can I achieve this functionality?
    Thanks,
    Ajay.

    Hi,
    Say you have an internal table with fields as:
    VBELN (sales order)
    POSNR (sales order line item)
    WERKS (plant)
    MATNR (matnr)
    And you write all these fields on output screen using reporting and when you click on any column field then a particular transaction/report should be called, then follow the below mentioned logic:-
    DATA : BEGIN OF it_final OCCURS 0,
             vbeln TYPE vbap-vbeln,
             posnr TYPE vbap-posnr,
             werks TYPE vbap-werks,
             matnr TYPE vbap-matnr,
           END OF it_final.
    DATA : fldname(25) TYPE c,
           fldvalue(25) TYPE c.
    START-OF-SELECTION.
      "select query into it_final internal table
    AT LINE-SELECTION.
      GET CURSOR FIELD fldname VALUE fldvalue.
      CASE fldname.
        WHEN 'IT_FINAL-VBELN'.
          WRITE : / fldvalue.
          "code to call transaction/report
        WHEN 'IT_FINAL-POSNR'.
          WRITE : / fldvalue.
          "code to call transaction/report
        WHEN 'IT_FINAL-WERKS'.
          WRITE : / fldvalue.
          "code to call transaction/report
        WHEN 'IT_FINAL-MATNR'.
          WRITE : / fldvalue.
          "code to call transaction/report
      ENDCASE.
    END-OF-SELECTION.
      ULINE.
      FORMAT HOTSPOT.
      LOOP AT it_final.
        WRITE : /1 it_final-vbeln, 10 it_final-posnr, 20 it_final-werks, 30 it_final-matnr.
        HIDE : it_final-vbeln, it_final-posnr, it_final-werks, it_final-matnr.
      ENDLOOP.
      ULINE.
    Now to call a transaction user:-
    SET PARAMETER ID '<param_id>' FIELD '<field_value>'.
    CALL TRANSACTION '<t_code>' AND SKIP FIRST SCREEN.
    To call a report use:-
    SUBMIT <report_name> WITH <parameter_name1> = <field_value1>
                         WITH <parameter_name2> = <field_value2>
                         "and so on use parameters
                         AND RETURN.
    <paramater_name> refer to the parameter of the called report <report_name>
    and <field_value> refer to value for the parameter to be passed.
    Hope this helps you.
    Regards,
    Tarun

Maybe you are looking for