SQL query tunning help

Hi All,
Could you please help me out the below SQL query tuning .
Temp table is having 1 Million records
Master table is having 60 Million records
Query :
SELECT B.*,U.ID, SD, LE, LAE
FROM client.Temp B, client.Master
U WHERE U.policyno = B.policyno
AND B.UPFLAG = 0
1.     Indexes are created on both email columns and Upflag for both tables.
2.     Gathered DBMS Stats for MASTER Table
Data is loading 100k/hour on production .

When your query takes too long ...
When your query takes too long ...
HOW TO: Post a SQL statement tuning request - template posting
HOW TO: Post a SQL statement tuning request - template posting

Similar Messages

  • Cluster bar chart- sql query please help-

    Hi,
    I am trying to create cluster bar chart and am stumped with this sql query.Any help is appreciated.
    Here is my table
    city     region      issue     value
    c1     north     i1     y
    c1     north     i2     y
    c2     north     i1     n
    c2     north     i2     y
    c3     south     i1     y
    c3     south     i2     n
    c4     east     i1     n
    c4     east     i2     n
    The bar chart will have 3 series, north south and east.
    And labels will be i1 and i2. value will be number of times this issue was encountered(y) in this region.
    How can I get something like this from the above table-
    region     issue     count(yes)
    north     i1     1
    north     i2     2
    south     i1     1
    south     i2     0
    east     i1     0
    east     i2     0
    thanks

    WITH table1 AS
    (SELECT 1435177 qte_id, 2 seq_no
    FROM dual
    UNION ALL
    SELECT 1435177 qte_id, 5 seq_no
    FROM dual
    UNION ALL
    SELECT 1435177 qte_id, 7 seq_no
    FROM dual
    UNION ALL
    SELECT 1435177 qte_id, 8 seq_no
    FROM dual
    UNION ALL
    SELECT 1435177 qte_id, 12 seq_no
    FROM dual
    UNION ALL
    SELECT 1435177 qte_id, 14 seq_no
    FROM dual
    table2 AS
    SELECT 1435177 qte_id, 1 seq_no, 98500 cmmt_curr_amt
    FROM dual
    UNION ALL
    SELECT 1435177 qte_id, 2 seq_no, 98500 cmmt_curr_amt
    FROM dual
    UNION ALL
    SELECT 1435177 qte_id, 3 seq_no, 0 cmmt_curr_amt
    FROM dual
    UNION ALL
    SELECT 1435177 qte_id, 4 seq_no, 98500 cmmt_curr_amt
    FROM dual
    UNION ALL
    SELECT 1435177 qte_id, 5 seq_no, 98500 cmmt_curr_amt
    FROM dual
    UNION ALL
    SELECT 1435177 qte_id, 7 seq_no, 98500 cmmt_curr_amt
    FROM dual
    UNION ALL
    SELECT 1435177 qte_id, 8 seq_no, 98500 cmmt_curr_amt
    FROM dual
    UNION ALL
    SELECT 1435177 qte_id, 11 seq_no, 59300 cmmt_curr_amt
    FROM dual
    UNION ALL
    SELECT 1435177 qte_id, 12 seq_no, 59300 cmmt_curr_amt
    FROM dual
    UNION ALL
    SELECT 1435177 qte_id, 14 seq_no, 59300 cmmt_curr_amt
    FROM dual
    SELECT qte_id, t2_seq_no, cmmt_curr_amt - cmmt_curr_lag diff
    FROM
    (SELECT t2.qte_id, t2.seq_no t2_seq_no, t1.seq_no t1_seq_no, cmmt_curr_amt, LAG(cmmt_curr_amt,1,0) OVER (PARTITION BY t2.qte_id ORDER BY t2.seq_no) cmmt_curr_lag
    FROM table1 t1, table2 t2
    WHERE t2.qte_id = t1.qte_id (+)
    AND t2.seq_no = t1.seq_no(+)
    ORDER BY 1,2
    WHERE t1_seq_no IS NOT NULL
    ORDER BY 1,2
    QTE_ID T2_SEQ_NO DIFF
    1435177 2 0
    1435177 5 0
    1435177 7 0
    1435177 8 0
    1435177 12 0
    1435177 14 0

  • Native SQL query - Need help

    Hi All,
    We have a native SQL query accessing Oracle database(given below).
    Can anyone please let me know what this query is about and how can we fine tune the query?
    SELECT O.OBJECT_NAME ,
                   H.SID,
                   HS.MACHINE,
                   HS.PROCESS,
                   W.SID,
                   WS.MACHINE,
                   WS.PROCESS,
                   H.CTIME,
                   W.CTIME,
                   WS.ROW_WAIT_OBJ#,
                   WS.ROW_WAIT_FILE#,
                   WS.ROW_WAIT_BLOCK#,
                   WS.ROW_WAIT_ROW#,
                   HP.SPID,
                   WP.SPID
            FROM V$LOCK H, V$LOCK W, V$LOCK I, V$LOCK I2, ALL_OBJECTS O,
                 V$SESSION HS, V$SESSION WS, V$PROCESS HP, V$PROCESS WP
            WHERE   H.ID1 = W.ID1
            AND     H.SID <> W.SID
            AND     H.TYPE IN ('TX','DL')
            AND     H.REQUEST = 0
            AND     H.SID = I.SID
            AND     I.TYPE = 'TM'
            AND     I.ID1 = O.OBJECT_ID
            AND     I.ID1 = I2.ID1
            AND     W.SID = I2.SID
            AND     I2.TYPE = 'TM'
            AND     H.SID = HS.SID
            AND     W.SID = WS.SID
            AND     HS.PADDR = HP.ADDR
            AND     WS.PADDR = WP.ADDR
            INTO :EXCL_LOCK_WAITERS-OBJ_NAME   ,
                 :EXCL_LOCK_WAITERS-HOLDER_SID ,
                 :EXCL_LOCK_WAITERS-H_HOSTNAME ,
                :EXCL_LOCK_WAITERS-HOLDER_PID ,
                 :HOLDER_PID ,
                 :EXCL_LOCK_WAITERS-WAITER_SID ,
                 :EXCL_LOCK_WAITERS-W_HOSTNAME ,
                :EXCL_LOCK_WAITERS-WAITER_PID ,
                 :WAITER_PID ,
                 :EXCL_LOCK_WAITERS-HELD_SINCE ,
                 :EXCL_LOCK_WAITERS-WAITSSINCE,
                 :ROW_WAIT_OBJ,
                 :ROW_WAIT_FILE,
                 :ROW_WAIT_BLOCK,
                 :ROW_WAIT_ROW,
                 :H_PROCESS,
                 :W_PROCESS
          ENDEXEC
    Thanks in advance.
    Neethu Mohan

    Hi Neethu,
    It gives you an overwiew of blocking Oracle sessions.
    1. In general, the SQL checks Oracle sessions (SID's) that were requirering a DML lock ('TM') and now holding row locks (TX)  to prevent destructive interference of simultaneous conflicting DML or DDL operations. DML statements automatically acquire both table-level locks and row-level locks ('TX')  => holders
    It joins these with the sessions that are waiting of releasing the lock by the holders  => waiters.
    2. it retrieves the detail information wich  Oracle process , the object (table) , it's row , block  and file
    are affected by the locks.
    3. Normally, the locks are only hold for a short period of time. If you have blocking sessions it may be of a log running task (i.e. mass data update of a table) ; but it could also be a application bug due to improper handling of concurrent updates of the same object.
    4. Tuning
    V$tables are expensive to query: Why?
    v$ tables are generally Oracle memory structures.
    v$ tables are not read consistent.
    v$ tables require latches to access -- cannot modify and read memory at the same
    time.
    heavy access to v$ tables like this may cause some serious heavy duty contention.
    Especially if you self join V$lock several times.
    So the best would be to save the contents of V$LOCK in some table:
    Create table mylocks as select * from v$lock;
    Use that table for self-joining and joins to the other tables.
    You can also CTAS the other v$ tables to bypass the performance bottleneck while retrieving
    v$ directly.
    You can empty or drop the created tables any time for new data.
    Because you want to investigate only lock hold for a longert time  to copy the v$ memory structures into
    physical tables is not a disadvantage. You certainly will wait longer on finishing your query
    instead of copy them into the tables.
    Hope this helped
    yk

  • Passing parameter to a SQL query - Please help

    Hi All,
    I am new to JDBC. I have been trying to pass an external variable to an SQL Query.
    The query is
    String username1="le";
    PreparedStatement pstmt = null;
    pstmt = c.prepareStatement("select * from users where USER_NAME like '%?%'");
         pstmt.setString(1, username1);
         pstmt.executeQuery();
         ResultSet rs = pstmt.getResultSet();
    I am trying to retrieve values from the users table where the USER_NAME column value that is a String contains the supplied value username1.
    I am using the question mark (?) character to pass the value from the variable username1. I am also using the '%' substitution character which matches for any number of characters. So, the above query should retrieve rows where the USER_NAME is something like "charles","leander","Elena" etc.( that contains "le")
    I am getting the error:
    SQLException: java.sql.SQLException: ORA-01006: bind variable does not exist
    I changed the query to
    PreparedStatement pstmt = null;
    pstmt = c.prepareStatement("select * from users where USER_NAME like '% " + username1 + "%'");
         //pstmt.setString(1, username1);
         pstmt.executeQuery();
    This time , it is not giving the error and retrieving properly.
    But I want to use the original query and use the "pstmt.setString(1, username1); " . Is there any way of achieving this?
    Please help.
    Cheers,
    charles_am

    hi,
    try this...
    String username1="%le%";
    pstmt = c.prepareStatement("select * from users where USER_NAME like ?")
    pstmt.setString(1,username1);
    cheers,
    rpk

  • SQL query performace help

    Hi,
    I have a question about the performace of this SQL query. I have to display only 8 rows from this query. How is this query will be executed? What happens if the inside query returns more than 500 rows? Will it slow down?
    SELECT * FROM (SELECT prod_date, create_date, prod_name, priority, status FROM CUST_PROD
    WHERE cust_id = 100 and Status in(1, 5) order by priority, prod_name ) WHERE ROWNUM < = 8
    Thank you..

    Its hard to tell what effect more data will have on a query until it happens. As BluShadow pointed out you're not talking about huge amounts of data; with luck the numbers you mentioned will not make any difference.
    It looks like you're using the inline view to a top N query. You could look into using the RANK() function to do something similar.

  • Sql query tunning, need help!

    below is the sql that runs very very slow:
    SELECT h.STAT_HIST_ID, h.WF_ACTN, h.USR_ACTN,
          h.WF_CHNG_DATA, h.USR_CHNG_DATA, h.PRCSG_TM, h.UPD_LOGIN, h.UPD_DT,
          RTRIM(c.LAST_NM || ', ' || c.FIRST_NM || ' ' || c.MIDDLE_INITIAL)
          FROM WF_STAT_HIST h, CSE_USER_DATA_VW c WHERE h.WRK_ITM_ID = 'GT1MPTG01ATBZP'
          AND h.UPD_LOGIN = c.USER_NM (+) ORDER BY h.UPD_DT ;Here is its index:
    all_indexes:
    SCGDBA     WF_STAT_HIST_INDX_ARCH     NORMAL     SCGDBA     WF_STAT_HIST     TABLE     NONUNIQUE     DISABLED          IMS_I
    SCGDBA     PK_WF_STAT_HIST     NORMAL     SCGDBA     WF_STAT_HIST     TABLE     UNIQUE     DISABLED          SCG_I
    SCGDBA     WF_STAT_HIST_IDX1     NORMAL     SCGDBA     WF_STAT_HIST     TABLE     NONUNIQUE     DISABLED          SCG_I
    all_ind_columns:
    SCGDBA     WF_STAT_HIST_INDX_ARCH     SCGDBA     WF_STAT_HIST     ARCHIVEDT     1     7     0     ASC
    SCGDBA     PK_WF_STAT_HIST     SCGDBA     WF_STAT_HIST     STAT_HIST_ID     1     22     0     ASC
    SCGDBA     WF_STAT_HIST_IDX1     SCGDBA     WF_STAT_HIST     WF_ACTN     1     50     50     ASC
    SCGDBA     WF_STAT_HIST_IDX1     SCGDBA     WF_STAT_HIST     USR_ACTN     2     50     50     ASCHere is its explain plan:
    Plan hash value: 479056202
    | Id  | Operation           | Name             | Rows  | Bytes | Cost (%CPU)| Time     |
    |   0 | SELECT STATEMENT    |                  |   216 | 30456 | 70864   (2)| 00:14:11 |
    |   1 |  SORT ORDER BY      |                  |   216 | 30456 | 70864   (2)| 00:14:11 |
    |*  2 |   HASH JOIN OUTER   |                  |   216 | 30456 | 70863   (2)| 00:14:11 |
    |*  3 |    TABLE ACCESS FULL| WF_STAT_HIST     |   216 | 24840 | 70857   (2)| 00:14:11 |
    |   4 |    TABLE ACCESS FULL| CSE_USER_DATA_VW |  1013 | 26338 |     6   (0)| 00:00:01 |
    Predicate Information (identified by operation id):
       2 - access("H"."UPD_LOGIN"="C"."USER_NM"(+))
       3 - filter("H"."WRK_ITM_ID"='GT1MPTG01ATBZP')Could you guys take a look and tell me what the problem might be?
    I can`t give you TKPROF, because I got no access to that trace files!
    Any thoughts would be appreciated:-)
    Edited by: PhoenixBai on Sep 27, 2009 3:13 PM

    Pl post the output of the following
    select count(*) from wf_stat_hist;
    select count (distinct wrk_itm_id) from wf_stat_hist;
    select count (distinct upd_login) from wf_stat_hist;Your code is performing poorly because your WHERE clause is searching on a column on WF_STAT_HIST that is not indexed. Hopefully the output of the above code will determine which column (WRK_ITM_ID or UPD_LOGIN) is a better candidate to be indexed on. If neither of these columns is a good candidate, you may have to change the WHERE clause to be more selective rather than do a full table scan as the plan indicates.
    HTH
    Srini

  • I need to denormalize data in sql query, please help!

    With the query
    select ref, start_time, end_time, person
    from appointments
    I get, eg:
    REF START_TIME END_TIME PERSON
    1234 10:00 11:00 USER1
    1234 10:00 11:00 USER2
    The users want to see it like this:
    REF START_TIME END_TIME PERSON
    1234 10:00 11:00 USER1, USER2
    How do I do this just in sql?
    cheers
    Tracey.

    Apologies, my mistake, I forgot to connect by the ref as well...
    (Note: you can ignore the CAST to VARCHAR2(40) as that just helped me get the formatted output.)
    SQL> ed
    Wrote file afiedt.buf
      1  with t as (select 1234 as ref, '10:00' as start_time, '11:00' as end_time, 'USER1' as person from dual union all
      2             select 1234, '10:00', '11:00', 'USER2' from dual union all
      3             select 345, '11:00', '12:00', 'USER3' from dual)
      4  -- END OF TEST DATA
      5  select ref, start_time, end_time, CAST(MAX(LTRIM(SYS_CONNECT_BY_PATH(person,', '),', ')) AS VARCHAR2(40)) as users
      6  from (select ref, start_time, end_time, person
      7              ,row_number() over (partition by ref, start_time, end_time order by person) rn
      8        from t) t
      9  CONNECT BY rn = PRIOR rn+1 AND ref = PRIOR ref
    10  START WITH rn = 1
    11* GROUP BY ref, start_time, end_time
    SQL> /
           REF START END_T USERS
          1234 10:00 11:00 USER1, USER2
           345 11:00 12:00 USER3
    SQL>

  • Single sql query-please help

    Hi experts,
    what i want to do is write a single query which will show whether a employee
    exits in the company or not.I have two tables emp and dept.There are as follows.
    SQL> select * from emp;
    NAME DEPTNO EMPNO
    xxx 10 33036
    YYY 12 2345
    ZZZ 13 678
    KKK 14 5678
    RRR 15 7865
    SQL> select * from dept;
    DEPTNO LOCATION
    10 AAA
    11 BBB
    12 CCC
    13 DDD
    what i want is it will select records from the emp table and find whether corrosponding
    deptno really exists in the dept table.If the value is found in deptno column the dept table then it will set the value
    Y other wise it will be N and all i have to do with the help of a single query.
    expected result
    name empno exists
    xxx 33036 Y
    YYY 2345 Y
    ZZZ 678 Y
    KKK 5678 N
    RRR 7865 N
    Please help.
    Regards
    Rajat

    SELECT EMPNO, NAME , EMPNO , NVL( ( SELECT 'Y' FROM    DEPT WHERE EMP.DEPTNO=DEPT.DEPTNO),'N') EXIST
    FROM EMP
    ORDER BY 1
    Demo
    SQL> WITH EMP AS(
      2  SELECT 'XXX' NAME , 10 DEPTNO ,33036  EMPNO FROM DUAL UNION
      3  SELECT 'YYY', 12, 2345 FROM DUAL UNION
      4  SELECT 'ZZZ', 13 ,678 FROM DUAL UNION
      5  SELECT 'KKK', 14 ,5678 FROM DUAL UNION
      6  SELECT 'RRR', 15 ,7865 FROM DUAL  ),
      7  DEPT AS(
      8  SELECT 10  DEPTNO,'AAA' DNAME FROM DUAL UNION
      9  SELECT 11 ,'BBB' FROM DUAL UNION
    10  SELECT 12 ,'CCC' FROM DUAL UNION
    11  SELECT 13 ,'DDD'FROM DUAL )
    12  SELECT EMPNO, NAME , EMPNO , NVL( ( SELECT 'Y' FROM    DEPT WHERE EMP.DEPTNO=DEPT.DEPTNO),'N')
    EXIST
    13  FROM EMP
    14  ORDER BY 1
    15  /
         EMPNO NAM      EMPNO E
           678 ZZZ        678 Y
          2345 YYY       2345 Y
          5678 KKK       5678 N
          7865 RRR       7865 N
         33036 XXX      33036 Y
    SQL> Edited by: Salim Chelabi on Dec 7, 2008 4:15 AM

  • SQL query - syntax help

    Hi All,
    I have a table that has 2 columns X, and Y.
    create table temp(
    x number(5);
    y number (5);
    );I am trying to write a query to check both columns, here is what I mean.
    SELECT CASE
                 IF X = 1 AND Y = 0 THEN 3
              IF X = 2 AND Y = 1 THEN 4
              IF X  = 4 AND Y IS NULL THEN 5
          END AS STATUS
    FROM .....temp ...etc.I know what I want but having problems with the syntax.
    Thanks in Advance for your help.
    I'm using Oracle 11g, SQL Dev 3.0

    select case when x = 1 and y = 0 then 3
                when x = 2 and y = 1 then 4
                when x = 4 and y is null then 5
                else null
            end ans
      from ...for more on the case syntax see either one of these links.
    http://download.oracle.com/docs/cd/B19306_01/appdev.102/b14261/case_statement.htm
    http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/expressions004.htm

  • Need SQl Query tune

    Hi,
    This is a part of the procedure which is veryyy slow.. Need some suggestions for the below Query to improve performance. The explain plan and all will be correct in PRODUCTION DB.. In that system, I cant have the explain plan to be copied here. So need suggestions by the Query.
    Here PB,PBD and PBD1 are very huge tables..
    I am unable to paste the Query.. Please help me
    Thanks in advance..

    My car is broken, but I cannot produce it for repairs.
    Not sure of your expectations, but this isn't a way where volunteers have been provided any tiny piece of information to suggest any tuning advice.
    Please read the linked thread HOW TO: Post a SQL statement tuning request - template posting and post with mentioned information.

  • How to add pl sql query into help text of an item.

    Hi
    In my database i have added comments to the colums. I would like to use this comments in the help text of the items of my application. I have the query but when I use it in the help text attribute it does not execute the query, it presents the query.
    Any ideas?

    It is probably too late now, but note that if you create your application using the Create Application Wizard and Application Models, based on User Interface Defaults created in the SQL Workshop, any column comments will automatically become application help text.

  • SQL query tune

    Hi,
    I have a query which is used in one of ur reports which tries to access 11 tables using oracle reports application. when users are trying to access thsi report its taking aroung 4 min. to compete the report, in a day we need this report to run on more than for nearly 70 times(70 reports in a day).
    we provide the registration number, based on this number it generates the details of the owner, address, registration details, permits etc
    we have more than 1gb of sga in which shared pool has nearly 750mb.
    Select           
         pt.ppr_pert_no PERT_NO,
         gm.GOF_OFF_NM TRAN_A,
         (ROW_FST_NAME || ' ' || ROW_MID_NAME || ' ' ||
    ROW_LST_NAME)          NAME,
         ROW_FH_NAME          G_NAME,
         rad_addr_ln1 addr1,
         rad_addr_ln2 addr2,
         rad_addr_ln3 addr3,
         RAD_CITY city,
         tm.tsn_state_nm state,
         nvl( fn_get_rte_nm(prt_off_cd, prt_route) , '-' )
    ROUTE,
         rt.RVD_REGN_NO     re_no,
         rt.RVD_MKR_NAME     make,
         rt.RVD_MKR_CLAS      model,
         rt.RVD_FUEL          petrol,
         rt.RVD_CHAS_NO chasis,
         rt.RVD_ENG_NO engine,
         pd.ppd_oper_dt dt,
    pd.ppd_fr_mt_no          FARE_MNO,
         pd.ppd_fr_mt_mk          FARE_MK,
         ( nvl(rt.RVD_STNG_CAP,0) || '+' ||
    nvl(rt.RVD_DC_CAP, 0))          S_CAPACITY,
         pt.ppr_exp_dt               EXPIRY_DT,
         pd.ppd_fare_rt               RATE_FR,
         pd.ppd_oper_flg
    OPER_TYP,
         pt.ppr_pert_typ PERT_TYP
    FROM
         gm_off_dtl gm,
         pt_permits pt,
         pt_per_dtls     pd,
         pt_per_route pr,
         pm_route     pm,
         rt_breg rb,
         rt_veh_dtl      rt,
         rt_owner     po,
         rt_address     pa,
         tm_state_nm tm
    WHERE     GOF_OFF_CD = (select gpr_value from gm_prm_chr
    where gpr_type = 'G_OF_CODE' )
    AND     pt.ppr_off_cd      =      GOF_OFF_CD
    and     pt.ppr_pert_no      =      :per_no
    and      pt.ppr_pert_st in      ('PDG', 'VAL')
    and pt.ppr_pert_typ in
    ('CCP','DP','BSP','TPCC','TCP')
    and      pd.ppd_off_cd     =     pt.ppr_off_cd     
    and      pd.ppd_pert_no      =      pt.ppr_pert_no
    and      pd.ppd_pd_st      =      'N'
    and      rt.RVD_REGN_NO     =     pd.ppd_regn_no      
    and      pr.pru_off_cd(+) =      pd.ppd_off_cd
    and      pr.pru_pert_dtl(+) =      pd.ppd_pert_dtl
    and      pm.prt_route (+) =      pr.pru_route
    and      rb.rbr_regn_no = rt.rvd_regn_no
    and      rb.rbr_active = 'A'
    and      ROW_OFF_CD      =      pd.PPD_OFF_CD
    AND     po.ROW_OWNER_ID     =      pd.ppd_owner
    and      RAD_OFF_CD      =      ROW_OFF_CD
    and      pa.rad_addr_id      =     po.row_addr_id
    and      (tm.tsn_state_nm = pa.rad_state or
    tm.tsn_state_cd = pa.RAD_STATE)
    group by
    pt.ppr_pert_no ,
    gm.GOF_OFF_NM ,
    (ROW_FST_NAME || ' ' || ROW_MID_NAME || ' ' ||
    ROW_LST_NAME),
    ROW_FH_NAME     ,
    RAD_ADDR_LN1 ,
    RAD_ADDR_LN2 ,
    RAD_ADDR_LN3 ,
    RAD_CITY ,
    tm.tsn_state_nm ,
    nvl( fn_get_rte_nm(prt_off_cd, prt_route) , '-' ) ,
    rt.RVD_REGN_NO     ,
    rt.RVD_MKR_NAME     ,
    rt.RVD_MKR_CLAS      ,
    rt.RVD_FUEL          ,
    rt.RVD_CHAS_NO ,
    rt.RVD_ENG_NO ,
    pd.ppd_fr_mt_no          ,
    pd.ppd_fr_mt_mk          ,
    ( nvl(rt.RVD_STNG_CAP,0) || '+' ||
    nvl(rt.RVD_DC_CAP, 0))     ,
    pt.ppr_exp_dt          ,
    pd.ppd_fare_rt     ,
    pd.ppd_oper_dt,
    pd.ppd_oper_flg     ,
    pt.ppr_pert_typ
    how to tune or any other ways to do it.

    here are the details
    rt_owner,rt_address,tm_state_nm has around 1,50,234 records
    pt_per_route,pt_per_dtls,pt_permits,rt_berg,pm_route has around 2lac records
    reprot will be based on registration number and will display all the details like his name of the owner,father name,address,vehicle type,class,manufacture,engine no,chassisno,permit obtain date,permit expire date, permit fare etc.pertain to that registration and if permit is obtain more than twice the details will also be shown.
    oracle ver.8.1.7/windows2000 server
    optimizer mode is rule based.

  • SQL Query -- Please Help

    Table1
    QTE_ID     SEQ_NO
    1435177     2
    1435177     5
    1435177     7
    1435177     8
    1435177     12
    1435177     14
    Table2
    QTE_ID     SEQ_NO     CMMT_CURR_AMT
    1435177     1     98500
    1435177     2     98500
    1435177     3     0
    1435177     4     98500
    1435177     5     98500
    1435177     7     98500
    1435177     8     98500
    1435177     11     59300
    1435177     12     59300
    1435177     14     59300
    The result should be
    QTE_ID     SEQ_NO     CMMT_CURR_AMT
    1435177     2     0 (where 0 = cmmt_curr_amt of seq 2 - cmmt_curr_amt of seq 1 from table 2)
    1435177     5     0 (where 0 = cmmt_curr_amt of seq 5 - cmmt_curr_amt of seq 4 from table 2)
    1435177     7     0 (where 0 = cmmt_curr_amt of seq 7 - cmmt_curr_amt of seq 5 from table 2)
    1435177     8     0 (where 0 = cmmt_curr_amt of seq 8 - cmmt_curr_amt of seq 7 from table 2)
    1435177     12     0 (where 0 = cmmt_curr_amt of seq 12 - cmmt_curr_amt of seq 11 from table 2)
    1435177     14     0 (where 0 = cmmt_curr_amt of seq 14 - cmmt_curr_amt of seq 12 from table 2)
    I have to get the difference of cmmt_curr_amt from the table2 from seq 14 to seq 12 for seq14 in table 1.
    Please help me in writing the query.
    Thanks in advance.
    Srinivas

    WITH table1 AS
    (SELECT 1435177 qte_id, 2 seq_no
    FROM dual
    UNION ALL
    SELECT 1435177 qte_id, 5 seq_no
    FROM dual
    UNION ALL
    SELECT 1435177 qte_id, 7 seq_no
    FROM dual
    UNION ALL
    SELECT 1435177 qte_id, 8 seq_no
    FROM dual
    UNION ALL
    SELECT 1435177 qte_id, 12 seq_no
    FROM dual
    UNION ALL
    SELECT 1435177 qte_id, 14 seq_no
    FROM dual
    table2 AS
    SELECT 1435177 qte_id, 1 seq_no, 98500 cmmt_curr_amt
    FROM dual
    UNION ALL
    SELECT 1435177 qte_id, 2 seq_no, 98500 cmmt_curr_amt
    FROM dual
    UNION ALL
    SELECT 1435177 qte_id, 3 seq_no, 0 cmmt_curr_amt
    FROM dual
    UNION ALL
    SELECT 1435177 qte_id, 4 seq_no, 98500 cmmt_curr_amt
    FROM dual
    UNION ALL
    SELECT 1435177 qte_id, 5 seq_no, 98500 cmmt_curr_amt
    FROM dual
    UNION ALL
    SELECT 1435177 qte_id, 7 seq_no, 98500 cmmt_curr_amt
    FROM dual
    UNION ALL
    SELECT 1435177 qte_id, 8 seq_no, 98500 cmmt_curr_amt
    FROM dual
    UNION ALL
    SELECT 1435177 qte_id, 11 seq_no, 59300 cmmt_curr_amt
    FROM dual
    UNION ALL
    SELECT 1435177 qte_id, 12 seq_no, 59300 cmmt_curr_amt
    FROM dual
    UNION ALL
    SELECT 1435177 qte_id, 14 seq_no, 59300 cmmt_curr_amt
    FROM dual
    SELECT qte_id, t2_seq_no, cmmt_curr_amt - cmmt_curr_lag diff
    FROM
    (SELECT t2.qte_id, t2.seq_no t2_seq_no, t1.seq_no t1_seq_no, cmmt_curr_amt, LAG(cmmt_curr_amt,1,0) OVER (PARTITION BY t2.qte_id ORDER BY t2.seq_no) cmmt_curr_lag
    FROM table1 t1, table2 t2
    WHERE t2.qte_id = t1.qte_id (+)
    AND t2.seq_no = t1.seq_no(+)
    ORDER BY 1,2
    WHERE t1_seq_no IS NOT NULL
    ORDER BY 1,2
    QTE_ID T2_SEQ_NO DIFF
    1435177 2 0
    1435177 5 0
    1435177 7 0
    1435177 8 0
    1435177 12 0
    1435177 14 0

  • What's wrong with this sql query?  Help

    hi
    i am having difficulty executing this query
    ResultSet s=st.executeQuery("select * from employee where iden = ?"+id);
    here in my program st is statement obg
    iden is attribute name in table
    id i am getting at run time from user
    please help...it says wrong number of parameters
    thank you

    That's correct, get rid of the Question mark. Questions marks are used in PreparedStatements, but they are also used in pattern matching. I am assuming the iden is the table identity. Therefore, I am assuming it is numeric. If so, you can't use the question mark because pattern matching is only done with strings. If you are treating your statement as a PreparedStatement, then you have done it wrong. (See the API) Here is a code snippet from the API:PreparedStatement pstmt =     
       con.prepareStatement("UPDATE EMPLOYEES SET SALARY = ? WHERE ID = ?");
    pstmt.setBigDecimal(1, 153833.00)
    pstmt.setInt(2, 110592)It seems you are using a Statement object, so, you need to get rid of that question mark.
    tajenkins

  • Using substr function loses index. Query tune help

    Hi
    when i use substr function in the join condition it loses index and the query is slow. How to fix this problem or tune this query. These are the lines in the query.
    and substr(a.invoice_num,1,9) = l.invoice_num
    and substr(c.invoice_num,1,9) = mgr_apprv_lst.user_key
    and substr(c.invoice_num,1,9) = pbl_apprv_lst.user_key
    select
    pap.full_name employe_name,
         k.SEGMENT1 ||'.' ||k.segment2 Cost_Center,
         a.invoice_num Invoice_Number,
         b.item_description Line_Item,
         b.amount Amount,
         cc.trx_id Corporate_Card_Transaction_Id,
         cc.transaction_date Date_Charge_Incurred,
         cc.posted_date Date_Charge_Posted_To_USBank,
         cc.creation_date Date_Posted_To_IExpense,
         a.creation_date Expense_Report_Creation_Date,
         l.report_submitted_date Expense_Report_Submitted_Date,
         mgr_apprv_lst.activity_begin_date Managers_Approval_Begin_Date,
         mgr_apprv_lst.activity_end_date Managers_Approval_End_Date,
         pbl_apprv_lst.activity_begin_date AP_Approval_Begin_Date,
         pbl_apprv_lst.activity_end_date AP_Approval_End_Date,
         e.check_date Payment_Date_To_USBank,
         e.check_number Payment_Number,
         mgr_apprv_lst.activity_result_display_name Managers_Process_Result,
         pbl_apprv_lst.activity_result_display_name AP_Process_Result
    from
         ap_checks_all e,
         ap_invoice_payments_all d,
         ap_invoices_all c,
    ap_expense_report_headers_all a,
         ap_credit_card_trxns_all cc,
         per_all_people_f pap,
         ap_expense_report_headers_all l,
         ap_expense_report_lines_all b,
         gl_code_combinations k,
         (select ias1.user_key,
         ias1.activity_result_display_name,
         ias1.activity_begin_date,
         ias1.activity_end_date
    from wf_item_activity_statuses_v ias1,
    (select c1.invoice_num
         from ap_checks_all e1,
         ap_invoice_payments_all d1,
         ap_invoices_all c1
         where trunc(e1.check_date) between nvl(:From_Date, trunc(e1.check_date))
         and nvl(:To_Date, trunc(e1.check_date))
    and e1.org_id = 141
    and e1.void_date IS null
    and d1.check_id = e1.check_id
         and c1.invoice_id = d1.invoice_id) inv_lst1
    where ias1.item_type = 'APEXP'
    and ias1.user_key = inv_lst1.invoice_num
    and ias1.activity_name = 'AP_MANAGER_APPROVAL_PROCESS') mgr_apprv_lst,
    (select ias2.user_key,
         ias2.activity_result_display_name,
         ias2.activity_begin_date,
         ias2.activity_end_date
    from wf_item_activity_statuses_v ias2,
    (select c2.invoice_num
         from ap_checks_all e2,
         ap_invoice_payments_all d2,
         ap_invoices_all c2
         where trunc(e2.check_date) between nvl(:From_Date, trunc(e2.check_date))
         and nvl(:To_Date, trunc(e2.check_date))
    and e2.org_id = 141
    and e2.void_date IS null
    and d2.check_id = e2.check_id
         and c2.invoice_id = d2.invoice_id) inv_lst2
    where ias2.item_type = 'APEXP'
    and ias2.user_key = inv_lst2.invoice_num
    and ias2.activity_name = 'AP_PAYABLES_APPROVAL_PROCESS') pbl_apprv_lst
    where
    trunc(e.check_date) between nvl(:From_Date, trunc(e.check_date))
    and nvl(:To_Date, trunc(e.check_date))
    and e.org_id = 141
    and e.void_date IS null
    and d.check_id = e.check_id
    and c.invoice_id = d.invoice_id
    and a.invoice_num = c.invoice_num
    and a.source = 'CREDIT CARD'
    and a.employee_id = nvl(:Emp_id,a.employee_id)
    and a.report_header_id = b.report_header_id
    and cc.trx_id = b.credit_card_trx_id
    and pap.person_id = a.employee_id
    and pap.effective_start_date <= trunc(sysdate)
    and pap.effective_end_date >= trunc(sysdate)
    and k.code_combination_id = b.code_combination_id
    and k.segment2 between nvl(:From_Cost_Center,k.segment2)
    and nvl(:To_Cost_Center,k.segment2)
    and substr(a.invoice_num,1,9) = l.invoice_num
    and substr(c.invoice_num,1,9) = mgr_apprv_lst.user_key
    and substr(c.invoice_num,1,9) = pbl_apprv_lst.user_key

    Hi
    If I understood correctly your logic, and if the columns involved are of type varchar2, you can use the like operator:
    and a.invoice_num like l.invoice_num || '%'
    and c.invoice_num like mgr_apprv_lst.user_key  || '%'
    and c.invoice_num like pbl_apprv_lst.user_key  || '%'In this case, Oracle will be able to use the indexes. If they are numeric, you need to do something like:
    and a.invoice_num between l.invoice_num * 1000000
                                and l.invoice_num * 1000000 + 999999I hope this makes sense...
    Luis

Maybe you are looking for