Query in Order By clause used with UNION

When tryna execute the below qurey I get
ORA-01784 - ORDER BY item must be the number of a SELECT-list expression
Please help me resolve this asap.
SELECT cm_workqueue.WQUEUEID_REF,cm_workqueue.WQUEUEID_CODE,cm_workqueue.mstr_queue_encode,cm_workqueue.desc_text
FROM cm_workqueue,
cm_wkqueue_role,
atrt_role
WHERE cm_workqueue.MSTR_QUEUE_ENCODE = 10000000
AND cm_workqueue.WQUEUEID_REF = cm_wkqueue_role.WQUEUEID_REF
AND atrt_role.ROLEID_REF = cm_wkqueue_role.roleid_ref
AND cm_wkqueue_role.REFERRAL_YORN_ENCODE = 10000000
AND cm_workqueue.STATUS_REF = 10000000
AND cm_wkqueue_role.STATUS_REF = 10000000
UNION
SELECT cm_workqueue.WQUEUEID_REF,cm_workqueue.WQUEUEID_CODE,cm_workqueue.mstr_queue_encode,cm_workqueue.desc_text
FROM cm_workqueue,
cm_wkqueue_role,
atrt_role
WHERE cm_workqueue.mstr_queue_encode = 10000001
AND cm_workqueue.status_ref = 10000000
AND cm_wkqueue_role.wqurolid_ref = 1
AND EXISTS (
SELECT 1
FROM cm_filterset f,
cm_wkqueue_role cmr
WHERE f.wqueueid_ref = cm_workqueue.wqueueid_ref
AND f.status_ref = 10000000
AND cmr.roleid_ref = atrt_role.roleid_ref
AND cmr.wqueueid_ref = f.mstrquid_ref
AND cmr.referral_yorn_encode = 10000000
AND cmr.status_ref = 10000000)
ORDER BY CASE
WHEN cm_workqueue.wqueueid_ref < 999 THEN 1
ELSE cm_workqueue.mstr_queue_encode
END,
LOWER (cm_workqueue.desc_text)

To be correct, the column names in the order by must be the name of the column alias or the number of the selected column. If you don't give a column alias, the column name of the first select is taken.
SQL> create table t1 (a varchar2(1));
Table created.
SQL> create table t2 (aa varchar2(1));
Table created.
SQL> select a aa from t1
  2  union
  3  select aa from t2
  4  order by aa;
no rows selectedDid you see how we nicely could order by the column name of the second select? NOT! It's because of the column alias. Look at the next select
SQL> select a aa from t1
  2  union
  3  select aa from t2
  4  order by a;
order by a
ERROR at line 4:
ORA-00904: "A": invalid identifierDid you see, that the column name of the first select is not valid as there is a column alias?
That's why the best is, to use result set numbers when ordering in unions
SQL> select a aa from t1
  2  union
  3  select aa from t2
  4  order by 1;
no rows selected
SQL> Edited by: Leo Mannhart on Mar 6, 2009 10:37 AM
And that's why the columns in the order by MUST be in the select list. Another error in the original post

Similar Messages

  • How to query which Tcodes specified user used with one day?

    Hi guys:
      How to query which Tcodes specified user used with one day?

    Hi
    You can use transaction code ST03N.
    1. Go to tx code - ST03N
    2. Under "Workload" you can select your "Instance or Total" so that it expands and shows you Days, Week, Month.
    3. If you want to look at the transactions executed for a particular day, lets say, then double click on any day.
    4. At the bottom left you get "Analysis Views"
    5. Select & expand "User and Settlement Statistics"
    6. Double click on "User Profile"
    7. On the right side of the window you get a list of all the users
    8. Double click on a particular user you want to view the details of.
    9. The new screen will display the "Transactions/Reports Utilized by User XXX"
    If you wanna track which users executed a particular transacation then follow this:
    10. In "Analysis Views" expand "Transaction Profile"
    11. Double click on "Standard"
    You can view the list of Transactions and Reports.
    12. Double click on the Tx Code or Report you wanna check and it will show the Use of it.
    This will help you.
    Regards
    Sumit Jain
    [reward with points if the answer is useful]

  • Order by clause using decode function

    Hi everybody,
    i need below order in my report.
    Connecticut
    greenwich
    stamford
    bridgeport
    New York
    NYC
    wrestcher
    byram
    Georgia
    atlanta
    athens
    oconny
    first i need above order in my view out put.
    so in order by clause i used first decode function for State ordering
    and in second decode function for city ordering.
    i do not need order by ascending or descending.
    so pls anybody can help me.
    any help is greatly appreciated.
    thanks.

    add asc after the decode. default is desc

  • Order by decode statement with Union all

    Hi
    I use Database Version: Oracle 9.2.0.1.0
    OCI version 9.2
    I try to use order by decode while i have a select base on union and I get en error:
    ORA-01785: ORDER BY item must be the number of a SELECT-list expression
    Here is my code:
    select catalog_type, catalog_id, item_id, item_name
    from items_catalog
    union all
    select catalog_type, catalog_id, organisation_unit_id, organisation_unit_name
    from units_catalog
    order by decode(catalog_type, 1, item_id, organisation_unit_id)
    If I use the decode on a select statement without union, it works.
    What I have to do in order to make it work on union all select?
    Tnx

    Hi,
    Here's a slightly different way:
    COLUMN     sort_key   NOPRINT
    VARIABLE     catalog_type_wanted     VARCHAR2 (5)
    EXEC         :catalog_type_wanted := '4'; 
    SELECT       catalog_id, item_id, item_name
    ,       CASE
               WHEN  :catalog_type_wanted = '1'  THEN  '1'
                                               ELSE  '2'
           END || item_id          AS sort_key
    FROM       items_catalog
          UNION ALL
    SELECT    catalog_id, organisation_unit_id, organisation_unit_name
    ,       CASE
               WHEN  :catalog_type_wanted = '1'  THEN  '2'
                                               ELSE  '1'
           END || organisation_unit_name     AS sort_key
    FROM       units_catalog
    ORDER BY  sort_key
    ;This way guarantees that (for example) 'SHOP50' comes before 'SHOP58', whether or not they are in the first half of the output or the second half.
    Notice that this uses the SQL*Plus COLUMN ... NOPRINT command to hide the sort_key column.
    If you're not using SQL*Plus, then whatever front end you are using may have a similar feature.
    If not, then you can do the UNION in a sub-query, as Daniel did, but do the ORDER BY in the main query (only).

  • Order By clause problem with ENAME and Rownum?

    From Scott/Tiger user I run this query from EMP table.
    select rownum,EMPNO,ENAME,JOB from emp
    order by Ename;
    Output is like this
    ROWNUM EMPNO ENAME JOB
    11      11     ADAMS CLERK
    2      2      ALLEN SALESMAN
    6      6      BLAKE MANAGER
    7      7      CLARK MANAGER
    13      13     FORD ANALYST
    12      12     JAMES CLERK
    4      4      JONES MANAGER
    9      9      KING PRESIDENT
    5      5      MARTIN SALESMAN
    14      14      MILLER CLERK
    8      8      SCOTT ANALYST
    1      1      SMITH CLERK
    10      10      TURNER SALESMAN
    3      3      WARD SALESMAN
    Here Rownum order is disturb, I want it also in Ascending order with ENAME.
    Please send the solution of this problem
    Best Regards,
    Shahzad

    select row_number() over (order by ename) as rn, empno, ename, job from emp
      order by ename;

  • ROWNUM can use with ORDER BY clause?

    Hi
    Following i have two quires in ROWNUM order for example..
    1:query
    select rownum,empno,ename,deptno from emp
    where rownum <= 14;
    result:
    rownum empno ename deptno
    1 7369 SMITH 20
    2 7499 ALLEN 30
    3 7521 WARD 30
    4 7566 JONES 20
    5 7654 MARTIN 30
    6 7698 BLAKE 30
    7 7782 CLARK 10
    8 7788 SCOTT 20
    9 7839 KING 10
    10 7844 TURNER 30
    11 7876 ADAMS 20
    12 7900 JAMES 30
    13 7902 FORD 20
    14 7934 MILLER 10
    2:Query , add ORDER BY clause it will change the sequence of ROWNUM.
    select rownum,empno,ename,deptno from emp
    where rownum <= 14
    order by deptno;
    result:
    rownum empno ename deptno
    7 7782 CLARK 10
    9 7839 KING 10
    14 7934 MILLER 10
    1 7369 SMITH 20
    11 7876 ADAMS 20
    13 7902 FORD 20
    8 7788 SCOTT 20
    and so on......
    but i want these records from 2 to 5...how i can query this through ROWNUM or any sequence because ORDER BY clause change the order of ROWNUM and i could get this result.
    rownum empno ename deptno
    9 7839 KING 10
    14 7934 MILLER 10
    1 7369 SMITH 20
    11 7876 ADAMS 20
    please send me general query which i can apply on any table.
    advance thanks.

    My guess is that you are looking for a pagination solution. If so, here is a thread on Asktom:
    http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:127412348064
    So, essentially, you ORDER BY first and then write a wrapper to get your the rownum.
    If you do it this way, You will also use the top-n optimization that Oracle uses.
    Rahul.

  • How to tack print out use with Query ?

    Hi
    I have to tack purchase order print out (print details- PO date, BP code, name, item name, Quantity and Amount . System Print Layout means from Purchase Order layout designer) use with Query
    How to tack print out use with Query (system print layout means from  Purchase Order layout designer) ?
    Regards
    Aravind M

    Hi
    I have to take purchase order print out and I created 3 templates for that purchase order in layout designer (Because as per the MIS requirement I created 3 templates, its cover more information). I have to take print out 3 templates at a time. So i try to take print use with Query.
    Regards
    Aravind M

  • Materialized view and Order by clause

    Hi all. I'd like to have some information regarding the order by clause used in a materialized view.
    I'm using Oracle 9.2 and Win2003 server. I have a common view (my_common_view) on which a materialized view is based (my_materialized_view). Materialized view is built for fast refresh and is created in this way:
    create materialized view my_materialized_view as
    select * from my_common_view
    order by 1,2,3;
    My question is: if I query this materialized view without adding an order by clause in the statement, I will obtain always an ordered result set? So, is it useful to include the order by clause in the materialized view script or the order by has to be include in the statement used for quering the materialized view (for example like this: select * from my_materialized view order by ...)?
    Thank you very much.
    Ste.

    SQL> create table t
      2  (x int)
      3  /
    Table created.
    SQL> create view vw
      2  as
      3  select *
      4    from t
      5   order by x
      6  /
    View created.
    SQL> select *
      2    from v$version
      3  /
    BANNER
    Oracle9i Enterprise Edition Release 9.2.0.7.0 - 64bit Production
    PL/SQL Release 9.2.0.7.0 - Production
    CORE    9.2.0.7.0       Production
    TNS for IBM/AIX RISC System/6000: Version 9.2.0.7.0 - Production
    NLSRTL Version 9.2.0.7.0 - Production
    SQL>

  • Entity beans, order by clause ..

    I know its 1.4, but is there an ETA on these goodies, i need them sooooo bad...
    thx.

    Can you try the following and update whether it works,
    Get the VO's order by clause using the getOrderByClause api, then set the order by clause to null and then set your where clause and finally set back the order by clause.
    Not sure whether such a workaround is necessary. Will check and update later.

  • Order by clause with union

    Hi,
    I wanted to know if this is possible with order by clause and union. some how union messes up the ordering of the left part of the union statement.
    assume the data is as below in table tab1
    c1 c2
    4 1
    4 2
    5 1
    5 2
    7 1
    7 2
    8 3
    9 4
    the expected output must be for all c1 < 7 ordering must be on c1 and for all c1 > 7 ordering must be on c2.
    so the query i tried was
    select c1,c2 from tab1 where c1 <7 order by c1
    union
    select c1,c2 from tab1 where c1 >=7 order by c2
    it is giving the message 'Sql command not properly ended'.
    Suggestions are welcome

    SQL>
    SQL> With t As
      2  (
      3  Select 4 c1, 1 c2 From dual Union All
      4  Select 4 ,2       From dual Union All
      5  Select 5 ,1       From dual Union All
      6  Select 5 ,2       From dual Union All
      7  Select 7 ,1       From dual Union All
      8  Select 7 ,2       From dual Union All
      9  Select 8 ,3       From dual Union All
    10  Select 9 ,4       From dual
    11  ), t1 As (Select c1,c2 From t Where c1 <7 Order By c1)
    12  ,t2 As (select c1,c2 from t where c1 >=7 Order By c2)
    13  Select * From t1 Union All
    14  Select * From t2;
            C1         C2
             4          1
             4          2
             5          1
             5          2
             7          1
             7          2
             8          3
             9          4
    8 rows selected
    SQL> /*-- Union All
      2   select c1,c2 from t where c1 >=7
      3   Order By c1,c2;
      4  */
      5 
    with union clause order by must be placed in the end,,
    select c1 from dual union all
    select c2 from dual
    order by  c1

  • 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

  • Where to place ORDER BY clause when a query includes UNION

    I have a query that has UNION in it. could you please tell me where to place the ORDER BY clause. Because it's throwing an error if i place the ORDER BY clause at the end

    Because you are using the UNION set operator, you need to either specifically list your columns or use positional notation.
    Without a set operator we can order by the column name without specifically listing it:
    SQL> select * from dual
      2  order by dummy;
    D
    X
    1 row selected.This doesn't work once you introduce a set operator:
    SQL> ed
    Wrote file afiedt.buf
      1  select * from dual union all
      2  select * from dual union all
      3  select * from dual
      4* order by dummy
    SQL> /
    order by dummy
    ERROR at line 4:
    ORA-00904: "DUMMY": invalid identifierSo you need to either use positional notation:
    SQL> ed
    Wrote file afiedt.buf
      1  select * from dual union all
      2  select * from dual union all
      3  select * from dual
      4* order by 1
    SQL> /
    D
    X
    X
    X
    3 rows selected.Or, specifically list or alias the columns you are projecting:
    SQL> ed
    Wrote file afiedt.buf
      1  select dummy from dual union all
      2  select dummy from dual union all
      3  select dummy from dual
      4* order by dummy
    SQL> /
    D
    X
    X
    X
    3 rows selected.
    SQL> ed
    Wrote file afiedt.buf
      1  select dummy as d from dual union all
      2  select dummy as d from dual union all
      3  select dummy as d from dual
      4* order by d
    SQL> /
    D
    X
    X
    X
    3 rows selected.cheers,
    Anthony

  • Select query with UNION clause in database adapter

    Friends,
    I have got a SQL query with two UNION clause withing it. like
    select a,b,c
    from a
    union
    select a,b,c
    from b
    The schema generated is like below in sequence
    <element>a</element>
    <element>b</element>
    <element>c</element>
    <element>a</element>
    <element>b</element>
    <element>c</element>
    So, the columns from different select queries joined with UNION clause are all appeared in schema instead of the distinct columns.
    Is there any way around to solve this issue ? or will need to with DB function/procedure.

    I think I know what you are saying but your example doesn't make sense, your SQL should produce something like
    I had to change a, b, c with elementA, elementB, elementC as a and b are reserved html tags.
    <elementA>DateA</elementA>
    <elementB>DataB</elementB>
    <elementC>DataC</elementC>
    ...What is the result of the query when you run it in SQLPlus? Is it what you expect?
    cheers
    James

  • EJB finder query with ORDER BY clause

    Hi,
    How to query the data in ordered list using Order By clause in the entity Bean.
    Here is the part of My code in which i am interrested.
    /* @ejbgen:finder
    * signature = "Collection findAllData()"
    * ejb-ql = "SELECT OBJECT(z) FROM DataProfileBean AS z"
    abstract public class DataProfileBean extends EntityAdapter {
    * @ejbgen:cmp-field column = DATA_PREFIX
    * @ejbgen:primkey-field
    * @ejbgen:local-method transaction-attribute = Required
    public abstract Integer getDataPrefix();
    public abstract void setDataPrefix(Integer DataPrefix);
    Now what modification needs to be done in the ejb-ql query to find the DataPrefix in the sorted list.
    Regards,
    Dilip

    I don't think the current spec for EJBs support ORDER BY. But Weblogic has an extension that you can use (that is, if you're using it). Here's the info: http://e-docs.bea.com/wls/docs61/ejb/cmp.html#1076421

  • Order by clause with Named Query

    hi
    i have to give order by clause in Named Query
    how we have to specify is can any body help
    thanks
    Harish

    Assuming an Entity called Handset:
    select h from Handset h order by h.description

Maybe you are looking for

  • Message log for bapi_alm_order_maintain

    Hi All, Iam getting wbs element and start date from xi proxy as input. Using bapi bapi_alm_order_maintain i need to update basic start date in iw32 . By this bapi the date had been getting updated. Now for example iam getting 10 wbs element from xi 

  • Was told not to report off transaction planning cubes

    Hello there, we recently implemented Hyperion Planning and have 3 Essbase cubes: Planning, Capex, and workforce. We want to hook OBIEE up to the cubes for reporting. We have been told it is not best practice to report off the same cubes we're transac

  • WRT54GS w/Windows Vista Slow Download

    I have a WRT54GS wireless router with 2 dell laptops connected. One laptop runs fine, the other (brand new Dell D420 running Windows Vista Business 32bit) has EXTREMLY slow downloads. I have covered all the bases I can think of: - replaced Dell Wirel

  • Modifying a Selection in PSE4

    I've made a selection that is almost perfect. There are 3 small areas that must be removed from the selection and I am not having much luck removing them with the selection brush. Perhaps the areas are too small. Marilyn Lee in a previous post said t

  • What video formats are compatible with iMove '11

    I want to be able to know what video formats are compatible with iMovie '11. Not how to convert them, just a simple list of video formats I can use.