Sql count function in order by clause

Post Author: krypton
CA Forum: Data Connectivity and SQL
Hi Guys
Can i ask a quick question. I am trying to retrieve data remotely from a SQL Server via crystal reports.
Within the Database Expert I have entered a SQL query to retrive the number of (call center) support calls raised by our customers:-
Select `Primary_Company`, COUNT(`Calls`)From  `SPRT_Issue` GROUP BY  `Primary_Company`ORDER BY  COUNT(`Calls`) desc
The customer's column is called 'Primary Company' and the calls they raise are in the 'Calls' column. the above is a normal sql query.
However Crystal fails to run the query and generates an error message :-
Failed to open a rowset. Details: 420: Driver&#93; Expected lexical element not found: <identifier>
I dont understand why it wont run. In the ORDER BY clause if i replace field 'Calls' by the field 'Primary Company' then it works.
I think the problem is that it wont accept the count function in the order by clause. which is what i want it to do i.e display the calls in descending order by each customer.
Could someone tell me if there is a way around it.
Thanks
Krypton

Post Author: krypton
CA Forum: Data Connectivity and SQL
Thanks Lynn
I tried your suggestion. But there is one probelm.
When i sort the data in descending order using the count(calls) field, the data is returned but the customer's name appears multiple times along with their calls raised.
E.g, if customer Mark raised multiple calls i.e. 2, 5, and 10 calls, then the report will show
Mark   2
Mark  5
Mark 10
But is there a way to aggregate all the calls for mark and show them only once:
such as Mark   17
Thanks

Similar Messages

  • Help: PL/SQL passing paramter to ORDER BY clause

    I am working on a procedure that using a parameter to pass sorting order. If there parameter, say p_order which is varchar2, can I just use the it directly passing the field name to the order by clause?
    What I really want to know is that do I have to use decode together with p_order to achieve the goal?
    WJH

    Hi,
    The positional notaion in ORDER BY, e.g.
    ORDER BY 2, 1is one of the rare cases in which a numeric literal is required. Using an expression, even a bind variable, won't raise an error, but won't sort, either. (It's equivalent to sorting by a constant, which doesn't sort at all.)
    Like Centinul said, you have to use dynamic SQL to construct the ORDER BY clause, or use some kind of IF-THEN-ELSE logic (such as CASE), like this:
    SELECT    ename, sal
    FROM      scott.emp
    ORDER BY  CASE
                  WHEN  :x = 1  THEN ename
                  ELSE  TO_CHAR (sal, '000000')
              END;

  • OBIEE: Incorrect SQL - with count function uses ORDER BY instead GROUP BY

    I made a basic report that is a client count; I want to know how many clients the company have.
    But, when I run this report, OBIEE generates a ORDER BY sentence, instead a GROUP BY. Remember that I'm using count function, that is a agregation.
    The SQL generated was:
    select 'N0' as c1,
    count(*) as c2
    from
    (select distinct T1416.CLIENT_INTER_KEY as c1
    from
    (select *
    from prd.D_SERVICE where SOURCE_SYS in ('ARBOR','PPB') and DW_SERV_ST_ID in (100000003,100000009)) T1836,
    (select *
    from prd.D_CLIENT) T1416,
    (select *
    from prd.D_CUSTOMER_ACCOUNT where SOURCE_SYS In ('ARBOR','PPB')) T1515
    where ( T1416.DW_CLIENT_ID = T1515.DW_CLIENT_ID and T1515.DW_CUST_ACCT_ID = T1836.DW_CUST_ACCT_ID and T1836.MSISDN = '917330340' )
    ) D1
    order by c1
    The error that I receive is:
    "Query Status: Query Failed: [nQSError: 16001] ODBC error state: S1000 code: -1005018 message: [Sybase][ODBC Driver][Adaptive Server Anywhere]Illegal ORDER BY item Order Item: 'N0',
    -- (opt_OrderBy.cxx 429) .
    [nQSError: 16011] ODBC error occurred while executing SQLExtendedFetch to retrieve the results of a SQL statement."
    If I substitute ORDER BY with GROUP BY and test it in Sybase, Ithe query runs without any problem.
    select 'N0' as c1,
    count(*) as c2
    from
    (select distinct T1416.CLIENT_INTER_KEY as c1
    from
    (select *
    from prd.D_SERVICE where SOURCE_SYS in ('ARBOR','PPB') and DW_SERV_ST_ID in (100000003,100000009)) T1836,
    (select *
    from prd.D_CLIENT) T1416,
    (select *
    from prd.D_CUSTOMER_ACCOUNT where SOURCE_SYS In ('ARBOR','PPB')) T1515
    where ( T1416.DW_CLIENT_ID = T1515.DW_CLIENT_ID and T1515.DW_CUST_ACCT_ID = T1836.DW_CUST_ACCT_ID and T1836.MSISDN = '917330340' )
    ) D1
    group by c1
    Do you know why OBIEE generates this SQL??? Why uses, with a aggregation function, a ORDER BY and not a GROUP BY? How can I resolve this problem???
    Regards,
    Susana Figueiredo

    Verify your repository design and make sure that you have defined count aggregate on fact column. You would also need to define the content level of each dimension in fact table.

  • Problem in sql query because of order by clause

    Hi All,
    I am facing a one problem in my one sql query.I am using Oracle 10gR2.
    Query is given below:
    SELECT t1.ename
            FROM T1, T2
           WHERE T1.EMPNO = 1234
             AND T1.ACCOUNTNO = T2.ACCOUNTNO
             AND T1.SEQ = T2.SEQ
           ORDER BY T2.SEQThe Plan of the query is :
    | Id  | Operation                     | Name                 | Rows  | Bytes | Cost (%CPU)| Time     |
    |   0 | SELECT STATEMENT              |                      |     2 |   218 | 11716   (1)| 00:00:41 |
    |*  1 |  TABLE ACCESS BY INDEX ROWID  | T1                   |     1 |    89 |     1   (0)| 00:00:01 |
    |   2 |   NESTED LOOPS                |                      |     2 |   218 | 11716   (1)| 00:00:41 |
    |*  3 |    TABLE ACCESS BY INDEX ROWID| T2                   |     2 |    40 | 11715   (1)| 00:00:41 |
    |   4 |     INDEX FULL SCAN           | PK_T2_SEQ            | 58752 |       |   122   (5)| 00:00:01 |
    |*  5 |    INDEX RANGE SCAN           | FK_ACCOUNTNO         |     3 |       |     0   (0)| 00:00:01 |
    ----------------------------------------------------------------------------------------------------Now i want to reduce the time of this query.
    If i am removing Order by clause from query than performance of the query is totally perfect but with order by clause its taking a time.
    I have already set SORT_AREA_SIZE but still nothing is improving.
    Welcome and thanks for your suggestions.
    Thanks,
    Edited by: BluShadow on 23-Jun-2011 07:55
    added {noformat}{noformat} tags and formatted explain plan to make it readable.  Please see {message:id=9360002} for details on how to post code and data                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

    Hi,
    There are a couple of things I do not understand.
    1. Why don't you put {noformat}{noformat} around your code, it makes it so much easier to read, especially your explain plan
    2. You claim that the ORDER BY is problematic compared to no order by. Then why do you choose to post only one plan?
    3. It is hard to understand how your tables relate, and which indexes you have and which you don't.
    - PK_T2_SEQ, does this mean that SEQ alone is primary key of T2?
    - If SEQ is primary key of T2, why do you join on accountno, seq and not just seq?
    - If SEQ is primary key of T2 one of the tables is denormalized.
    4. FK_ACCOUNTNO, is this an index on accountno, alone?
    - Or is this AccountNo, Seq?
    5. Is there no index on T1.EMPNO?
    Above could of course just be a case of my not understanding the names of your indexes.
    So, here are my guesses:
    Above plan is for the ORDER BY query. That means the optimizer, has chosen to full scan PK_T2_SEQ, since data is then read according to the ORDER BY.
    (This could be a bad choice)I
    You could try and order by t1.seq, instead. Result should be the same.
    Regards
    Peter                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

  • SQL Query rewrite, remove ORDER BY clause

    Hello,
    we've just installed a proprietary application which uses an Oracle 11.2.0.2 RAC database. We've seen that one of the auto-generated application's queries
    has poor performance (3-5 minutes for results), the query does a SELECT and at the end it uses an ORDER BY clause. If we remove the ORDER BY clause
    the query returns the results in less than 5 seconds. Unfortunately, we can't re-write the query in the application since we don't have any access to it and
    i was wondering if there is a way to rewrite the specific query from the database.
    We've already seen the SQL Patch but we can change only the hints of the query so we can't remove the ORDER BY clause from it. From what we've seen
    outlines also change only the hints and not the actual sql statement.
    Is there a way to rewrite the specific query from the database ?
    thanks in advance,
    Giannis

    Maybe DBMS_ADVANCED_REWRITE can help but this a SQL question than has very likely nothing to do with RAC.
    See http://www.oracle-base.com/articles/10g/dbms_advanced_rewrite.php.

  • Odd Error with decode function in Order By Clause

    I am trying to compile a procedure and can't get around an error with a dynamic order by that doesn't make much sense to me. I can repoduce the error with a plain select statment in sql plus so I can rule out a declaration error. Here is an example with 2 numeric columns and a date column.
    select task_id, display_date, remark_id from task_list
    where task_id > 1000
    order by decode('Task_ID', 'Task_ID',Task_ID, 'Display_Date', Display_Date, 'Remark_ID',Remark_ID)
    returns the error:
    select task_id, display_date, remark_id from task_list
    ERROR at line 1:
    ORA-00932: inconsistent datatypes: expected NUMBER got DATE
    I'm not sure why this error is occuring, because it doesn't even hit the Display_Date field in the Decode statment. If I take the Display_date out of the statement, it runs properly. If I change Display_Date to To_Char(Display_Date) it also runs fine, but the sorting is incorrect. Also I'm running 9.2, and do not want to use dynamic sql to build my query. Any Ideas out there as to the cause of this error?

    I did find a workaround to this issue by breaking the decode statment into three separate statement, but I still think that the way it is written above should work, and may be a bug, or an error that I don't understand fully.
    The Order by was rewritten like this:
    order by decode(pSort, 'Task_ID',Task_ID), decode(pSort, 'Display_Date', Display_Date),
    decode(pSort, 'Remark_ID',Remark_ID);
    Thanks

  • Decode function in order by clause

    Hi,
    I need a help from you..
    SELECT 'ALTER ' || object_type || ' '|| object_name ||' COMPILE;' FROM user_objects WHERE object_type IN ('FUNCTION', 'PACKAGE', 'PROCEDURE', 'TRIGGER', 'VIEW') ORDER BY DECODE(object_type, 'VIEW','A', 'FUNCTION','B', 'PROCEDURE', 'C', 'PACKAGE','D', 'Z');
    Explanation: This will sort by type of an object, in the decode assigned value A to VIEW, B FUNCTION, C PROCEDURE, D to PACKAGE and Z to OTHERS. so all the views will be at the top and then functions, then procedures, then packages and finally the rest of the object.
    Please let me know how this query will be executed.
    Cheers,
    bell.

    Hi,
    Try this:
    FOR obj_cur IN (
    SELECT 'ALTER ' || object_type || ' '|| object_name ||' COMPILE;'  exc_cmd FROM user_objects WHERE object_type IN ('FUNCTION', 'PACKAGE', 'PROCEDURE', 'TRIGGER', 'VIEW') ORDER BY DECODE(object_type, 'VIEW','A', 'FUNCTION','B', 'PROCEDURE', 'C', 'PACKAGE','D', 'Z')
    LOOP
    BEGIN
      EXECUTE IMMEDIATE obj_cur.exc_cmd ;
    EXCEPTION
        WHEN OTHERS THEN
         dbms_output.put_line(obj_cur.exc_cmd || SQLERRM);
    END;
    END LOOP;
    Regards

  • How to use oracle nlssort function in order by clause?

    If I need to use linguistic sort feature in Oracle, I can write a SQL like that:
    Select product_name from product
    order by NLSSORT(product_name, 'NLS_SORT=GENERIC_M');
    How can I generate a SQL like this by Toplink?

    Hi,
    To answer your query:-
    http://abhishek-soablog.blogspot.com/2008/08/orclquery-database.html
    Cheers,
    Abhi...

  • Using oracle function in order by clause

    Hello,
    can i use report query or report that generate query like this :
    select fname, lname from peoples order by dbms_random.value;
    dbms_random is an oracle function to generate random value.
    Thnaks

    add the following call to your query:
    query.addOrdering(query.getExpressionBuilder().getFunction("dbms_random.value"));
    --Gordon                                                                                                                                                                                                                                                                       

  • Creating optional elements using SQL / XML functions

    Hi,
    I am struggling with some SQL / XML functionality in order to create some optional elements in a XMLType.
    I have one table with data to be generated into an XML document. The table looks like this (only the attributes related to the problem are shown):
    msg_id (pk)
    geslacht_hoofdverzekerde
    geboortedatum_hoofdverzekerde
    geslacht_medeverzekerde
    geboortedatum_medeverzekerde
    I have to create an XML document that looks like this:
    <ber:rekenparameters>
    <ber:verzekerde>
    <ber:codeRelatierol>HVZ</ber:codeRelatierol>
    <ber:geslacht>M</ber:geslacht>
    <ber:datumGeboorte>01-01-1960</ber:datumGeboorte>
    </ber:verzekerde>
    <ber:verzekerde>
    <ber:codeRelatierol>MVZ</ber:codeRelatierol>
    <ber:geslacht>V</ber:geslacht>
    <ber:datumGeboorte>01-01-1961</ber:datumGeboorte>
    </ber:verzekerde>
    </ber:rekenparameters>
    Where <ber:codeRelatierol> is hard coded: i.c. of hoofverzekerde 'HVZ' in case of medeverzekerde 'MVZ'.
    Geslacht en datumGeboorte are taken form the table.
    The element <ber:verzekerde> is obligatiry for hoofdverzekerde but optional for medeverzekerde: only in case geslacht_medeverzekerde and geboortedatum_medeverzekerde are not null this 2nd <ber:verzekerde> element has to be added.
    I created the next SQL (I skipped some stuff in order to focus on the problem):
    CREATE OR REPLACE
    VIEW dps_v_berekening_berichten
    AS
    SELECT msg_id as msg_id
    , XMLElement( "ber:berekening"
    , XMLAttributes( 'http://www.w3.org/2001/XMLSchema-instance' AS "xmlns:xsi"
    , 'http://www.mycompany.nl/berekenen' AS "xmlns:ber"
    , 'http://www.mycompany.nl/berekenen http://www.mycompany.nl/berekenen/berekening.xsd' AS "xsi:schemaLocation"
    , XMLElement( "ber:nummerRekenRegel"
    , XMLAttributes( 'http://www.mycompany.nl/berekenen' AS "xmlns:ber" )
    , 1
    , XMLElement( "ber:rekenparameters"
    , XMLAttributes( 'http://www.mycompany.nl/berekenen' AS "xmlns:ber" )
    , XMLElement( "ber:codeEvolutie" ) -- Empty
    , XMLElement( "ber:verzekerde"
    , XMLForest ( 'HVZ' AS "ber:codeRelatierol"
    , xoa.geslacht_hoofdverzekerde AS "ber:geslacht"
    , xoa.geboortedatum_hoofdverzekerde AS "ber:datumGeboorte"
    , XMLElement( "ber:verzekerde"
    , XMLForest ( 'MVZ' AS "ber:codeRelatierol"
    , xoa.geslacht_medeverzekerde AS "ber:geslacht"
    , xoa.geboortedatum_medeverzekerde AS "ber:datumGeboorte"
    -- Some more elements.
    ) AS bericht
    FROM DST_UP1_XML_OUTDATA xoa
    The problem is that the XMLForest always creates an <ber:verzekerde> element for medeverzekerde. I tried to create an inline view with just the "vezekerde"attributes and conditionally joining this inline view with the dst_up1_xml_outdata table. That didn't solve my problem. Because in case of a medeverzekerde available it returned two XML documenst: one that included the hoofdverzekerde and one that included the medeverzekerde. And obviously that's not what I want.
    I imagine I have to juggle with some of these SQL / XML functions although I cannot put the finger on the exact differences between some of these (XMLElement, XMLForest, XMLAgg, XMLSequence). The examples shown in de XML DB Developer's Guide don't seem to adress my problem.
    Help !

    Hi, are you talking about this part that you don't want to be there? I am not so clear about your requirements.
    XMLElement( "ber:verzekerde"
    , XMLForest ( 'MVZ' AS "ber:codeRelatierol"
    , xoa.geslacht_medeverzekerde AS "ber:geslacht"
    , xoa.geboortedatum_medeverzekerde AS "ber:datumGeboorte"
    If you want to control certain subelements so they don't appear, do not use xmlforest. Use xmlelement instead so it is easier to control it using case when. Mark (of Oracle, he should be here soon since OOW is over) showed me this technique and it helps tremendously, because oftentimes XDB will return an empty tag even though your consumers don't like it!
    try something like this,
    (case when xoa.geboortedatum_medeverzekerde = 'MVZ' then
    xmlelement("ber:verzekerde",
    xmlelement("What you want", colname),
    xmlelement("ber:codeRelatierol", 'MVZ'),
    (case when colname is not null or = some other value then
    xmlelement("Rest", colname))
    end)
    end),
    You may have to twig some details there. But the key is to use the case when construct to get rid of unwanted elements or empty elements. So you should only have the elements if you have a value of 'MVZ'. Hope this helps.
    Ben

  • 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  Dynamic in Oracle function

    How can i get order by Clause Dynamic in Oracle function
    My function Returns sql query with SYS_REFCURSOR . and i will pass the order by column as input parameter
      create or replace
    FUNCTION TEST_SSK
            p_srot  number
    RETURN SYS_REFCURSOR
    AS
    C_testssk SYS_REFCURSOR;
    BEGIN
    OPEN C_TESTSSK FOR
    SELECT LOAN_CODE,LOAN_DATE,DUE_DATE,LOAN_AMT FROM LOAN_MASTER
    order by P_SROT;
    return C_testssk;
    end;Edited by: user10736825 on Dec 22, 2010 11:34 AM

    you can go for a dynamic query ;)
    create or replace
    FUNCTION TEST_SSK
            p_srot  number
    RETURN SYS_REFCURSOR
    AS
    C_testssk SYS_REFCURSOR;
    l_str VARCHAR2(4000);
    l_order VARCHAR2(100);
    BEGIN
    l_str := 'SELECT LOAN_CODE,LOAN_DATE,DUE_DATE,LOAN_AMT FROM LOAN_MASTER ';
    IF p_sort = 'LC'
    THEN
      l_order := ' ORDER BY LOAN_CODE ';
    ELSIF p_sort = 'LD'
    THEN
      l_order := ' ORDER BY LOAN_DATE ';
    END IF;
      l_str := l_str || l_order ;
    OPEN C_TESTSSK FOR l_str;
    return C_testssk;
    end;

  • Slow SQL output when table alias is NOT used in order by clause

    Hi guys,
    My query is based on Oracle 9208
    I have a table TAB1 with 68000 records with transaction_id as the primary key in this table (unique index).
    I have another TAB2 with the same number of records again with transaction_id in this table (foreign key to above).
    I have the below query that gets executed via an application:-
    SELECT TO_CHAR(V1.TRANSACTION_ID), V1.POLICY_ID, V1.REQUEST_TYPE
    FROM <VIEW> V1 WHERE V1.CERT_SERIAL_NUM=56192 AND
    (V1.AUTH_GROUP_ID=0 OR V1.AUTH_GROUP_ID=1) AND ROWNUM <= 3 ORDER
    BY TRANSACTION_ID ASC
    The above view V1 is created as below:-
    CREATE OR REPLACE FORCE VIEW "V1"
    ("TRANSACTION_ID",
    "PARENT_TRANSACTION_ID",
    "CA_DN_ID",
    "AUTH_GROUP_ID",
    "POLICY_ID",
    "REQUEST_TYPE",
    "REQUEST_STATUS",
    "EE_DN_HASH",
    "EE_DN",
    "EE_EMAIL_HASH",
    "EE_EMAIL",
    "KEY_USAGE",
    "SMART_CARD_SERIAL",
    "CERT_TYPE",
    "CERT_SERIAL_NUM",
    "CERT_INDEX",
    "RENEWAL_FLAG",
    "ARCHIVE_FLAG",
    "TIME_RECEIVED",
    "DOWNLOADED",
    "REQUEST_DATA",
    "ACTION",
    "STEP_NUM")
    AS
    SELECT
    T1.transaction_id,
    T1.parent_transaction_id,
    T1.ca_dn_id,
    V2.auth_group_id,
    V2.policy_id,
    T1.request_type,
    T1.request_status,
    T2.ee_dn_hash,
    T2.ee_dn,
    T2.ee_email_hash,
    T2.ee_email,
    T2.key_usage,
    T2.smart_card_serial,
    T2.cert_type,
    T2.cert_serial_num,
    T2.cert_index,
    T2.renewal_flag,
    T2.archive_flag,
    T1.time_received,
    T1.downloaded,
    T1.request_data,
    V2.action,
    V2.step_num
    FROM TAB1
    <ANOTHER VIEW> V2,
    TAB2 T1,
    TAB2 T2
    WHERE
    T1.transaction_id = T2.transaction_id
    AND
    V2.policy_id = T1.policy_id
    order by transaction_id;
    The query at the top runs within milliseconds if the VIEW is created as :-
    order by t2.transaction_id
    But without the alias "t2" in the order by, the query takes about 1 1/2 minutes
    Can you tell me why? I thought if you ordering by primary key (lesser number of values compared to foreign key values), the query should be faster..no?
    Thanks in advance

    Thanks for keeping up with this issue Hemant.
    Here are the plans for each case.
    I would be very interested in how you'd recognize which plan is the best and what are the derivatives.
    I don't much (or rather anything!) what is 'card' values, 'cost' values etc which I believe are used to decide the best plan of the lot.
    Thanks again
    Note TAB1 and TAB2 are from view definition posted initially
    1) Execution Plan for VIEW1 <<-- With ORDER BY" clause but no table ailas (order by transaction_id)
    SQL> EXPLAIN PLAN FOR SELECT TO_CHAR(QT.TRANSACTION_ID), QT.POLICY_ID, QT.REQUEST_TYPE
    2 FROM <VIEW1> QT WHERE QT.CERT_SERIAL_NUM=24293 AND
    3 (QT.AUTH_GROUP_ID=0 OR QT.AUTH_GROUP_ID=1) AND ROWNUM <= 3 ORDER
    4 BY TRANSACTION_ID ASC
    5 /
    Explained.
    Elapsed: 00:00:01.00
    SQL> select * from table(dbms_xplan.display);
    PLAN_TABLE_OUTPUT
    | Id | Operation | Name | Rows | Bytes | Cost |
    | 0 | SELECT STATEMENT | | 3 | 195 | 17 |
    |* 1 | COUNT STOPKEY | | | | |
    | 2 | VIEW | VIEW1 | 17 | 1105 | 17 |
    |* 3 | SORT ORDER BY STOPKEY | | 17 | 38573 | 17 |
    | 4 | NESTED LOOPS | | 17 | 38573 | 10 |
    | 5 | MERGE JOIN CARTESIAN | | 1 | 167 | 9 |
    | 6 | VIEW | VIEW2 | 1 | 52 | 8 |
    | 7 | SORT UNIQUE | | 1 | 156 | 8 |
    | 8 | NESTED LOOPS | | 1 | 156 | 6 |
    | 9 | NESTED LOOPS | | 1 | 143 | 6 |
    | 10 | NESTED LOOPS OUTER | | 1 | 117 | 5 |
    |* 11 | HASH JOIN | | 1 | 104 | 5 |
    | 12 | NESTED LOOPS | | 1 | 52 | 2 |
    |* 13 | TABLE ACCESS FULL | TAB3 | 1 | 39 | 2 |
    |* 14 | INDEX UNIQUE SCAN | (PK_TAB4) | 1 | 13 | |
    | 15 | TABLE ACCESS FULL | TAB5 | 82 | 4264 | 2 |
    | 16 | VIEW PUSHED PREDICATE | View3 | 1 | 13 | |
    | 17 | NESTED LOOPS | | 1 | 52 | 2 |
    | 18 | NESTED LOOPS | | 1 | 39 | 2 |
    |* 19 | INDEX UNIQUE SCAN | (PK_TAB6) | 1 | 13 | 1 |
    |* 20 | INDEX RANGE SCAN | (PK_TAB7) | 1 | 26 | 1 |
    |* 21 | INDEX UNIQUE SCAN | (PK_TAB8) | 1 | 13 | |
    | 22 | TABLE ACCESS BY INDEX ROWID| TAB9 | 3 | 78 | 1 |
    |* 23 | INDEX UNIQUE SCAN | (PK_TAB9) | 1 | | |
    |* 24 | INDEX UNIQUE SCAN | (PK_TAB10)| 1 | 13 | |
    | 25 | BUFFER SORT | | 1 | 115 | 9 |
    | 26 | TABLE ACCESS BY INDEX ROWID | TAB2 | 1 | 115 | 1 |
    |* 27 | INDEX RANGE SCAN | (TAB2_IDX2)| 1 | | |
    |* 28 | TABLE ACCESS BY INDEX ROWID | TAB1 | 12 | 25224 | 1 |
    |* 29 | INDEX UNIQUE SCAN | (PK_TAB1) | 1 | | |
    Predicate Information (identified by operation id):
    1 - filter(ROWNUM<=3)
    3 - filter(ROWNUM<=3)
    11 - access("TAB5"."PATH_ID"="TAB4"."PATH_ID")
    13 - filter("TAB3"."AUTH_GROUP_ID"<>(-1) AND ("TAB3"."AUTH_GROUP_ID"=0 OR "TAB3"."AUTH_GROUP_ID"=1))
    14 - access("TAB3"."PATH_ID"="TAB4"."PATH_ID")
    19 - access("TAB5"."DOMAIN_ID"="TAB6"."DOMAIN_ID")
    20 - access("TAB6"."DOMAIN_ID"="TAB7"."DOMAIN_ID")
    21 - access("TAB7"."RULE_ID"="TAB8"."RULE_ID")
    23 - access("TAB9"."POLICY_ID"="TAB5"."POLICY_ID")
    24 - access("TAB9"."ASSOCIATED_FORM_ID"="TAB10"."FORM_ID")
    27 - access("TAB2"."CERT_SERIAL_NUM"=24293)
    28 - filter("View2"."POLICY_ID"="TAB1"."POLICY_ID")
    29 - access("TAB1"."TRANSACTION_ID"="TAB2"."TRANSACTION_ID")
    Note: cpu costing is off
    54 rows selected.
    Elapsed: 00:00:01.81
    Execution Plan
    0 SELECT STATEMENT Optimizer=CHOOSE
    1 0 COLLECTION ITERATOR (PICKLER FETCH) OF 'DISPLAY'
    Statistics
    39 recursive calls
    0 db block gets
    629 consistent gets
    0 physical reads
    104 redo size
    5169 bytes sent via SQL*Net to client
    405 bytes received via SQL*Net from client
    5 SQL*Net roundtrips to/from client
    31 sorts (memory)
    0 sorts (disk)
    54 rows processed
    2) Execution Plan for VIEW1 <<-- With ORDER BY" clause and table alias (order by TAB2.transaction_id)
    SQL> explain plan for SELECT TO_CHAR(QT.TRANSACTION_ID), QT.POLICY_ID, QT.REQUEST_TYPE
    2 FROM <VIEW1> QT WHERE QT.CERT_SERIAL_NUM=30003 AND
    3 (QT.AUTH_GROUP_ID=0 OR QT.AUTH_GROUP_ID=1) AND ROWNUM <= 3 ORDER
    4 BY TRANSACTION_ID ASC
    5 /
    Explained.
    Elapsed: 00:00:10.20
    SQL> select * from table(dbms_xplan.display);
    PLAN_TABLE_OUTPUT
    | Id | Operation | Name | Rows | Bytes | Cost |
    | 0 | SELECT STATEMENT | | 3 | 195 | 14 |
    | 1 | SORT ORDER BY | | 3 | 195 | 14 |
    |* 2 | COUNT STOPKEY | | | | |
    | 3 | VIEW | VIEW1 | 17 | 1105 | 13 |
    | 4 | NESTED LOOPS | | 17 | 38573 | 13 |
    | 5 | MERGE JOIN CARTESIAN | | 1 | 167 | 12 |
    |* 6 | TABLE ACCESS BY INDEX ROWID | TAB2 | 1 | 115 | 4 |
    | 7 | INDEX FULL SCAN | (TAB2_IDX) | 94 | | 1 |
    | 8 | BUFFER SORT | | 1 | 52 | 8 |
    | 9 | VIEW | VIEW2 | 1 | 52 | 8 |
    | 10 | SORT UNIQUE | | 1 | 156 | 8 |
    | 11 | NESTED LOOPS | | 1 | 156 | 6 |
    | 12 | NESTED LOOPS | | 1 | 143 | 6 |
    | 13 | NESTED LOOPS OUTER | | 1 | 117 | 5 |
    |* 14 | HASH JOIN | | 1 | 104 | 5 |
    | 15 | NESTED LOOPS | | 1 | 52 | 2 |
    |* 16 | TABLE ACCESS FULL | TAB3 | 1 | 39 | 2 |
    |* 17 | INDEX UNIQUE SCAN | (PK_TAB4) | 1 | 13 | |
    | 18 | TABLE ACCESS FULL | TAB5 | 82 | 4264 | 2 |
    | 19 | VIEW PUSHED PREDICATE | View3 | 1 | 13 | |
    | 20 | NESTED LOOPS | | 1 | 52 | 2 |
    | 21 | NESTED LOOPS | | 1 | 39 | 2 |
    |* 22 | INDEX UNIQUE SCAN | (PK_TAB6) | 1 | 13 | 1 |
    |* 23 | INDEX RANGE SCAN | (PK_TAB7) | 1 | 26 | 1 |
    |* 24 | INDEX UNIQUE SCAN | (PK_TAB8) | 1 | 13 | |
    | 25 | TABLE ACCESS BY INDEX ROWID| TAB9 | 3 | 78 | 1 |
    |* 26 | INDEX UNIQUE SCAN | (PK_TAB9) | 1 | | |
    |* 27 | INDEX UNIQUE SCAN | (PK_TAB10) | 1 | 13 | |
    |* 28 | TABLE ACCESS BY INDEX ROWID | TAB1 | 12 | 25224 | 1 |
    |* 29 | INDEX UNIQUE SCAN | (PK_TAB1) | 1 | | |
    Predicate Information (identified by operation id):
    2 - filter(ROWNUM<=3)
    6 - filter("TAB2"."CERT_SERIAL_NUM"=30003)
    14 - access("TAB5"."PATH_ID"="TAB4"."PATH_ID")
    16 - filter("TAB3"."AUTH_GROUP_ID"<>(-1) AND ("TAB3"."AUTH_GROUP_ID"=0 OR "TAB3"."AUTH_GROUP_ID"=1))
    17 - access("TAB3"."PATH_ID"="TAB4"."PATH_ID")
    22 - access("TAB5"."DOMAIN_ID"="TAB6"."DOMAIN_ID")
    23 - access("TAB6"."DOMAIN_ID"="TAB7"."DOMAIN_ID")
    24 - access("TAB7"."RULE_ID"="TAB8"."RULE_ID")
    26 - access("TAB9"."POLICY_ID"="TAB5"."POLICY_ID")
    27 - access("TAB9"."ASSOCIATED_FORM_ID"="TAB10"."FORM_ID")
    28 - filter("VIEW2"."POLICY_ID"="TAB1"."POLICY_ID")
    29 - access("TAB1"."TRANSACTION_ID"="TAB2"."TRANSACTION_ID")
    Note: cpu costing is off
    53 rows selected.
    Elapsed: 00:00:08.29
    Execution Plan
    0 SELECT STATEMENT Optimizer=CHOOSE
    1 0 COLLECTION ITERATOR (PICKLER FETCH) OF 'DISPLAY'
    Statistics
    1079 recursive calls
    0 db block gets
    597 consistent gets
    21 physical reads
    0 redo size
    5177 bytes sent via SQL*Net to client
    405 bytes received via SQL*Net from client
    5 SQL*Net roundtrips to/from client
    63 sorts (memory)
    0 sorts (disk)
    53 rows processed
    3) Execution Plan for VIEW1 <<-- Without any "ORDER BY" clause
    SQL> explain plan for SELECT TO_CHAR(QT.TRANSACTION_ID), QT.POLICY_ID, QT.REQUEST_TYPE
    2 FROM <VIEW1> QT WHERE QT.CERT_SERIAL_NUM=30003 AND
    3 (QT.AUTH_GROUP_ID=0 OR QT.AUTH_GROUP_ID=1) AND ROWNUM <= 3 ORDER
    4 BY TRANSACTION_ID ASC
    5 /
    Explained.
    Elapsed: 00:00:10.20
    SQL> select * from table(dbms_xplan.display);
    PLAN_TABLE_OUTPUT
    | Id | Operation | Name | Rows | Bytes | Cost |
    | 0 | SELECT STATEMENT | | 3 | 213 | 11 |
    | 1 | SORT ORDER BY | | 3 | 213 | 11 |
    |* 2 | COUNT STOPKEY | | | | |
    | 3 | NESTED LOOPS | | 17 | 1207 | 10 |
    | 4 | MERGE JOIN CARTESIAN | | 1 | 32 | 9 |
    | 5 | VIEW | VIEW2 | 1 | 26 | 8 |
    | 6 | SORT UNIQUE | | 1 | 156 | 8 |
    | 7 | NESTED LOOPS | | 1 | 156 | 6 |
    | 8 | NESTED LOOPS | | 1 | 143 | 6 |
    | 9 | NESTED LOOPS OUTER | | 1 | 117 | 5 |
    |* 10 | HASH JOIN | | 1 | 104 | 5 |
    | 11 | NESTED LOOPS | | 1 | 52 | 2 |
    |* 12 | TABLE ACCESS FULL | TAB3 | 1 | 39 | 2 |
    |* 13 | INDEX UNIQUE SCAN | PK_TAB4 | 1 | 13 | |
    | 14 | TABLE ACCESS FULL | TAB5 | 82 | 4264 | 2 |
    | 15 | VIEW PUSHED PREDICATE | VIEW3 | 1 | 13 | |
    | 16 | NESTED LOOPS | | 1 | 52 | 2 |
    | 17 | NESTED LOOPS | | 1 | 39 | 2 |
    |* 18 | INDEX UNIQUE SCAN | PK_TAB6 | 1 | 13 | 1 |
    |* 19 | INDEX RANGE SCAN | PK_TAB7 | 1 | 26 | 1 |
    |* 20 | INDEX UNIQUE SCAN | PK_TAB8 | 1 | 13 | |
    | 21 | TABLE ACCESS BY INDEX ROWID| TAB9 | 3 | 78 | 1 |
    |* 22 | INDEX UNIQUE SCAN | PK_TAB9 | 1 | | |
    |* 23 | INDEX UNIQUE SCAN | PK_TAB10 | 1 | 13 | |
    | 24 | BUFFER SORT | | 1 | 6 | 9 |
    | 25 | TABLE ACCESS BY INDEX ROWID | TAB2 | 1 | 6 | 1 |
    |* 26 | INDEX RANGE SCAN | TAB2_IDX2 | 1 | | |
    |* 27 | TABLE ACCESS BY INDEX ROWID | TAB1 | 12 | 468 | 1 |
    |* 28 | INDEX UNIQUE SCAN | PK_TAB1 | 1 | | |
    Predicate Information (identified by operation id):
    2 - filter(ROWNUM<=3)
    10 - access("TAB5"."PATH_ID"="TAB4"."PATH_ID")
    12 - filter("TAB3"."AUTH_GROUP_ID"<>(-1) AND ("TAB3"."AUTH_GROUP_ID"=0 OR "TAB3"."AUTH_GROUP_ID"=1))
    13 - access("TAB3"."PATH_ID"="TAB4"."PATH_ID")
    18 - access("TAB5"."DOMAIN_ID"="TAB6"."DOMAIN_ID")
    19 - access("TAB6"."DOMAIN_ID"="TAB7"."DOMAIN_ID")
    20 - access("TAB7"."RULE_ID"="TAB8"."RULE_ID")
    22 - access("TAB9"."POLICY_ID"="TAB5"."POLICY_ID")
    23 - access("TAB9"."ASSOCIATED_FORM_ID"="TAB10"."FORM_ID")
    26 - access("TAB2"."CERT_SERIAL_NUM"=1022)
    27 - filter("VIEW2"."POLICY_ID"="TAB1"."POLICY_ID")
    28 - access("TAB1"."TRANSACTION_ID"="TAB2"."TRANSACTION_ID")
    Note: cpu costing is off
    52 rows selected.
    Elapsed: 00:00:03.37
    Execution Plan
    0 SELECT STATEMENT Optimizer=CHOOSE
    1 0 COLLECTION ITERATOR (PICKLER FETCH) OF 'DISPLAY'
    Statistics
    38 recursive calls
    0 db block gets
    287 consistent gets
    0 physical reads
    0 redo size
    5006 bytes sent via SQL*Net to client
    405 bytes received via SQL*Net from client
    5 SQL*Net roundtrips to/from client
    29 sorts (memory)
    0 sorts (disk)
    52 rows processed

  • TestStand Open SQL Statement does not support SQL's ORDER BY clause???

    TestStand 1.0.3
    Windows 2000 SP1
    SQL Server 2000 Personal
    You've got to be kidding me...
    It appears that the built-in TestStand Open SQL Step does NOT support the
    "ORDER BY" clause in the SELECT statement, even though the documentation
    says it does. Is this true?
    I have an Open SQL Statement query:
    "SELECT * FROM [MyTable] WHERE ([Batch ID]=1234)"
    it works fine, returning a correct record count 120 records. If I change
    the Open SQL Statement query simply by adding an ORDER BY clause, such as:
    "SELECT * FROM [MyTable] WHERE ([Batch ID]=1234) ORDER BY [MyField] ASC"
    it returns a record count of zero. I know that "MyField" exists in the
    MyTable table and contains valid data. The
    second query works fine in SQL
    Server Enterprise Manager.
    Am I missing something? Is it true that the TestStand Open SQL Step does
    NOT support the "ORDER BY" clause? If not, what &#$!ing good is it and why
    does the manual state it is supported? Is there any other way using just
    the TestStand steps to order a database recordset on one or more fields?
    Any help would be appreciated.
    Grrrrr....
    Bob Rafuse
    Etec Inc.

    > Bob -
    > The database step types do not do anything special to the SQL command
    > that you give it. The step just passes the command to the ADO
    > provider. I tried a simple query using the step types with the
    > following command,
    >
    > "SELECT UUT_RESULT.* FROM UUT_RESULT WHERE ([UUT_SERIAL_NUMBER] =
    > 12345) ORDER BY [EXECUTION_TIME] ASC"
    >
    > and this return the expected results and the record count parameter
    > was as expected. I tried this on TS 1.0.2 and TS 2.0 with MS Access
    > 2000 and MS SQL Server 7.0. I do not have MS SQL Server 2000 at this
    > time.
    >
    > It would be surprised if the step types are messing something up.
    I've been doing some experimenting over the past couple of days. Simple,
    one-table queries seem to handle the ORDER BY clause fine. Th
    ings seem to
    get messed up when I try multi-table queries with ORDER BY clause with the
    TestStand database steps. I get no errors but the returned record counts
    are always 0 with the ORDER BY and positive without the ORDER BY. The exact
    same queries work fine in Visual Basic/ADO and the SQL Server Query
    Analyzer.
    > Questions:
    > 1. Have you verified whether the data is actually returned even though
    > the record count is zero?
    Hmmm... yes data IS getting returned (at least on the two instances I just
    checked), but the record count is always zero. I was not proceeding with
    processing if the record count was 0.
    Still... I don't know how to loop through the recordset without knowing how
    many records there are an not eventually generate an error by passing EOF.
    Is there another way using the TestStand database steps to determine a) the
    number of records in the recordset or b) when I'm at EOF?
    > 2. Are you using any advanced options on the Opend SQL Statement step
    > type, specifically
    the cursor type set to forward only? Forward only
    > cursors do not allow for record counts.
    Everything on the Advanced tab of the Open SQL Statement step is set to "Use
    Default".
    Bob.

  • The ORDER BY clause is invalid in views, inline functions, derived tables..

    State: HY000. Code: 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)
    I have already tried to follow this thread below, but no change.
    HI
    In this specific report ran above the offending sql uses a CTE.
    I am on 11.1.1.6.1, Windows Server 2008
    Currently in testing phase in migration from 10g.
    I know what the error means, just not how to resolve it or what setting may be causing this.

    In Physical layer, go to specific database(Physical layer's) properties, database features tab -> Un check the EXPRESSION_IN_ORDERBY_SUPPORTED.
    For that failed report, go to Answers-> advance tab-> Look for 'Advanced SQL Clauses'
    Check this box to issue an explicit Select Distinct
    Let me know updates
    If helps Pls mark correct or helpful

Maybe you are looking for

  • Use of ABAP expression in the Send Mail Header and Text

    Hi Workflow Gurus, I have a requirement wherein I have to inform the Manager of a user that his/her request for leave availment has been approved. However, we need to show the users name for the Manager to easily identify the user. Now, I checked the

  • How to send external email of a Purchase Order

    Hello, I have to send an external email (pdf attachment) of a Purchase Order. I use the standard sap program: /SMB40/FM06P and the smartform: /SMB40/MMPO_L. Within transaction NACE I used the following entries: Medium: 5 (External Send), Program: /SM

  • How  to read all files  under a folder directory in FTP site

    Hi Experts, I use this SQL to read data from a file in FTP site. utl_file.fopen('ORALOAD', file_name,'r'); But this need to fixed file name in a directory. However, client generate output file with auto finename. SO do we have any way to read all fil

  • FBL3N Open items

    Hi, I have a requirement where for a G/L, i have to match the total for my report. I will be taking all open items from the G/L and it should match with my report output. But it is not matching. The problem is in G/L account from FBL3N some accounts

  • Mapping a wrapped business object

    Hi, I have a business security requirement (in our domain model) which can be described as an Administrator impersonating a normal business user. This allows for key state changes to be performed on behalf of a user (so that they maintain ownership o