IN clause with ORDER BY clause in sub-queries

Hello,
We generate dynamic queries with the following statement pattern (could be many union/intersect sub-queries):
select my_col
from my_table
where my_col IN
select table_2.my_col , x_col from table_2 where x_col > 10
UNION
select table_3.my_col , y_col from table_3 where y_col > 20
INTERSECT
select table_4.my_col , z_col from table_4 where z_col is between 30 and 50
I know that I can do just the sub-queries w/ an ORDER BY clause as follows (as long as the 2nd parameter in the select stmts are of the same type):
select table_2.my_col , x_col from table_2 where x_col > 10
UNION
select table_3.my_col , y_col from table_3 where y_col > 20
INTERSECT
select table_4.my_col , z_col from table_4 where z_col is between 30 and 50
order by 2 desc
But my questions are:
1. What is (if there is) the syntax that will ensure that the result set order will be that of the ordering of the sub-queries?
Or does my SQL stmt have to have syntactically (but not semantically) change to achieve this?
Thanks,
Jim

Randolf Geist wrote:
just a minor doubt - I think it is not officially supported to have separate ORDER BYs in a compound query with set operators (e.g. UNION / UNION ALL subsets). Of course one could use inline views with NO_MERGE + NO_ELIMINATE_OBY hints, but I think the only officially supported approach is to use a single, final ORDER BY (that needs to use positional notation as far as I remember).
Randolf,
You're right, of course, about the separate "order by" clauses.
Interestingly the following type of thing does work though (in 10.2.0.3, at least):
with v1 as (
    select * from t1 where col1 = 'ABC' order by col2
v2 as (
    select * from t1 where col1 = 'DEF' order by col2
select * from v1
union all
select * from v2
;A quick check the execution plan suggsts that Oracle appears to be convering this to the following - even though its technically not acceptable in normal circumstances:
select * from t1 where col1 = 'ABC' order by col2
union all
select * from t1 where col1 = 'DEF' order by col2
;Regards
Jonathan Lewis
http://jonathanlewis.wordpress.com
http://www.jlcomp.demon.co.uk
To post code, statspack/AWR report, execution plans or trace files, start and end the section with the tag {noformat}{noformat} (lowercase, curly brackets, no spaces) so that the text appears in fixed format.
"Science is more than a body of knowledge; it is a way of thinking"
Carl Sagan                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

Similar Messages

  • With clause vs sub-queries

    db and dev 10g rel2 , hi all,
    i am trying to learn the "WITH CLAUSE" , and i do not see any difference between using it and using sub-queries .
    when i searched for this , i found that the with clause is used when You need to reference the subquery block multiple places in the query by specifying the query name , but i can not imagine an example for doing so .
    if you could provide me with an example please ? and telling me about another situations in which i could need using the "with clause" if any ?
    thanks

    >
    db and dev 10g rel2 , hi all,
    i am trying to learn the "WITH CLAUSE" , and i do not see any difference between using it and using sub-queries .
    when i searched for this , i found that the with clause is used when You need to reference the subquery block multiple places in the query by specifying the query name , but i can not imagine an example for doing so .
    if you could provide me with an example please ? and telling me about another situations in which i could need using the "with clause" if any ?
    >
    It isn't just about referencing a subquery multiple times. There are other advantages to using 'common table expressions'/'subquery factoring' also.
    Take a look at the example below. It first defines 'dept_costs' as a query block, then defines 'avg_cost' as a new query block and it references the first query block.
    Then the actual query references both query blocks just as if they are tables. And, in fact, in some circumstances Oracle will actually materialize them AS temporary tables.
    Look at how easy it is to understand the entire statement. You can focus first on the 'dept_costs' query block WITHOUT having to look at anything else. That is because the query block is self-contained; you are defining a result set. There is no 'join' or connection to any other part of the statement.
    It is easy for a developer, and for Oracle, to understand what is needed for that one piece.
    Next you can focus entirely on the 'avg_cost' query block. Since it uses the first query block just as if it were a table you can treat it as a table. That means you do NOT even need to look at the first query block to understand what the second query block is doing.
    Same with the actual query. You can analyze it by treating the two query blocks just as if they were other tables.
    Even better you can test the first query block by itself in sql*plus or other tool to confirm that it works and you can create an execution plan for it to make sure it will use an appropriate index. You can also then test the first and second query blocks together to make sure THEY have a proper execution plan.
    Then when you test then entire statement you already know that the query blocks work correctly.
    Try to do THAT with a query that uses nested sub-queries.
    Sure - you could write a set of nested sub-queries to accomplish the same thing (Oracle will sometimes rewrite your query that way itself) but it becomes one big query and the individual pieces are not nearly as easy to see, analyze or understand.
    It can be difficult if not impossible to extract a nested query in order to test it even to just try to get the syntax working. And when you do extract it you will often be testing something that isn't quite exactly the same as when i t was nested.
    So: easier to understand, easier to write and test (especially for new developers) as well as easier to use multiple times without having to duplicate it.
    >
    subquery_factoring_clause
    The WITH query_name clause lets you assign a name to a subquery block. You can then reference the subquery block multiple places in the query by specifying the query name. Oracle Database optimizes the query by treating the query name as either an inline view or as a temporary table.
    >
    The SQL Language doc has an example.
    http://docs.oracle.com/cd/B28359_01/server.111/b28286/statements_10002.htm#i2129904
    >
    Subquery Factoring: Example The following statement creates the query names dept_costs and avg_cost for the initial query block containing a join, and then uses the query names in the body of the main query.
    WITH
    dept_costs AS (
    SELECT department_name, SUM(salary) dept_total
    FROM employees e, departments d
    WHERE e.department_id = d.department_id
    GROUP BY department_name),
    avg_cost AS (
    SELECT SUM(dept_total)/COUNT(*) avg
    FROM dept_costs)
    SELECT * FROM dept_costs
    WHERE dept_total >
    (SELECT avg FROM avg_cost)
    ORDER BY department_name;
    DEPARTMENT_NAME DEPT_TOTAL
    Sales 313800
    Shipping 156400

  • Order by clause in Sub query

    Hi,
    Can we use order by clause in Sub query?
    While using the order by clause, I am getting the "missing expression error" . If I remove order by clause query executing fine.
    Here is my query:
    select *
    from emp_mstr
    where emp_no in(select
    emp_no
    from emp_mstr
    order by branch_no);
    Thanks & Regards,
    Mahi

    May be you miss some required spaces also, other than wrong use of ORDER BY
    select *
    from emp_mstr
    where emp_no in
         ( select e2.emp_no
           from emp_mstr e2
    --       order by e2.branch_no
         );Why do you want to ORDER BY in the subquery, which you use with IN clause? That will not make any difference in the result..Means the result you get with ORDER BY will be same as without that.. And in this case, ORDER by is a unncessary overhead.. And Ordering is very costly..
    And why do you want to have the IN clause at all in your query? You are referring the same tables in the main query and sub query..
    The below will give the same result
    select *
    from emp_mstr
    where emp_no is not nullIf you want to use another table in the subquery, always use aliasess...
    select *
    from emp_mstr
    where emp_no in
         ( select e2.emp_no
           from emp_mstr2 e2
    --       order by e2.branch_no
         );

  • First clause with order by

    Hi,
    I have a requirement where I need to display a list of employees, performance rating and manager id. I want to display the emplyees first whos manager is as manager_id as input parameter. So lets say a manager logs in, he should see his reportees first and then therest. How can I implemet this in SQL? I am trying first clause with order by but not sure how to do that here. A help on this would be appreciated.
    procedure
    display_list(manager_id in varchar2)
    Output is:
    emp_id
    manager_id
    performance_rating
    Edited by: Nandini on Apr 28, 2013 10:48 PM

    You can use a CASE statment, which will check the MANGER_ID column against the passed manager id value, as shown below..
    order by
      case when manager_id = :input_manager_id then 0
             else 1
      end,manager_id,performace_rating desc

  • Problem with order by clause

    Hai all,
    I have problem with order by clause,
    My query is
    "select number from table1 order by number asc "
    and the output is displaying as
    1
    10
    12
    13
    15
    17
    19
    2
    20
    21
    22
    But if we give order by it should display as below only right ?
    1
    2
    10
    12
    13
    15
    17
    19
    20
    21
    22 ........
    Please help me why it is not displaying like it. and how to make the statement to display like the second case. Thanks in advance.
    Regards,
    Uraja

    The column datatype that you are selecting is not of NUMBER datatype(might be char or varchar2) hence you are getting such result set.
    And for this purpose, it is recommended to set datatype of a column appropriately.
    For now you can add TO_NUMBER function to column in ORDER BY clause, only if it has data of number type.
    Edited by: Ora on 19 Nov, 2012 3:10 AM

  • Query with order by clause

    Hi,
    I found a query with order by clause in procedure which is taking long time.
    Stats are upto date.
    Total Rows :650000.
    It is ordered by primary key column.
    select * from table_name order by col1;
    col1 is a primary key.No of cpu's used is 4.
    can anyone suggest me a better solution to improve the performance of a query.
    Is it better to use parallel hint for above scenario.
    Any help really apprecaited.
    Thanks in advance.

    Hi,
    Thanks for ur immediate reply.
    It doesn't have where clause.
    below is the plan
    SQL> select * from table(dbms_xplan.display);
    PLAN_TABLE_OUTPUT
    Plan hash value: 619071987
    | Id  | Operation                   | Name                 | Rows  | Bytes | Cos
    |   0 | SELECT STATEMENT            |                      |   671K|   255M| 125
    |   1 |  TABLE ACCESS BY INDEX ROWID| FULL_ITEM_FACILITIES |   671K|   255M| 125
    |   2 |   INDEX FULL SCAN           | FIF_PK               |   671K|       |
    9 rows selected
    Executed in 0.094 secondsThanks in advance
    Edited by: unique on Jun 22, 2009 8:26 AM

  • Long run time with ORDER by clause

    Hi,
    I am having a query which is executing fine(in 2 mins) but when i am using order by clause in it, its taking around 13 mins.
    Can anyone suggest what could be th reason and how to execute the same query are get the ordered record?
    thanks.

    Sorry for not providing complet details.
    Database version id 10g.
    Below is the execution plan when using with order by clause.
    Execution Plan
    | Id  | Operation                              | Name              | Rows  | Bytes | Cost  |
    |   0 | SELECT STATEMENT                       |                   |     1 |   118 |  1538 |
    |   1 |  FILTER                                |                   |       |       |       |
    |   2 |   HASH JOIN                            |                   | 16657 |  1089K|    56 |
    |   3 |    TABLE ACCESS FULL                   | FILETYP           |     6 |    60 |     3 |
    |   4 |    TABLE ACCESS FULL                   | FILETYPSOURCEID   | 16657 |   927K|    52 |
    |   5 |   NESTED LOOPS                         |                   |     1 |    35 |     3 |
    |   6 |    TABLE ACCESS BY INDEX ROWID         | FILEWAREHOUSE     |     1 |    25 |     2 |
    |   7 |     INDEX RANGE SCAN                   | FILEWAREHOUSE_DX2 |     1 |       |     2 |
    |   8 |    TABLE ACCESS BY INDEX ROWID         | EXTFILE           |     1 |    10 |     1 |
    |   9 |     INDEX UNIQUE SCAN                  | PK_EXTFILE        |     1 |       |     1 |
    |  10 |  SORT ORDER BY                         |                   |     1 |   118 |  1538 |
    |  11 |   NESTED LOOPS                         |                   |     1 |   118 |  1534 |
    |  12 |    NESTED LOOPS OUTER                  |                   |     1 |   100 |  1533 |
    |  13 |     NESTED LOOPS OUTER                 |                   |     1 |    96 |  1532 |
    |  14 |      NESTED LOOPS                      |                   |     1 |    88 |  1531 |
    |  15 |       HASH JOIN                        |                   |     5 |   360 |  1524 |
    |  16 |        TABLE ACCESS BY INDEX ROWID     | RTXN              |     1 |    22 |     1 |
    |  17 |         NESTED LOOPS                   |                   |   117 |  5148 |  1518 |
    |  18 |          VIEW                          | VW_SQ_1           | 20869 |   448K|  1441 |
    |  19 |           HASH GROUP BY                |                   | 20869 |   427K|  1441 |
    |  20 |            FILTER                      |                   |       |       |       |
    |  21 |             TABLE ACCESS BY INDEX ROWID| RTXNSTATHIST      | 20869 |   427K|  1304 |
    |  22 |              INDEX RANGE SCAN          | RTXNSTATHIST_DX2  | 20869 |       |    29 |
    |  23 |          INDEX RANGE SCAN              | PK_RTXN           |     1 |       |     1 |
    |  24 |           NESTED LOOPS                 |                   |     1 |    24 |     3 |
    |  25 |            TABLE ACCESS BY INDEX ROWID | FILEWAREHOUSE     |     1 |    14 |     2 |
    |  26 |             INDEX RANGE SCAN           | FILEWAREHOUSE_DX2 |     1 |       |     2 |
    |  27 |            TABLE ACCESS BY INDEX ROWID | EXTFILE           |     1 |    10 |     1 |
    |  28 |             INDEX UNIQUE SCAN          | PK_EXTFILE        |     1 |       |     1 |
    |  29 |        TABLE ACCESS FULL               | RTXNTYP           |     1 |    28 |     5 |
    |  30 |       TABLE ACCESS BY INDEX ROWID      | RTXNSTATHIST      |     1 |    16 |     2 |
    |  31 |        INDEX RANGE SCAN                | PK_RTXNSTATHIST   |     1 |       |     1 |
    |  32 |      TABLE ACCESS BY INDEX ROWID       | NTWKNODE          |     1 |     8 |     1 |
    |  33 |       INDEX UNIQUE SCAN                | PK_NTWKNODE       |     1 |       |     1 |
    |  34 |     INDEX UNIQUE SCAN                  | PK_ORG            |     1 |     4 |     1 |
    |  35 |    TABLE ACCESS BY INDEX ROWID         | ACCT              |     1 |    18 |     1 |
    |  36 |     INDEX UNIQUE SCAN                  | PK_ACCT           |     1 |       |     1 |
    Below is the execution plan when running without ORDER BY clause...
    Execution Plan
    | Id  | Operation                             | Name              | Rows  | Bytes | Cost  |
    |   0 | SELECT STATEMENT                      |                   |     1 |   135 |  1537 |
    |   1 |  FILTER                               |                   |       |       |       |
    |   2 |   HASH JOIN                           |                   | 16657 |  1089K|    56 |
    |   3 |    TABLE ACCESS FULL                  | FILETYP           |     6 |    60 |     3 |
    |   4 |    TABLE ACCESS FULL                  | FILETYPSOURCEID   | 16657 |   927K|    52 |
    |   5 |   NESTED LOOPS                        |                   |     1 |    35 |     3 |
    |   6 |    TABLE ACCESS BY INDEX ROWID        | FILEWAREHOUSE     |     1 |    25 |     2 |
    |   7 |     INDEX RANGE SCAN                  | FILEWAREHOUSE_DX2 |     1 |       |     2 |
    |   8 |    TABLE ACCESS BY INDEX ROWID        | EXTFILE           |     1 |    10 |     1 |
    |   9 |     INDEX UNIQUE SCAN                 | PK_EXTFILE        |     1 |       |     1 |
    |  10 |  NESTED LOOPS                         |                   |     1 |   135 |  1534 |
    |  11 |   NESTED LOOPS OUTER                  |                   |     1 |   117 |  1533 |
    |  12 |    NESTED LOOPS OUTER                 |                   |     1 |   113 |  1532 |
    |  13 |     NESTED LOOPS                      |                   |     1 |   105 |  1531 |
    |  14 |      HASH JOIN                        |                   |     5 |   445 |  1524 |
    |  15 |       TABLE ACCESS FULL               | RTXNTYP           |     1 |    28 |     5 |
    |  16 |       TABLE ACCESS BY INDEX ROWID     | RTXN              |     1 |    22 |     1 |
    |  17 |        NESTED LOOPS                   |                   |   117 |  7137 |  1518 |
    |  18 |         VIEW                          | VW_SQ_1           | 20869 |   794K|  1441 |
    |  19 |          HASH GROUP BY                |                   | 20869 |   427K|  1441 |
    |  20 |           FILTER                      |                   |       |       |       |
    |  21 |            TABLE ACCESS BY INDEX ROWID| RTXNSTATHIST      | 20869 |   427K|  1304 |
    |  22 |             INDEX RANGE SCAN          | RTXNSTATHIST_DX2  | 20869 |       |    29 |
    |  23 |         INDEX RANGE SCAN              | PK_RTXN           |     1 |       |     1 |
    |  24 |          NESTED LOOPS                 |                   |     1 |    24 |     3 |
    |  25 |           TABLE ACCESS BY INDEX ROWID | FILEWAREHOUSE     |     1 |    14 |     2 |
    |  26 |            INDEX RANGE SCAN           | FILEWAREHOUSE_DX2 |     1 |       |     2 |
    |  27 |           TABLE ACCESS BY INDEX ROWID | EXTFILE           |     1 |    10 |     1 |
    |  28 |            INDEX UNIQUE SCAN          | PK_EXTFILE        |     1 |       |     1 |
    |  29 |      TABLE ACCESS BY INDEX ROWID      | RTXNSTATHIST      |     1 |    16 |     2 |
    |  30 |       INDEX RANGE SCAN                | PK_RTXNSTATHIST   |     1 |       |     1 |
    |  31 |     TABLE ACCESS BY INDEX ROWID       | NTWKNODE          |     1 |     8 |     1 |
    |  32 |      INDEX UNIQUE SCAN                | PK_NTWKNODE       |     1 |       |     1 |
    |  33 |    INDEX UNIQUE SCAN                  | PK_ORG            |     1 |     4 |     1 |
    |  34 |   TABLE ACCESS BY INDEX ROWID         | ACCT              |     1 |    18 |     1 |
    |  35 |    INDEX UNIQUE SCAN                  | PK_ACCT           |     1 |       |     1 |
    -------------------------------------------------------------------------------------------Edited by: user10754555 on Feb 5, 2010 6:02 PM

  • Default where with order by clause

    Hi all
    How to use Default where with order by clause
    for example i want to use Default_where clause with order by clause
    Regards
    Shahzaib ismail

    Hi,
    I have a similar query for where clause.
    my query-find form is a multi record form like below:
    Criteria - Condition - Value
    Item/Category/Planner (List_Item) - Equals/Among (List_Item) - text_FIELD
    Item - Equals - ITEM_1
    Category - Among - ('CAT1', 'CAT2')
    Planner - Equals - PL1
    Find_BUTTON Clear_BUTTON
    User can select any criteria and condition combination and then enter the value in "Value" text field.
    My query is: how can I prepare a where clause based on user input since the user can enter any number of values/combinations? I guess I would have to use some looping to generate where clause.
    Thanks
    Imran

  • Oracle query tuning : query with Order-by clause

    Hi
    I am having a query in my database :
    SELECT * FROM SAPR3.HRP1001 WHERE "MANDT" = 990
    ORDER BY
    "MANDT" , "OTYPE" , "OBJID" , "PLVAR" , "RSIGN" , "RELAT" , "ISTAT" , "PRIOX" , "BEGDA" , "ENDDA" ,"VARYF" , "SEQNR" ;
    Autotrace output is :
    Execution Plan
    0 SELECT STATEMENT Optimizer=CHOOSE (Cost=4649 Card=171895 Byt
    es=22862035)
    1 0 SORT (ORDER BY) (Cost=4649 Card=171895 Bytes=22862035)
    2 1 TABLE ACCESS (FULL) OF 'HRP1001' (Cost=1170 Card=171895
    Bytes=22862035)
    Statistics
    0 recursive calls
    5 db block gets
    12157 consistent gets
    11543 physical reads
    0 redo size
    38253080 bytes sent via SQL*Net to client
    376841 bytes received via SQL*Net from client
    34201 SQL*Net roundtrips to/from client
    0 sorts (memory)
    1 sorts (disk)
    512992 rows processed
    Since it is a issue with order by , it seems a PGA memory issue. there is 12GB PGA available but only 3GB gets allocated. pga_aggregate target is set in the DB. There is a index created for al the columns on order by, but it is not getting used.
    pleas suggest me as I am running into major problems, i can post the output of any query u require from my side. Any help wil be highly apprciated.
    Rishi

    > The query was alwasy spilling over to the One-Parse execution . It can be seen thru ST04N ->resource consumption-> sql work area trace.
    >
    > An undocumented oracle parameter smmmax_size was set which allowed for more usage of physical memory by single process and there was no spillover to the TEMP tablespaces.
    >
    > Also the File read time was analysed from Unix level ( From SAP thru ST04 ->filesystem wait s-> Avg rd (ms) and Ang writes (ms) which showed that reading from the File was not happening well. )
    Hi Rishi,
    the provided execution statistics prove the opposite:
    >Statistics
    >...
    >0 sorts (memory)
    > 1 sorts (disk)
    >512992 rows processed
    This indeed was a single-pass sort, which means it had to use the temp tablespace for one pass of the sorting/grouping.
    Remember that Oracle distinguishes three kinds of sorts: 1. "in memory", 2. "single-pass" and 3. "multi-pass".
    Only the first one won't need to spill out data to the disks. The others do this by definition.
    BTW: the file read times in ST04 are aquired through Oracle V$ views and not directly from the OS - that can make a big difference sometimes.
    regards,
    Lars

  • Sub query with order by

    Hi ,
    I have created a sub query with order by on a column.( i have cutomized the IKM control append to put order by).I can see the order by condition.If i use this subquery(yellow interface) in main query(is also yellow interface) i can't see the order by condition
    Subquery(Q-yellow interface):
    select
    PID,
         START_TIME,
         ACTION_TYPE_CODE
    FROM
    select      DISTINCT
         SERVICE_TRACKING_S.PID PID,
         TO_TIMESTAMP(TO_CHAR(SERVICE_TRACKING_S.ACTION_TIME,'DD-MON-YY HH24:MI:SS'),'DD-MON-YY HH24:MI:SS') START_TIME,
         SERVICE_TRACKING_S.ACTION_TYPE_CODE ACTION_TYPE_CODE
    from     KSTGDB.SERVICE_TRACKING_S SERVICE_TRACKING_S
    where          (1=1)     
    ORDER BY-----------------------------------------------
    PID
    ,START_TIME ASC
    ODI_GET_FROM
    Main query(Q1--yellow interface):
    select
    PID,
         START_TIME,
         ACTION_TYPE_CODE,
         RN,
         RN_MAX
    FROM (     
    select      
         Q.PID PID,
         Q.START_TIME START_TIME,
         CASE WHEN Q.START_TIME-LAG(Q.START_TIME,1,Q.START_TIME) OVER (PARTITION BY Q. PID ORDER BY Q.START_TIME)> numtodsinterval(75,'minute')
    or Q.PID!=LAG(Q.PID,1,0) OVER (PARTITION BY Q.PID ORDER BY Q.START_TIME) THEN 1 ELSE Q.ACTION_TYPE_CODE END ACTION_TYPE_CODE,
         ROW_NUMBER() OVER(PARTITION BY Q.PID ORDER BY Q.START_TIME) RN,
         count(*) over (PARTITION BY Q.PID ORDER BY Q.START_TIME ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING )
    RN_MAX
    from     (
    select      DISTINCT
         SERVICE_TRACKING_S.PID PID,     TO_TIMESTAMP(TO_CHAR(SERVICE_TRACKING_S.ACTION_TIME,'DD-MON-YY HH24:MI:SS'),'DD-MON-YY HH24:MI:SS') START_TIME,     SERVICE_TRACKING_S.ACTION_TYPE_CODE ACTION_TYPE_CODE
    from     KSTGDB.SERVICE_TRACKING_S SERVICE_TRACKING_S
    where     (1=1)
    ----------------- i don't see order by here--------------------------------
    ) Q
    where          (1=1)     
    ) ODI_GET_FROM
    thanks in advance
    K

    Hi,
    Add a new KM step with the SQL you want to use for the sub query and select the +"Use Current Command for Derived-Table sub-select statement"+ checkbox. This new step can be the last one of your IKM.
    Basically, you can copy the select statement of the "Insert new rows" step.
    Regards,
    JeromeFr

  • Prepared Statement with ORDER BY

    I am trying to use order by with prepared statement but it is not ordering.
    String sql = "SELECT * FROM MATERIAL WHERE (LOWER(NAMEE) LIKE ('%' || ? || '%') ORDER BY ? ";
    PreparedStatement ps=CM.getStatement(sql);
    ps.setString(1,p);
    ps.setString(2,sort);
              ResultSet r = ps.executeQuery();
    Can any one tell me how do I use prepared statement with order by

    You can not parameterize column names and such, only literals. You should build the ORDER BY clause dynamically.

  • Move non-partition to Partition with order by cluse

    Hi,
    I have non partition table of around 9GB and want to convert it into Partiiton table. Following is the des:
    SQL> select num_rows,blocks from dba_tables where table_name='DEMAND_DRAFT_STATUS';
      NUM_ROWS     BLOCKS
      21720123    1228647
    SQL> select index_name,index_type from dba_indexes where table_name='DEMAND_DRAFT_STATUS';
    INDEX_NAME                     INDEX_TYPE
    SYS_C0011138                   NORMAL
    IDX_DEMD_DRFT_STAT_INSERTED_BY BITMAP
    IDX_DEMD_DRFT_STAT_UPDATED_BY  BITMAP
    SQL> select clustering_factor from dba_indexes where index_name='SYS_C0011138';
    CLUSTERING_FACTOR
            387978241
    SQL> select column_name,column_position from dba_ind_columns where index_name='SYS_C0011138';
    COLUMN_NAME
    COLUMN_POSITION
    ISSU_BR_CODE
                  1
    ISSU_BANK_CODE
                  2
    ISSU_EXTN_CNTR_CODE
                  3
    COLUMN_NAME
    COLUMN_POSITION
    DD_NUM
                  4
    ISSUE_DATE
                  5
    DD_CRNCY_CODE
                  6
    COLUMN_NAME
    COLUMN_POSITION
    OT_TYPE
                  7
    PRODUCT_CODE
                  8
    CURRENCY_CODE
                  9
    9 rows selected.
    SQL> desc DEMAND_DRAFT_STATUS
    Name                                                                                                              Null?    Type
    ISSU_BR_CODE                                                                                                      NOT NULL VARCHAR2(6)
    ISSU_BANK_CODE                                                                                                    NOT NULL VARCHAR2(6)
    ISSU_EXTN_CNTR_CODE                                                                                               NOT NULL VARCHAR2(2)
    DD_NUM                                                                                                            NOT NULL VARCHAR2(16)
    ISSUE_DATE                                                                                                        NOT NULL DATE
    DD_CRNCY_CODE                                                                                                     NOT NULL VARCHAR2(3)
    OT_TYPE                                                                                                           NOT NULL VARCHAR2(2)
    PRODUCT_CODE                                                                                                      NOT NULL VARCHAR2(5)
    CURRENCY_CODE                                                                                                     NOT NULL VARCHAR2(3)
    DD_STATUS                                                                                                                  CHAR(1)
    DD_STATUS_DATE                                                                                                             DATE
    DD_AMT                                                                                                                     NUMBER(20,4)
    DD_REVAL_DATE                                                                                                              DATE
    PRNT_ADVC_FLG                                                                                                              CHAR(1)
    PRNT_RMKS                                                                                                                  VARCHAR2(50)
    PAYEE_BR_CODE                                                                                                              VARCHAR2(6)
    PAYEE_BANK_CODE                                                                                                            VARCHAR2(6)
    PAYING_BR_CODE                                                                                                             VARCHAR2(6)
    PAYING_BANK_CODE                                                                                                           VARCHAR2(6)
    ROUTING_BR_CODE                                                                                                            VARCHAR2(6)
    ROUTING_BANK_CODE                                                                                                          VARCHAR2(6)
    INSTRMNT_TYPE                                                                                                              VARCHAR2(6)
    INSTRMNT_ALPHA                                                                                                             VARCHAR2(6)
    INSTRMNT_NUM                                                                                                               VARCHAR2(16)
    PUR_NAME                                                                                                                   VARCHAR2(80)
    PAYEE_NAME                                                                                                                 VARCHAR2(80)
    PRNT_OPTN                                                                                                                  CHAR(1)
    PRNT_FLG                                                                                                                   CHAR(1)
    PRNT_CNT                                                                                                                   NUMBER(3)
    DUP_ISS_CNT                                                                                                                NUMBER(3)
    DUP_ISS_DATE                                                                                                               DATE
    RECTIFED_CNT                                                                                                               NUMBER(3)
    PAID_EX_ADVC                                                                                                               CHAR(1)
    ADVC_RCV_DATE                                                                                                              DATE
    BC_FLG                                                                                                                     CHAR(1)
    ENTERED_BY                                                                                                                 CHAR(1)
    CAUTIONED_STAT                                                                                                             CHAR(1)
    CAUTIONED_REASON                                                                                                           VARCHAR2(50)
    PAID_ADVC_FLG                                                                                                              CHAR(1)
    INVT_SRL_NUM                                                                                                               VARCHAR2(16)
    PRNT_REM_CNT                                                                                                               NUMBER(2)
    INSERTED_BY                                                                                                                NUMBER(10)
    UPDATED_BY                                                                                                                 NUMBER(10)
    INSERTED_ON                                                                                                                DATE
    UPDATED_ON                                                                                                                 DATE
    DEL_FLG                                                                                                                    CHAR(1)
    LCHG_TIME                                                                                                                  DATE
    RCRE_TIME                                                                                                                  DATE
    BUSINESS_DATE                                                                                                              DATEFollowing questions:
    1) I want to Range partition by ISSUE_DATE (there is no issue), but i'm thinking of reordering the column of index i.e 'SYS_C0011138' (which is also primary key). For example low cardinality columns i will place first and high selective column i would place at last in order. Is that a good idea?
    2) While creating Partition table i want to give insert /*+ parallel 10 */ into partitiontable select * from DEMAND_DRAFT_STATUS order by CURRENCY_CODE,DD_CRNCY_CODE,PRODUCT_CODE,OT_TYPE,ISSU_BANK_CODE,ISSU_EXTN_CNTR_CODE,ISSUE_DATE,ISSU_BR_CODE,DD_NUM;
    Why i'm doing this because index is going to be created on CURRENCY_CODE,DD_CRNCY_CODE,PRODUCT_CODE,OT_TYPE,ISSU_BANK_CODE,ISSU_EXTN_CNTR_CODE,ISSUE_DATE,ISSU_BR_CODE,DD_NUM columns so clustering factore os that index would be good(this is primary key index btw). Is it good?
    3) Once partition is done i want to compress the old partitions, but i'm bit worried that i wont get enough compress ratio if i create partition table with ORDER by clause. whats your thought?
    Please give recommendation
    Regards

    rp0428 wrote:
    >
    whats your thought?
    >
    Your post raises nothing but questions about WHY you are doing any of this. We can't suggest appropriate solutions without knowing what the problems are or what goals you are trying to achieve.
    >
    I have non partition table of around 9GB and want to convert it into Partiiton table
    >
    Ok - but WHY? Are you currently having problems? What are they? Is performance poor? For what type of queries or operations? Is management a problem - can't easily roll-off old data?
    >Well its management decision to partitions the table which are expected to have high size in future. There reference of 6GB i gave only for explanation, actually we have 2-3 tables which are 500+GB and will grow.
    Its a DWH environment where daily incremental load happens. So we came out of decision to have tables(which have daily incremental load) partitioned on monthly basis. And keep last quatat partitions on Cell Flash cache(its exadata)
    CLUSTERING_FACTOR
    387978241
    >
    Yep - that is a high factor for 1.2 million blocks. But are you sure it is correct? It's 17 times greater than the number of rows in the table. That suggests that to read the entire table using the index every block has to be read 17 times. That doesn't make sense - odds are the table and index stats you provided are not current.
    Yes thats seems to be correct figure. I'll check once again and post..
    >
    And it has this quote from Tom Kyte's book Expert Oracle Database Architecture
    >
    we could also view the clustering factor as a number that represents the number of logical i/o’s against the table, that would be performed to read the entire table via the index.
    >
    Even assuming that index has a high clustering factor the next question is:
    So what? That would only be relevant if you are using that index to access the entire table or large numbers of blocks.
    Is there a problem you are having that you can identify the clustering factor as the main culprit?
    If it ain't broke, don't fix it.
    >Yes that correct but clustering factor worry me much after seeing its value which is very high as compared to # of blocks.
    1) I want to Range partition by ISSUE_DATE (there is no issue), but i'm thinking of reordering the column of index i.e 'SYS_C0011138' (which is also primary key). For example low cardinality columns i will place first and high selective column i would place at last in order. Is that a good idea?
    >
    Range partitioning by a date can be useful so lets just assume it is in your case; maybe you are thinking about dropping old data. Is your primary key going to be global or local? That will affect performance if you drop old partitions; the global index has to be maintained but the local one doesn't.
    I'm not going to drop any partitions of old data as of now but will compress them because they wont anticipate DMLS.(but might be select)
    Now the question is about Primary key > wll it be Local of Global? Well i thought of having this Key as Local(because one of my primary key column will contain the partition key as well).
    Reasons
    1) better manageability( like if i want to compress old partitions, then having a gloabl parimary key index would become unusable state)
    2) Might increase in select search criteria (assumption)
    But having this as local will it impact any performance?
    The primary key does include the partitioning column so it could be a local index. But the partitioning column is not the leading column so this would be a local nonprefixed index. Is that what you need or do you need prefixed index?
    Have you researched the difference and determined which one you need for your use case?
    No. My partition Key would not be in leading column of Primary key. But is there any difference if i not keep partition key as leading column of primary key?
    See Local Partitioned Indexes in the VLDB and Partitioning Guide
    http://docs.oracle.com/cd/B28359_01/server.111/b32024/partition.htm
    Is rearranging the key columns a good idea? Hard to say without knowing your usage pattern.
    Why are you reordering the primary key columns? Why are you putting low cardinality columns first?Well i have read some where quite lot times that having Low cardinality columns as leading coluns would increase performance and later we can get better compression ratios. Let me know if i'm wrong.
    You shouldn't be doing either of these without a solid reason. What are the reasons? How do you know that, for your usage pattern, you won't make things worse?
    >
    2) While creating Partition table i want to give insert /*+ parallel 10 */ into partitiontable select * from DEMAND_DRAFT_STATUS order by CURRENCY_CODE,DD_CRNCY_CODE,PRODUCT_CODE,OT_TYPE,ISSU_BANK_CODE,ISSU_EXTN_CNTR_CODE,ISSUE_DATE,ISSU_BR_CODE,DD_NUM;
    Why i'm doing this because index is going to be created on CURRENCY_CODE,DD_CRNCY_CODE,PRODUCT_CODE,OT_TYPE,ISSU_BANK_CODE,ISSU_EXTN_CNTR_CODE,ISSUE_DATE,ISSU_BR_CODE,DD_NUM columns so clustering factore os that index would be good(this is primary key index btw). Is it good?
    >
    That should give a good initial clustering factor for the index unless a lot of DML is done.
    >
    3) Once partition is done i want to compress the old partitions, but i'm bit worried that i wont get enough compress ratio if i create partition table with ORDER by clause. whats your thought?
    >
    You are probably correct since having the low cardinality data first will break things up more. But you are still trying to solve a problem that you don't know even exists. How much space will each partition take if uncompressed? How much if compressed?
    At a minimum you should perform a tests for a realistic subset of data (a sample issue date).
    1. Create an unordered uncompressed table (new tablespace to make size data more accurate).
    2. Create an unordered compressed table
    3. Create an ordered uncompressed table
    4. Create an ordered compressed table
    Yes i have tries to simulate this for 9GB table(only) and size of unordered compressed table is 1.2GB and size of ordered compressed table is 1.9GB out of 9B(uncompressed)
    Then you will have a good idea what the space savings might be.
    I suggest you first document the issues and problems you are having and list specific goals for any architecture changes.
    For partiioning I would focus on identifying the goals, the partition key(s), and the numbers and types (global or local) for the primary key and other indexes.
    Application team have already identified the Partitions key on tables which are supposed to be partitioned And they say most of the queries are on those date columns. Secondly as i explained having partition we will keep old partition in compressed state and new partitions in Cell flash cache.
    For the primary key index (that includes the partitioning key) determine if you need a prefixed or nonprefixed index.
    again whats the difference if i dont have partition key column in leading column of index?
    Based on your usage patterns determine the indexes and columns that are the most important for querying. The clustering factor might be ideal for the primary key but totally screwed up for the indexes and queries that really count. You can't get an ideal clustering factor for every index.
    The data order for the initial load can be in the order that gives you the best clustering factor for the main index you need it for. That assumes that you actually need the index for a purpose that the clustering factor can help with.Thanks a lot your explanation was helpful.
    This setup for tables (i.e. having high # of columns in primary key for example 6-7 out of 15 columns approx in a table) is common for mostly all of the tables which are going to be partition.
    And most of the columns in parimary key have 2-3 distinct values out of 1Million rows, so was about to think that i should put them ahead while creating a new primary key (Local) on newly created partition.
    While doing this i also take into consideration that i load the data in ORDER BY clause (i.e. order by all columns of PK) to get excellent CF and moreover we could benefit from Storage Index on Exadata. and thanks for inputs too highly appreciated

  • Analytical function count(*) with order by Rows unbounded preceding

    Hi
    I have query about analytical function count(*) with order by (col) ROWS unbounded preceding.
    If i removed order by rows unbouned preceding then it behaves some other way.
    Can anybody tell me what is the impact of order by ROWS unbounded preceding with count(*) analytical function?
    Please help me and thanks in advance.

    Sweety,
    CURRENT ROW is the default behaviour of the analytical function if no windowing clause is provided. So if you are giving ROWS UNBOUNDED PRECEDING, It basically means that you want to compute COUNT(*) from the beginning of the window to the current ROW. In other words, the use of ROWS UNBOUNDED PRECEDING is to implicitly indicate the end of the window is the current row
    The beginning of the window of a result set will depend on how you have defined your partition by clause in the analytical function.
    If you specify ROWS 2 preceding, then it will calculate COUNT(*) from 2 ROWS prior to the current row. It is a physical offset.
    Regards,
    Message was edited by:
    henryswift

  • Performance issue with ORDER BY

    The following query runs in 60 milli sec with no order by clause. With order by it takes about 3 sec. The end result of the query is 25 records. One thing we noticed is order by is taking place after the union irrespective where we place the order by clause. Not sure how to make oracle perform order by on end result. Any thoughts truly appreciated.
    SELECT *
    FROM
    (SELECT ROWNUM as R,
    STATUS,
    TASK_ID,
    PRIORITY,
    INSTALL_TYPE,
    CUSTOMER_TYPE,
    TYPE_ID,
    QUEUE,
    FALLOUTREASON,
    START_DT,
    NOTIF_CREATE_DT,
    ACCT_NUM,
    ADSLTN,
    SDPID,
    PERFORMER_DN,
    SUPERVISOR_DN
    FROM
    (SELECT *
    FROM
    (SELECT *
    FROM
    (SELECT t.STATUS,
    TASK_ID,
    PRIORITY,
    INSTALL_TYPE,
    CUSTOMER_TYPE,
    S.TYPE_ID,
    QUEUE,
    null FALLOUTREASON,
    START_DT,
    null NOTIF_CREATE_DT,
    null ACCT_NUM,
    ADSLTN,
    null SDPID,
    PERFORMER_DN,
    SUPERVISOR_DN
    FROM (select * from TASK_ACTIVITY where TYPE_ID='2' AND SUB_TYPE_ID='2' AND status not in ( 'Terminated','Complete') ) T,
    SOEG_CRM S
    WHERE t.activity_id= s.activity_id
    UNION ALL
    (SELECT t.STATUS,
    TASK_ID,
    PRIORITY,
    null INSTALL_TYPE,
    null CUSTOMER_TYPE,
    TYPE_ID,
    QUEUE,
    'Review Letters for Printing' FALLOUT_REASON,
    START_DT,
    null NOTIF_CREATE_DT,
    null ACCT_NUM,
    null ADSLTN,
    null SDPID,
    PERFORMER_DN,
    SUPERVISOR_DN
    FROM (select * from TASK_ACTIVITY where TYPE_ID='3' AND status not in ( 'Terminated','Complete') ) T
    UNION ALL
    (SELECT t.STATUS,
    TASK_ID,
    PRIORITY,
    INSTALL_TYPE,
    CUSTOMER_TYPE,
    op.TYPE_ID,
    QUEUE,
    FALLOUT_REASON,
    START_DT,
    NOTIF_CREATE_DT,
    ACCT_NUM,
    ADSLTN,
    SDPID,
    PERFORMER_DN,
    SUPERVISOR_DN
    FROM (select * from TASK_ACTIVITY where TYPE_ID='1' AND SUB_TYPE_ID='1' and status not in ( 'Terminated','Complete') ) T,
    OPNOTES OP
    WHERE t.activity_id= op.activity_id
    UNION ALL
    (SELECT t.STATUS,
    TASK_ID,
    PRIORITY,
    INSTALL_TYPE,
    CUSTOMER_TYPE,
    a.TYPE_ID,
    QUEUE,
    null FALLOUTREASON,
    START_DT,
    null NOTIF_CREATE_DT,
    null ACCT_NUM ,
    ADSLTN,
    null SDPID,
    PERFORMER_DN,
    SUPERVISOR_DN
    FROM (select * from TASK_ACTIVITY where TYPE_ID='2' AND SUB_TYPE_ID='1' and status not in ( 'Terminated','Complete') ) T,
    AELERA a
    WHERE t.activity_id= a.activity_id
    WHERE (PERFORMER_DN = 'cadmin1'
    OR SUPERVISOR_DN in ('Address_Issues','PROCESSED','SG_EOSN','Test_Queue','Test_Queue_P','BUTST1','BUTST2','BU_UNAS','DATAR','PST','PST_ME','SVC','SVC_EX','SVC_ME','TEST_2_LETTER_QUEUE','TEST_2_OP_ESC_QUEUE','TEST_2_PROV_ESC_QUEUE','TEST_3','Test_4','Test_6','Test_8','test_5_LETTER_QUEUE','test_5_OP_ESC_QUEUE','test_5_PROV_ESC_QUEUE','cadmin1'))
    AND ((STATUS = 'Pending'
    AND PERFORMER_DN = QUEUE)
    OR STATUS = 'Suspended'
    OR STATUS = 'Acquired'
    OR (STATUS = 'Pending'
    AND PERFORMER_DN <> QUEUE))
    AND (TYPE_ID = '2'
    OR TYPE_ID = '3')
    --order by priority
    WHERE R >= 1
    and R < 26
              --order by priority 
    Here is the explain plan
    19 SELECT STATEMENT
    18 . VIEW
    17 SORT [ORDER BY]
    16 COUNT
    15 . VIEW
    14 UNION-ALL
    4 NESTED LOOPS
    1 APPL.TASK_ACTIVITY TABLE ACCESS [FULL]
    3 APPL.SOEG_CRM TABLE ACCESS [BY INDEX ROWID]
    2 APPL.XPK_SOEG_CRM INDEX [UNIQUE SCAN]
    5 APPL.TASK_ACTIVITY TABLE ACCESS [FULL]
    9 NESTED LOOPS
    6 APPL.BSOMS_OPNOTES TABLE ACCESS [FULL]
    8 APPL.TASK_ACTIVITY TABLE ACCESS [BY INDEX ROWID]
    7 APPL.XPKTASK_ACTIVITY INDEX [UNIQUE SCAN]
    13 NESTED LOOPS
    10 APPL.TASK_ACTIVITY TABLE ACCESS [FULL]
    12 APPL.AELERA TABLE ACCESS [BY INDEX ROWID]
    11 APPL.XPK_AELERA INDEX [UNIQUE SCAN]

    Hi,
    I must admit, that I didn't read your whole query, but did you try to use a subselect?
    Select a.* from (your_query) a
    order by a.priorityBTW: Don't use SELECT *
    Dim

  • Re-using a function with minor changes to load sub Nav menu

    Hi.
    I have a function that loads xml and uses its to create a
    main navigation menu. When an object is selected the movie detects
    which button has been selected and traces the label for that
    button. I need to take this a step further and have a
    sub-navigation menu load when the "Home2" button is selected. As
    you can see from the comments in the code with 2 minor changes to
    the createBoxes function, the sub-nav menu can be loaded instead of
    the main nav menu. My problem is this, I need to main nav menu is
    be present at all times, I only need the sub nav to load when the
    Home 2 button is pressed and I somehow need to reuse the
    createBoxes function with the changes stated in the the comments in
    order to load the sub nav menu when required. I want to reuse this
    function (if possible) instead of duplicating the code in a new
    function.
    If anyone can show me how this is done I would be very
    greatful.
    Thanks
    Barry.

    basically the submenu will look exactly the same as the main
    menu, only it will be positioned to the right of it when the home2
    button is pressed. I then need it to unload when any other button
    is pressed. By changing the following lines in the current
    createBoxes function the submenu loads as I would like. I am just
    wondering if it is possible to pass in a couple of vars to change
    the lines of code as detailed below, depending on whether the home2
    button or one of the other mainn nav buttons is pressed.
    The code that when changed loads the submenu:
    In function createBoxes change var il:XMLList =
    xml.navigation.main_navigation; to var il:XMLList =
    xml.navigation.main_navigation.sub_navigation; . This loads the sub
    nav instead of the main nav but in the same position as the main
    nav.
    by also changing box.x = 100; to box.x = 260; in the function
    createBoxes the sub nav menu loads in the position I would like it
    to be.
    Now I am wondering if changing the code to something like the
    following (see attached code) could this work?
    Barry.

Maybe you are looking for

  • Which version of Windows (XP, Vista, 7?) is best for MacBook 2.2Ghz Intel Core 2 Duo, OS 10.6.8 when using boot camp?

    Hi all, I am about to install boot camp, and I was wondering which version of Windows works best for my Mac. Many thanks for your help. Francesco

  • Adding Z-Tran in SAP Menu

    Hi All, How can I add a Z-Transaction in SAP Easy Access, SAP Menu -> Accounting -> Financial Accounting -> General Ledger ->Document Entry. I want to enter my Z-Transaction here. How can I do this? I am trying with se43 (S000 & S000_RW). But I cont

  • Jco topics

    hi all i want learn more about jco topics.plz help me thanks hareesh

  • E90 Gallery problem.

    Hi all, Having a problem with gallery on my E90. Am unable to see any of the photos I take on the phone. Can take and save photos fine, but the only way I can review them is by connecting up to PC Suite and "exploring" the memory card. Gallery report

  • HR posting to Accounting

    Hi there, I am working a implementation project........we have sucessfully run the payroll and posting to accounting is also done correctly and doucment number is also generated with GL ledgers Problem is Finance guys could not see that documnet... e