Doubt in order by clause

Hi,
I have the below query
select distinct first_value(column_name) over (partition by table_name order by decode(column_name,'REP','1',
'END','2','3')),table_name from all_tab_columns where owner='ORCL'
can someone plz tell me how the order by will function here or how the result will be ordered.
Thanks,
Mat

Its better you post your original query with the actual and expected output..
The syntex
decode(column_name,REPORTING_DATE ,'1',END_DATE,'2','3'...
is not correct for all_tab_cols as teh column names are not enclosed with single quotes.
Regards
Arun

Similar Messages

  • Order By Clause in View

    Hi,
    I have a doubt regarding Order By Clause in Views.
    As per my knowledge, we can't put an order by clause in the subquery that defines view. But when i created a view in Oracle 9i with the order by clause, view got created.
    Please see the my view code below :
    create or replace view testview
    as select * from employees
    order by 1,2;
    Could anyone please confirm that we can have order by clause in Views in oracle 9i?
    Thanks,
    Tandra

    According to the SQL Reference doc, there is no such restriction for a non-updatable view:
    The view subquery cannot select the CURRVAL or NEXTVAL pseudocolumns.
    If the view subquery selects the ROWID, ROWNUM, or LEVEL pseudocolumns, those columns must have aliases in the view subquery.
    If the view subquery uses an asterisk (*) to select all columns of a table, and you later add new columns to the table, the view will not contain those columns until you re-create the view by issuing a CREATE OR REPLACE VIEW statement.
    For object views, the number of elements in the view subquery select list must be the same as the number of top-level attributes for the object type. The datatype of each of the selecting elements must be the same as the corresponding top-level attribute.
    You cannot specify the SAMPLE clause.
    This restriction exists only for updatable views:
    If you want the view to be inherently updatable, it must not contain any of the following constructs:
    A set operator
    A DISTINCT operator
    An aggregate or analytic function
    A GROUP BY, ORDER BY, CONNECT BY, or START WITH clause
    A collection expression in a SELECT list
    A subquery in a SELECT list
    Joins (with some exceptions as described in the paragraphs that follow).

  • Doubt regarding multiple criteria in order by clause

    Hi, I don't understand the effect of multiple elements inside the order by clause. I have the following example table:
    factor_x | factor_y | price
    =====================
    1 | 5 | 1000
    2 | 4 | 6970
    3 | 3 | 3688
    4 | 2 | 9087
    5 | 1 | 10000
    =====================
    So I tried: select price from pricetable order by factor_x; results as follows:
    1000
    6970
    3688
    9087
    10000
    Then: select price from pricetable order by factor_y; results as follows:
    10000
    9087
    3688
    6970
    1000
    Then: select price from pricetable order by factor_x, factor_y; results as follows:
    1000
    6970
    3688
    9087
    10000
    which is same as using order by factor_x. Can anyone tells me what is the effect of adding a 2nd, 3rd..... criterion in the order by clause? Because in this example I cannot see the difference. Many thanks.

    Hi,
    I did a little change in your data. Hope it will help you to understand.
    SQL> WITH T AS (SELECT 1 X , 1 Y , 1000 PRICE FROM DUAL UNION A
      2  SELECT 2 , 4 , 6970 FROM DUAL UNION ALL
      3  SELECT 4 , 3 , 3688 FROM DUAL UNION ALL
      4  SELECT 4 , 2 , 9087 FROM DUAL UNION ALL
      5  SELECT 4 , 5 , 10000 FROM DUAL)
      6  SELECT PRICE,X,Y FROM T ORDER BY X;
         PRICE          X          Y
          1000          1          1
          6970          2          4
          9087          4          2
         10000          4          5
          3688          4          3
    SQL> WITH T AS (SELECT 1 X , 1 Y , 1000 PRICE FROM DUAL UNION A
      2  SELECT 2 , 4 , 6970 FROM DUAL UNION ALL
      3  SELECT 4 , 3 , 3688 FROM DUAL UNION ALL
      4  SELECT 4 , 2 , 9087 FROM DUAL UNION ALL
      5  SELECT 4 , 5 , 10000 FROM DUAL)
      6  SELECT PRICE,X,Y FROM T ORDER BY X,Y;
         PRICE          X          Y
          1000          1          1
          6970          2          4
    9087 4 2
    3688 4 3
    10000 4 5
    SQL>Regards
    Avinash

  • 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                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

  • ORDER BY clause with expression

    Considering that all the column names and table name is valid, what happens internally when the following query is run?
    SQL> SELECT STU_ID, STU_NAME FROM STUDENT ORDER BY 2+3, STU_ID;
    What i found out was the rows get ordered in ascending order of STU_ID. It's quite clear that it is ignoring the expressing 2+3, so my doubts are:
    (a) Is it appending any pseudo-column to the table with 2+3 getting evaluated to 5?
    (b) If there exist a 5th column in the table student and we specify it in the select statement's select list (5 column names) then why its not considering it as 5 and sorting the data according that?
    (c) Is it true that, any expression we write is getting evaluated to NULL? Then is it that ORDER BY NULL evaluates to no ordering and the parser searches for next column-name or position (if specified) ?
    (d) if neither then, what's the reason of the ignorance?

    Hi,
    Welcome to the forum!
    987236 wrote:
    Considering that all the column names and table name is valid, what happens internally when the following query is run?
    SQL> SELECT STU_ID, STU_NAME FROM STUDENT ORDER BY 2+3, STU_ID;
    What i found out was the rows get ordered in ascending order of STU_ID. It's quite clear that it is ignoring the expressing 2+3, so my doubts are:
    (a) Is it appending any pseudo-column to the table with 2+3 getting evaluated to 5There's no pseduo-column involved. (Pseudo-columns are something that Oracle provides for you, without you having to spell out what you want. ROWID and LEVEL are examples of pseudo-columns. In this example, 2+3 is an Expression . Like pseudo-columns, expressions can usually appear in the ORDER BY clause, even though they are not actually stored in the table.)
    It's sorting first by the NUMBER 5 (= 2 + 3), not the 5th column. Since the NUMBER 5 has the same value on every row, every row ties for first place. The result of sorting by any constant is the same; the results are the same as not including that exrpression in the ORDER BY clause. That is the ORDER BY clause you posted is equivalent to
    ORDER BY  2 + 3
    ,         'foo'
    ,         SYSDATE
    ,         NULL
    ,         stu_idand it's also equivalent to
    ORDER BY  stu_idNone of the expressions except stu_id vary from row to row, so none of them affect the sorting.
    (b) If there exist a 5th column in the table student and we specify it in the select statement's select list (5 column names) then why its not considering it as 5 and sorting the data according that?As Solomon said, only number literals are taken to mean a column. 5 is a number literal, so
    ORDER BY  5means "sort by the 5th column", but 2 + 3 is not a literal (it's an expression that happens to include a couple of literals), so
    ORDER BY  2 + 3does not refer to the 5th colunmn.
    (c) Is it true that, any expression we write is getting evaluated to NULL? Then is it that ORDER BY NULL evaluates to no ordering and the parser searches for next column-name or position (if specified) ?No, when you way "ORDER BY x", the rows with lower values of x come first, followed by rows with higher values of x. Rows with the same value of x will be together, in no particular order with respect to each other (unless there is a tie-breaker expression later in the ORDER BY clause.
    That's exactly what happens when you say "ORDER BY NULL". NULL has the same value (actually, the same lack of any value) on all rows, so all the rows have the same value of x, and they will appear in no particular order.
    (d) if neither then, what's the reason of the ignorance?

  • RESTICTIONS ON ORDER BY CLAUSE

    Can someone clear my doubts.I find it in DOCS but didn't succeded.Where it is written ?Please Explain me.
    Can We use ORDER BY CLAUSE in SubQuery ?
    Thanks.
    KarTiK.

    No,
    Even though It wont work..
    Findout yourself..
    SQL> select ename,sal
      2  from (select a.*,rownum s
      3        from emp a
      4        order by sal desc)
      5  where s = &rnk
      6  /
    Enter value for rnk: 4
    old   5: where s = &rnk
    new   5: where s = 4
    ENAME             SAL
    JONES            2975
    SQL> /
    Enter value for rnk: 3
    old   5: where s = &rnk
    new   5: where s = 3
    ENAME             SAL
    WARD             1250
    SQL> /
    Enter value for rnk: 1
    old   5: where s = &rnk
    new   5: where s = 1
    ENAME             SAL
    SMITH             800                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

  • Order by clause not working

    Hi, everyone,
    I am trying to use the following insert statement to insert records in asc order. if i use the select statement alone the order by clause works fine, but when i use the same sql and add a insert into table statment the order by clause does not work. I am not getting the records in ascending order. could anybody help me in this regard?
    INSERT INTO cat_sales_stg
    select      b.SSC,                
         ltrim(rtrim(a.CAT_DESC)) cat_desc,                
         SUM(a.AMOUNT) AMOUNT               
    FROM Trans a, SSC b                     
    WHERE a.ACCOUNT_NUMBER = TO_CHAR(b.ACCOUNT_NUMBER)                    
         AND a.TMONTH >= 200905 AND a.TMONTH <= 200910                
         AND a.FORMAT_NAME = 'ABC'                
         AND b.BMONTH = 200910                
         AND b.SAMPLE = 3                
         AND b.BANNER_NAME = 'ABC'               
         AND b.MODEL_NAME = 'XYZ'               
    group by b.SSC, ltrim(rtrim(a.CAT_DESC))
    order by ssc,cat_desc
    Thanks in advance

    user10636796 wrote:
    Hi, everyone,
    I am trying to use the following insert statement to insert records in asc orderWhat Toon, William, and others have said is that you DON'T insert rows in a specific order. That is completely outside the way relational databases are designed. You insert rows as unordered and use an ORDER BY clause in a SELECT when reading them. ORDER BY is for SELECT statements, not INSERT.
    In particular Toon poined out that we can't control where individual rows get stored.
    There is a databas object called a varray that can store data in sorted order. I have never seen them used because selecting the data back out again is more work; using an ordinary table and an ORDER BY clause is much easier.

  • Suppress "Order By" clause in Answers Query

    Hello,
    Is it possible to Suppress "Order By" clause in Answers Query.
    I'm using a database view as data source. In the view definition, "order by" clause is already specified. Is it possible to get the same order in the OBIEE report??
    I do not want to use Sort Order column in the repository.
    Thanks,
    Girish

    You add a rownumber to your DB view and use that to 'sort' your report.
    regards
    John
    http://obiee101.blogspot.com/

  • How to make an ORDER BY clause dynamic

    is there anyway to make the ORDER BY clause in an SQL query within a DB control
    dynamic? I have tried to pass a String of the column name and place it in the
    statement within {}, but it doesn't work

    "Mark" <[email protected]> wrote:
    >
    is there anyway to make the ORDER BY clause in an SQL query within a
    DB control
    dynamic? I have tried to pass a String of the column name and place
    it in the
    statement within {}, but it doesn't workDid you find how ? please let me know, I also need to have a dynamic order by
    clause.

  • In Report,order by clause is not working

    Hi All,
    For the report i prepared the query in this one am using the order by clause,i run the query in the sql plus working fine am getting the data based on the order by clause.when i use the same query for the report am getting the data but not in the order(i.e order by clause in not working).
    Please help me how to do this one.
    Thanks in Advance.
    Regards,
    Leelakrishna.G

    Hi Dora,
    Data is coming in the proper sequence,but in the first group data is not coming(In first group data first line only displaying,remaining data is not comming).
    My req is:
    The report is executing based on the creation date(i.e.,from creation date to today).
    In this so many Purchase requisitions will be there for this one different approval in different times will be there.
    For Ex:
    we will take 2 PR number only(for the first one 3 approvals,second one 4 levels of approval).
    1. one supplier 1person raised by 1PR submit
    No Action
    Approve
    2. 2Supplier 2personraised by 2PR Submit
    NO Action
    Forword
    Approve
    with ur suggestion am trying data is gettin fine but first group first line is coming remaining data is not coing.
    EX:
    1. one supplier 1person raised by 1PR submit
    No Action
    Approve
    2PR Submit
    NO Action
    Forword
    Approve
    Note:"2. 2Supplier 2personraised by " this kind of lines data is missing.
    Can you pls check and suggest me how i can do this.
    Thanks in Advance.
    Regards,
    Leelakrishna.G

  • Error : The ORDER BY clause is invalid in views, inline functions, derived

    Hi All,
    I am on 11g 6.2, Windows Server 2008, my db SQL server 2008, I am facing the error for the reports in which I am trying to edit one the column formula and do something like 'abc/sum(abc)*100'.
    10058. [NQODBC] [SQL_STATE: HY000] [nQSError: 10058] A general error has occurred. [nQSError: 43113] Message returned from OBIS. [nQSError: 43119] Query Failed: [nQSError: 16001] ODBC error state: 37000 code: 8180 message: [Microsoft][ODBC SQL Server Driver][SQL Server]Statement(s) could not be prepared.. [nQSError: 16001] ODBC error state: 37000 code: 1033 message: [Microsoft][ODBC SQL Server Driver][SQL Server]The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP or FOR XML is also specified.. [nQSError: 16002] Cannot obtain number of columns for the query result. (HY000)
    One of the solutions to this which I have found is to edit the EXPRESSION_IN_ORDERBY_SUPPORTED feature in the db properties.
    I want to know what does EXPRESSION_IN_ORDERBY_SUPPORTED means?
    When I create a calculations in 11g like abc/sum(abc) in the column formula for a column then i get this error.
    What does this error mean? Does OBIEE 11g doesn't support using these expressions in the report and the fact that it applies the order by clause to the reports, the report fail?
    Could anybody please explain the issue. There is very limited information on this over the web.
    Thanks in advance.
    Ronny

    Thanks svee for the quick response, actually i had resolved the issue by unchecking the EXPRESSION_IN_ORDERBY_SUPPORTED option in the database. I want to understand how does that makes the difference?
    What does EXPRESSION_IN_ORDERBY_SUPPORTED mean? Does it mean that if I give any expression in my answers report and since obiee uses a order by for all the queries, the expression won't be supported?
    Please explain.

  • Order by clause in cursor problem

    Hello,
    I'm unable to compile package body when i'm using order by clause in cursor subquery in stored procedure.
    Sample code:
    CREATE PACKAGE Announces AS
    TYPE tRefCur IS REF CURSOR;
    PROCEDURE TopAnnounces(
    iiCount IN NUMBER,
    osAnnounces OUT tRefCur,
    oiRetVal OUT NUMBER
    END Announces;
    CREATE PACKAGE BODY Announces AS
    PROCEDURE TopAnnounces(
    iiCount IN NUMBER,
    osAnnounces OUT NUMBER,
    oiRetVal OUT NUMBER
    AS
    BEGIN
    OPEN osAnnounces FOR
    SELECT Id, Name, AnnCount FROM
    SELECT Id, Name, COUNT(CategoryId) AS AnnCount FROM tblAnnounces
    GROUP BY Id, Name
    -- bellow is the line, where the code crash
    ORDER BY AnnCount DESC
    WHERE ROWNUM < iiCount + 1;
    oiRetVal := 0;
    EXCEPTION
    WHEN OTHERS THEN
    oiRetVal := -255;
    END TopAnnounces;
    END Announces;
    If I compile the code above I will get this error:
    PLS-00103: Encoutered the symbol "ORDER" when expecting on of the following:
    After I remark the problematic line, the compilation is successful (but not the result :).
    Is there something I'm doing wrong?
    Thanks for advice
    Vojtech Novacek
    null

    Sorry you can not use order by clause into one temporal table created by subquery.
    Put the order by clause offside of subquery.
    Atte.
    CC.
    <BLOCKQUOTE><font size="1" face="Verdana, Arial">quote:</font><HR>Originally posted by Vojtech Novacek:
    Hello,
    I'm unable to compile package body when i'm using order by clause in cursor subquery in stored procedure.
    Sample code:
    CREATE PACKAGE Announces AS
    TYPE tRefCur IS REF CURSOR;
    PROCEDURE TopAnnounces(
    iiCount IN NUMBER,
    osAnnounces OUT tRefCur,
    oiRetVal OUT NUMBER
    END Announces;
    CREATE PACKAGE BODY Announces AS
    PROCEDURE TopAnnounces(
    iiCount IN NUMBER,
    osAnnounces OUT NUMBER,
    oiRetVal OUT NUMBER
    AS
    BEGIN
    OPEN osAnnounces FOR
    SELECT Id, Name, AnnCount FROM
    SELECT Id, Name, COUNT(CategoryId) AS AnnCount FROM tblAnnounces
    GROUP BY Id, Name
    -- bellow is the line, where the code crash
    ORDER BY AnnCount DESC
    WHERE ROWNUM < iiCount + 1;
    oiRetVal := 0;
    EXCEPTION
    WHEN OTHERS THEN
    oiRetVal := -255;
    END TopAnnounces;
    END Announces;
    If I compile the code above I will get this error:
    PLS-00103: Encoutered the symbol "ORDER" when expecting on of the following:
    After I remark the problematic line, the compilation is successful (but not the result :).
    Is there something I'm doing wrong?
    Thanks for advice
    Vojtech Novacek<HR></BLOCKQUOTE>
    null

  • Order By clause in Receiver JDBC adapter

    Hello All,
    Is it possible to construct a select statement that uses an Order by clause in JDBC receiver adapter?
    ex : I would like the JDBC adapter to from a query as follows:
    <b>Select * from EMP where sal = 10000 order by empid</b>
    We are able to construct the select statement without order by clause but not with it.
    Can any one suggest me how to do it. This is bit urgent for us.
    Thanks
    Abinash

    hi,
    did you try with
    SQL_DML
    http://help.sap.com/saphelp_nw04/helpdata/en/2e/96fd3f2d14e869e10000000a155106/content.htm
    Regards,
    michal

  • 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

  • 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