Optimising SELECT from a database

I'm trying to retrieve records from a huge transaction table.
1. I want to retrieve records into an internal table
2. If there was a duplicate record that is about to move into the same internal table it will be moved into another internal table.
3. Someone told me that using a LOOP may not be that effective. Using internal table KEYS and BINARY SEARCH would be ideal.
I have come here to seek for urgent guidance and knowledge.
Hope you all can help.

Hi Andrew,
do u want to select the records from the databese... then move the duplicate record in to another internal table.. is this u requirement!!
first select the data into let us say Ex IT_TAB1.
suppose F1 and F2 Are key fields in u r table.
then
SORT IT_TAB1 BY F1 F2.
THEN
IT_TAB2[] = IT_TAB1[].
SORT IT_TAB2 BY F1 F2.
DELETE ADJACENT DUPLICATE FROM IT_TAB2 COMPLARING F1 F2..
now u have two tables IT_TAB1 contains all the Records
IT_TAB2 contains only the single records..

Similar Messages

  • Selection from logical database.

    The question is simple. =) Wy does the second part works but not the first one. Isn't it possible to do like that. Anyone that can help with how i can do? 
    <b>Event : start-of-selection employees not connected to position</b>
    rp-provide-from-last p0001 space pn-begda pn-endda.
      check pnp-sw-found eq 1.
    if p0001-plans EQ '99999999'
      AND p0000-stat2 EQ '3'
      AND p0001-vdsk1 EQ 'IT'
      AND ( p0001-persg EQ '1' or
          p0001-persg EQ 'T' )
      AND ( p0001-bukrs EQ 'FR46' or
          p0001-bukrs EQ 'GB21' or
          p0001-bukrs EQ 'PL05' or
          p0001-bukrs EQ 'BE06' or
          p0001-bukrs EQ 'US22' ).
    <i>****** What should i write here.
    All the condistion are right
    But i dont know how/what to select.</i>
      if sy-subrc ne 0.
          ipos-pernr = pernr-pernr.
          ipos-sname = p0001-sname.
          ipos-orgeh = p0001-orgeh.
          ipos-bukrs = p0001-bukrs.
          ipos-plans = p0001-plans.
          append ipos.
          clear: ipos.
      endif.
      endif.
    Event : <b>start-of-selection connected to position but no task</b>************************************************************************
      rp-provide-from-last p0001 space pn-begda pn-endda.
      check pnp-sw-found eq 1.
          IF p0001-plans NE '99999999'
    AND p0000-stat2 EQ '3'
    AND p0001-vdsk1 EQ 'IT'
    AND ( p0001-persg EQ '1' OR
    p0001-persg EQ 'T' )
    AND  ( p0001-bukrs EQ 'FR46' OR
    p0001-bukrs EQ 'GB21' OR
    p0001-bukrs EQ 'PL05' OR
    p0001-bukrs EQ 'BE06' OR
    p0001-bukrs EQ 'US22' ).
      <i>  w_objid = p0001-plans.
        select sobid into w_sobid
                      up to 1 rows
                      from hrp1001
                      where otype = 'S'
                         and plvar = '01'
                         and objid = w_objid
                         and rsign = 'B'
                         and relat = '007'
                         and endda ge pn-begda
                         and begda le pn-endda.
        endselect.</i>
        if sy-subrc ne 0.
          itask-pernr = pernr-pernr.
          itask-sname = p0001-sname.
          itask-orgeh = p0001-orgeh.
          itask-bukrs = p0001-bukrs.
          itask-plans = p0001-plans.
          append itask.
          clear: itask, w_objid, w_sobid.
          endif.
         endif.
    end-of-selection.
    / Claes

    What should i write here.
    All the condistion are right
    But i dont know how/what to select.
    <b>Should be Replaced by</b>
    Function call RH_GET_POSITION_TAB. Then you'll get the list if positions!
    Is that what you ask ????
    Stphn

  • Am unable to get the data from Sys.databases when i use where condition for column owner_sid

     
    Hi all,
    Help me here when i try am unable to get a data,Can some one help me with a query to get.
    If it cant be done can i know why..
     select * from Sys.databases
      where owner_sid='0x01'
    What am trying is to get a login
    names from syslogin
    table with respect to Sid.
    Select a.name,b.name,a.owner_sid from Sys.databases a
      Join Syslogins b
      on a.owner_sid = b.sid
      where owner_sid like '0x01'
    Thanks all in Advance.....

    Below are a couple of examples of how to accomplish the task.  Note that SID is varbinary so the literal should not be enclosed in quotes.
    SELECT
    a.name AS DatabaseName
    ,b.name AS OwnerName
    ,a.owner_sid AS OwnerSID
    FROM sys.databases a
    JOIN sys.server_principals b ON
    a.owner_sid = b.sid
    WHERE owner_sid = 0x01;
    SELECT
    a.name AS DatabaseName
    ,SUSER_SNAME(owner_sid) AS OwnerName
    ,a.owner_sid AS OwnerSID
    FROM sys.databases a
    WHERE a.owner_sid = 0x01;
    Dan Guzman, SQL Server MVP, http://www.dbdelta.com

  • Stored procedure scope: using sp from another database

    Good day,
    I hava a stored procedure named sp_emps (simple select from an employees table) in a database named Employees.
    I would like use this procedure in a another database named New_Employees.
    Is this possible, without creating / copying the sp to the other database ?
    Also I did not want to make the sp a system object
    Please assist

    Hi Zimiso,
    The problem here is that no matter from where you execute your procedure, its context will always be the "Employees" database.
    What you ask for is possible, but only by using Dynamic SQL.
    You can have a @Database parameter on your procedure, and then concatenate the value in this parameter to wherever needed in your dynamic queries.
    For example:
    CREATE PROCEDURE sp_emps
    @Database NVARCHAR(200)
    AS
    DECLARE @Command NVARCHAR(MAX);
    SET @Command = 'SELECT * FROM ' + QUOTENAME(@Database) + '.dbo.MyEmployees'EXECUTE(@Command)
    More information about dynamic SQL:
    http://www.mssqltips.com/sqlservertip/1160/execute-dynamic-sql-commands-in-sql-server/
    This isn't an ideal solution because there are lots of possible risks and problems that come along with dynamic SQL (for example, SQL Injection).
    The better solution, in my opinion, would be to create the stored procedure as a system object (create it in the "master" database). This will make its context change based on the database from whence it was executed.
    Eitan Blumin; SQL Server Consultant - Madeira Data Solutions;

  • To fetch Data from multiple database tables!

    How to fetch Data from fields of multiple database tables!
    Give me one example!

    use <b>join....</b>
    c the SAPHELP docs...
    FROM tabref1 [INNER] JOIN tabref2 ON cond
    Effect
    The data is to be selected from transparent database tables and/or views determined by tabref1 and tabref2. tabref1 and tabref2 each have the same form as in variant 1 or are themselves Join expressions. The keyword INNER does not have to be specified. The database tables or views determined by tabref1 and tabref2 must be recognized by the ABAP Dictionary.
    In a relational data structure, it is quite normal for data that belongs together to be split up across several tables to help the process of standardization (see relational databases). To regroup this information into a database query, you can link tables using the join command. This formulates conditions for the columns in the tables involved. The inner join contains all combinations of lines from the database table determined by tabref1 with lines from the table determined by tabref2, whose values together meet the logical condition (join condition) specified using ON>cond.
    Inner join between table 1 and table 2, where column D in both tables in the join condition is set the same:
    Table 1                      Table 2
    A
    B
    C
    D
    D
    E
    F
    G
    H
    a1
    b1
    c1
    1
    1
    e1
    f1
    g1
    h1
    a2
    b2
    c2
    1
    3
    e2
    f2
    g2
    h2
    a3
    b3
    c3
    2
    4
    e3
    f3
    g3
    h3
    a4
    b4
    c4
    3
    |--|||--|
        Inner Join
        |--||||||||--|
        | A  | B  | C  | D  | D  | E  | F  | G  | H  |
        |--||||||||--|
        | a1 | b1 | c1 | 1  | 1  | e1 | f1 | g1 | h1 |
        | a2 | b2 | c2 | 1  | 1  | e1 | f1 | g1 | h1 |
        | a4 | b4 | c4 | 3  | 3  | e2 | f2 | g2 | h2 |
        |--||||||||--|
    Example
    Output a list of all flights from Frankfurt to New York between September 10th and 20th, 2001 that are not sold out:
    DATA: DATE   LIKE SFLIGHT-FLDATE,
          CARRID LIKE SFLIGHT-CARRID,
          CONNID LIKE SFLIGHT-CONNID.
    SELECT FCARRID FCONNID F~FLDATE
        INTO (CARRID, CONNID, DATE)
        FROM SFLIGHT AS F INNER JOIN SPFLI AS P
               ON FCARRID = PCARRID AND
                  FCONNID = PCONNID
        WHERE P~CITYFROM = 'FRANKFURT'
          AND P~CITYTO   = 'NEW YORK'
          AND F~FLDATE BETWEEN '20010910' AND '20010920'
          AND FSEATSOCC < FSEATSMAX.
      WRITE: / DATE, CARRID, CONNID.
    ENDSELECT.
    If there are columns with the same name in both tables, you must distinguish between them by prefixing the field descriptor with the table name or a table alias.

  • How to get the selection parameters from logical database into one of the t

    Hi Sap ABAP Champians,
    How to get the selection parameters from logical database into one of the tab in the tabstrip selection-screen.
    Please help me for this
    Thanks
    Basu

    Hi
    Thanks, that will work, but then I'll have to insert code into all my reports.
    I can see that "Application Server Control Console" is able to rerun a report.
    This must mean that the Report Server has access to the runtime parameters.
    But how?
    Cheers
    Nils Peter

  • How to remove spaces at selection screen coming from logical database

    Hi Experties,
    When we hide some selection from the logical database.  The spaces still occupied and this make our selection screen looks awkward.
    How to remove the spaces left by hidden selection ?
    Regards
    Nislina

    Hi,
    I came across your un-answered question while searching for a solution on the similer problem. Though I couldn't fine a solution on SDN, but figured out one myself. Thought its a good idea to share it.
    AT SELECTION-SCREEN OUTPUT.
      LOOP AT SCREEN.
        IF screen-group4 = '003'.
          screen-active = 0.
          MODIFY SCREEN.
        ENDIF.
      ENDLOOP.
    Here screen-group4 contains the sequence number of the select-options field starting from 000 onwards.
    You may like to assign points and close the question.
    Kind Regards,
    Khalid Mustafa

  • Selecting a one row from the database is not working

    Hi all I am having a small problem with getting a row from my database to show up. Here is the code for the search that I want to do
                            String item; String columnName; String result; columnName = (String)searchBox.getSelectedItem(); item = searchField.getText().trim(); String tableName = "employee"; if(item == null || item.equals("")){ System.out.println("No Text entered"); }else{ try { result = sql.retrieve_From_Database(columnName, tableName, item); System.out.println(result); } catch (SQLException e1) { sql.displayErrors(e1); }
    Here is the code for the retrieve_From_Database function.
    protected String retrieve_From_Database(String columnName, String tableName, String item) throws SQLException{ ResultSet rs = null; Statement sm = null; sm = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY); rs = sm.executeQuery("select * from " + tableName + " where " + columnName + " = " + item); result = rs.getString(columnName); if(result == null){ System.out.println("Error in retrieve"); return new String("No results in DB"); }else{ System.out.println(result); return result; } }
    When I run the first code I get an error of SQL Exception: Unknown column 'anthony' in 'where clause'. But yet when I run the query of
    Select * from employee where FirstName = 'anthony'
    within the mysql commandline I get the proper result. So my question is why when running this in the command line do I get the proper result but when running the code I do not?

    jo**** wrote:
    Hi. The problem is clear in the original post. The OP is trying to use PreparedStatement
    parameters to fill out general portions of the SQL query, instead of only data values.
    He is hoping to put in a column name. That will never work. Only data values are settable.
    HTH,
    Joe WeinsteinActually, no. There's no PreparedStatement there at all. It appears that his problem is that, while the working query entered directly in the client is
    Select * from employee where FirstName = 'anthony'the query he's constructing in his code is
    Select * from employee where FirstName = anthonySince there are no quotes around anthony, it's treated as a column name.
    If he had properly used PreparedStatment as suggested in reply 1, that would not be an issue. My guess, however, is that when he claims he's using PS, it's as Tolls suggested a couple of posts ago--he's simply replacing the Statement with a PreparedStatement, but not using the parameters as suggested.
    The wrong thing for OP to do at this point is see my above comment about the quotes and explicitly add them in the query string. The right thing to do, of course, is to actually use PS's parameters.
    Edited by: jverd on Oct 11, 2010 10:08 AM

  • Select query based on joining of tables from different database is taking too long

    Hi Team,
    Select query on table with millions of records is taking very long time. It took 50mins, the below query is joining on multiple table from two databases DB1.dbo.Table1 contains 100 million records and also Table3 and Table4 (of different database) might
    contain close to 1million records.
    Select T1.*
    From DB1.dbo.Table1 T1
    Join DB1.dbo.Table2 T2 on RTRIM(T1.Col3) = RTRIM(T2.Col3)
    Join Table4 CA on RTRIM(T1.Col1) + T2.Col2 = CA.Col1
    Join Table3 U on CA.Col2 = U.Col2 AND RTRIM(T2.Col2) = U.Col3
    Where U.Col4 NOT IN ('A1', 'A2', 'A3', 'A4', 'A5', 'A6','A7','A8','A9')
    And (T1.flg IS NULL OR T1.flg = 'N')
    And LTRIM(RTRIM(T2.Col2)) NOT IN ('B1','B2')
    How can i improve the performance of this query. Actual thing is update the data in Db1.dbo.table1 based on the conditions but if the select is taking close to 1hr then update will take hours together. Indexes already implemented on all the tables.
    Thanks,
    Eshwar.
    Please don't forget to Marked as Answer if my post solved your problem and use Vote As Helpful if a post was useful. It will helpful to other users.

    Thanks.
    Join Table4 CA on RTRIM(T1.Col1) + T2.Col2 = CA.Col1
    This join is not working with spaces if Rtrim is not used.
    Thanks! Eshwar.
    Please don't forget to Marked as Answer if my post solved your problem and use Vote As Helpful if a post was useful. It will helpful to other users.

  • Select table from another database

    Hello
    Consider 2 oracle databases: A and B.
    How can i create a table in database A receiving records through a select statement to a table Z in database B?
    Is this possible?
    I thank in advance all your replies!

    You need to create a database link on table A which points to the correct schema on table B.
    Note that there needs to be an entry in the tnsnames.ora file (assuming you're using local naming) on server A which points to the database on Server B.
    Then you just reference the table via the database link.
    eg select * from dual@TNSB would access the dual table on server B.
    (assuming TNSB is the database link name)
    If you use v$instance rather than dual (and have appropriate privileges) you will see the servername of server b rather than server a.
    Edited by: Keith Jamieson on Oct 1, 2008 3:13 PM

  • Dynamic table  selection from database

    Hi ;
    I want to create  variable field that I will use at database selection.I tried to do it with   select *  from ( tabname )..... but I couldn't do it how you any offer fot this issue.

    Hi,
    DATA FCAT1 TYPE LVC_T_FCAT."fieldcat of type internal table
    DATA:DYN_ITAB TYPE REF TO DATA,"holding the dynamic internal table
    WA TYPE REF TO DATA."holding the wa for dynamic internal table
    FIELD-SYMBOLS: <DISP_TABLE> TYPE TABLE,
    <WA> TYPE ANY.
    SELECT SINGLE tab_name FROM table INTO DB_TABLE.
      CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
            EXPORTING
              I_STRUCTURE_NAME = DB_TABLE
            CHANGING
              CT_FIELDCAT      = FCAT1[].
    CALL METHOD CL_ALV_TABLE_CREATE=>CREATE_DYNAMIC_TABLE"creating dynamic internal table using fieldcat
        EXPORTING
        IT_FIELDCATALOG = FCAT1[]
        IMPORTING
        EP_TABLE = DYN_ITAB.
        ASSIGN DYN_ITAB->* TO <DISP_TABLE>."creating internal table by refering the dynamically generated internal table structure
        CREATE DATA WA LIKE LINE OF <DISP_TABLE>."creating work area for the internal table
        ASSIGN WA->* TO <WA>.
        SELECT * FROM (DB_TABLE) INTO <WA> UP TO MAX_HITS ROWS."filling the internal table
        APPEND <WA> TO <DISP_table>.
        ENDSELECT.
    <b>REWARD ALL HELPFUL ANSWERS.</b>
    rgds,
    bharat.
    Message was edited by:
            Bharat Kalagara

  • Select data from another database

    Hi,
    Is it possible to retrieve data from a table from another database?
    For instance, I have two databases db_1 and db_2
    I login to the database db_1 using
    sqlplus username/psw@db1
    and to the database db_2 using
    sqlplus username/psw@db2
    In SQL> db1 I would like to use select statement from a table in the db2. Is it possible?
    Another question:
    I have two tablespaces TP1 and TP2 in the database db1
    The TP1 is my default tablespace.
    Can I switch from TP1 to TP2 tablespace in the sqlplus?
    If not, how can I select all tables obtained in the TP2? Or how can I select data from a table in the TP2?
    Thanks

    I don't know how to create a db link.<br>
    SQL Reference Guide - CREATE DATABASE LINK<br>
    There are some examples in the doc above.<br>
    <br>
    What about if db1 and db2 have the same table name?<br>No problem, exactly like when you have two same table name accross two schemas on same database. The table suffixed by the dblink name come from the other schema of other database (schema which you use to connect through the dblink to the other database).<br>
    <br>
    Nicolas.

  • Select data from different database

    hi,
    may I know how to select data from different database?
    for example,
    I've 2 databases, OracleDB and OracleAR
    Connect with OracleAR in SQL*Plus
    select * from OracleDB.TableName
    does Oracle support this kind of query?
    how can I retrieve data from other database while im connecting with
    other database?

    Hi,
    Yes, it's possible. No, your syntax won't work.
    First of all you have to define a DATABASE LINK inside the DB where you are already connected (in this case OracleAR). Read docs how to do that.
    Second thing is the query. It will look like
    SELECT * from TableName@<NameOfDatabaseLink>Greetings,
    Guido

  • Select from database table

    Hi Experts,
    I have a database table having fields
    entity account period category value  filename
    A         1         001      ABC       100   abc.xls
    A          1        002      ABC        200  abc.xls
    B         2         001      DEF        300   sef.xls
    I now need to update a control table from the database table which will select all the distinct filenames , from the database table .
    how to do this
    Thanks
    Ankit

    Hi,
       In the select query write:
    select distinct ( filename) from dtab.

  • Select records from one database and insert it into another database

    Hi
    I need to write a statement to select records from one database which is on machine 1 and insert these records on a table in another database which is on machine 2. Following is what I did:
    1. I created the following script on machine 2
    sqlplus remedy_intf/test@sptd @load_hrdata.sql
    2. I created the following sql statements in file called load_hrdata.sql:
    rem This script will perform the following steps
    rem 1. Delete previous HR data/table to start w/ clean import tables
    rem 2. Create database link to HR database, and
    rem 3. Create User Data import table taking info from HR
    rem 4. Drop HRP link before exiting
    SET COPYCOMMIT 100
    delete from remedy.remedy_feed;
    commit;
    COPY FROM nav/donnelley@hrp -
    INSERT INTO remedy.remedy_feed -
    (EMPLID, FIRST_NAME, MI, LAST_NAME, BUSINESS_TITLE, WORK_PHONE, -
    RRD_INTRNT_EMAIL, LOCATION, RRD_OFFICE_MAIL, RRD_BUS_UNIT_DESCR) -
    USING SELECT EMPLID, FIRST_NAME, MI, LAST_NAME, BUSINESS_TITLE, WORK_PHONE, -
    RRD_INTRNT_EMAIL, LOCATION, RRD_OFFICE_MAIL, RRD_BUS_UNIT_DESCR -
    FROM ps_rrd_intf_medium -
    where empl_status IN ('A', 'L', 'P', 'S', 'X')
    COMMIT;
    EXIT;
    However, whenever I run the statement I keep getting the following error:
    SP2-0498: missing parenthetical column list or USING keyword
    Do you have any suggestions on how I can fix this or what am I doing wrong?
    Thanks
    Ali

    This doesn't seem to relate to Adobe Reader. Please let us know the product you are using so we may redirect you or refer to the list of forums at http://forums.adobe.com/

Maybe you are looking for