Creating order in order by clause
hi
say in a data set i have value like
col1
sam,tony,park,john
without oracle's default order by clause how can i print data like
tony,park,sam,john (it amy be any order without oracle's desc or asc)
thanx
Hi,
but if col1 has string value as i mentioned in first post the how rows can be ordered? see here
SQL> select ename from emp
2 order by dbms_random.value;
ENAME
MARTIN
FORD
ALLEN
JONES
SMITH
TURNER
JAMES
MILLER
WARD
ADAMS
CLARK
ENAME
SCOTT
BLAKE
KING
14 rows selected.
SQL> /
ENAME
ALLEN
JONES
CLARK
BLAKE
SCOTT
ADAMS
KING
SMITH
FORD
JAMES
MARTIN
ENAME
WARD
TURNER
MILLER
14 rows selected.
SQL>
[\pre]
Now you can get the idea...
Similar Messages
-
Designer BC4J Generator Extension creates wrong order by clause
Hi,
Just tried out the new Designer BC4J Generator Extension and it creates a wrong order by clause. I have a List of values in Designer with one table usage. There is a bound item based on a columnname CODE and has the name L_CODE. The order by sequence is 1 and ascending. After generating it in JDeveloper the generated query is:
SELECT PrjEntityUsage.CODE,
PrjEntityUsage.NAAM
FROM X_PROJECTEN PrjEntityUsage
ORDER BY LCODE
So there are two problems:
1) If the name property in designer is different from the column name the select in the SQL query in JDeveloper needs an alias with the name property from Designer.
2)If there is an _ in the name it is ignored in the order by clause in JDeveloper.
I do not know where to report the bug so i placed it here.
Regards,
Andre JochemsAndre,
Thanks for notifying us about these. There are indeed bugs which we have logged and scheduled for a fix.
Regards
Sue Harper -
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.
RonnyThanks 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
nullSorry 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 PL/SQL cursors
I am trying to execute a procedure with some input parameters. I open a cursor
with a select statement. However, the order by clause in the query does not
recognize parameter sent through the procedure input parameters.
For example:
open <<cursor name>> for
select id from member order by <<dynamic parameter>>" does not work (Compiles fine but does not return the right result).
But if I try and give a static order by <<column name>> it works. Is the
order by clause in the PL/SQL a compile time phenomenon?
I have also tried it through dynamic sql. All the other parameters work except the order by <<parameter>> asc|desc
Also "asc" and "desc" does not work if given dynamically.
What alternatives do I have?
nullI don't think order by can be dynamic in a cursor, but it sure can be using dynamic sql. The only issue is that you must do a replace in the sql string with the dynamic variable. For example:
create or replace procedure test_dyn(p_col in varchar2, p_order in varchar2) as
q varchar2(500);
u_exec_cur number;
u_columnnumber NUMBER;
u_columndate DATE;
u_columnvarchar varchar2(50);
u_cur_count number;
u_ename varchar2(20);
u_sal number;
begin
q := 'select ename, sal from scott.emp order by p_col p_order';
-- got to do these two replaces
q:= replace(q,'p_col',p_col);
q:= replace(q,'p_order',p_order);
u_exec_cur := dbms_sql.open_cursor;
dbms_sql.parse(u_exec_cur,q,dbms_sql.v7);
dbms_sql.define_column(u_exec_cur, 1, u_columnvarchar, 20);
dbms_sql.define_column(u_exec_cur, 2, u_columnnumber);
u_cur_count := dbms_sql.execute(u_exec_cur);
loop
exit when (dbms_sql.fetch_rows(u_exec_cur) <= 0);
dbms_sql.column_value(u_exec_cur, 1, u_ename);
dbms_sql.column_value(u_exec_cur, 2, u_sal);
dbms_output.put_line(u_ename);
dbms_output.put_line(u_sail);
--htp.p(u_ename);
--htp.p(u_sal);
end loop;
end;
show errors;
Now when when I execute my procedure I can change the order by clause all I want, for example:
SQL> set serveroutput on;
SQL> exec gmika.test_dyn('sal','asc');
SMITH
800
ADAMS
1100
WARD
1250
MARTIN
1250
MILLER
1300
TURNER
1500
ALLEN
1600
JLO
2222
BLAKE
2850
JONES
2975
SCOTT
3000
FORD
3000
LOKITZ
4500
KING
5000
JAMES
5151
JAMES
5555
PL/SQL procedure successfully completed.
SQL>
null -
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,
TandraAccording 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). -
Order by clause in discoverer custom folder
hi,
i m working on discoverer 3.1.38.I've been trying to sort the period according to calendar sequence and for this i tried 2-3 different queries which are working fine on sql plus but in discoverer custome folder,one error is coming saying ..can't use ORDER BY clause in custom folder.
I've tried formatting option also but it doesn't work here with me as i preferably want to sort in MON-RR format.
thanks in anticipation
nullthanks for replying back. but i didn't get what exactly u meant by creting query in user edition and about alternative sort,i should have list of value item and item_to_sort in same folder but i don't have that..i am creating one cross tab report and using to_char(gl_date,'MON-RR') format on my top axis its' appearing alphabetically over there,i wrote one qurey which worked prefecly fine on sql plus,but in discoverer its' giving me error.i can't create alternative sort on this,so please give me some different solution
thanks in advance -
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 AMyou 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 advanceThanks 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 -
Sorting character column ( used in order by clause dynamically)
Hi,
I need help on sorting character-based numbers. Like say, I want to sort customers based on street numbers(which is a character string being used in the
order by clause) they live in.
The criteria are :
i. Numbers must take precedence.
This being a character string, 1000001 comes before 2. This shouldn't happen. And you cannot use to_number
since using it with a string having characters in it would raise an error.
ii. If only a single alphabet occurs as the last character, then treat the whole string as a number except the last character and then sort it
as if sorting a number. Something like : if you have 1000A, 200D, 200B, 1000X, the result would be 200B,200D,1000A,1000X.
iii. if a character occurs elsewhere in the string, then perform the search normally as if performing a character search.
The output of the following data :
100
A101
B100A
110C
C120B
120
100020
120C
C1100
100D
would be like :
100
100D
110C
120
120C
100020
A101
B100A
C120B
C1100
Please note that the sort is being done dynamically, so I could have access to the values of the street numbers only during run time.
Any help is really appreciated.
Thanks in advance.
Regards,
Anil.Create a function to test whether the column is numeric :
create FUNCTION is_numeric(v_number VARCHAR2)
RETURN INTEGER
IS
l_number NUMBER;
BEGIN
IF INSTR(UPPER(v_number),'E') > 0 THEN
RETURN 0;
END IF;
l_number := TO_NUMBER(v_number);
RETURN 1;
EXCEPTION
WHEN OTHERS THEN
RETURN 0;
END;
And try this query
Assume the table name is TEST with column STREET
select * from TEST
order by case is_numeric(STREET) when 1 then LPAD(STREET,20, ' ') else STREET end
Please make sure that 20 mentioned above is the column size for STREET.
Hope this helps.
-Nags -
JPQL - No UPPER() in ORDER BY clause??
I have created a NamedQuery and am attempting to order my returned collection using the ORDER BY clause, however, the specification asks for string ordering to be case insensitive. It seems that I cannot do that with the Java Persistence Query Language - or at least the implementation that we are using. I am using Glassfish v2ur2 with uses Oracle TopLink Essentials - 2.0.1, Build b04-fcs (found in my Exception message).
I suspect that the only way to do what I want is to create a Comparator for the ResultList and run a sort after I get the result list from my EntityManager. Is that correct or is my syntax wrong in my JPQL?
Here is my named query (cut from a larger list of named queries), pretty simple:
@NamedQuery(name = "MyObject.findAll", query = "SELECT a FROM MyObject a ORDER BY UPPER(a.myString), a.Id")Here is a summary exception report that I receive:
===========================================================================
Exception: javax.ejb.EJBException
Description: nested exception is: java.rmi.ServerException: RemoteException occurred in server thread; nested exception is:
java.rmi.RemoteException: null; nested exception is:
Exception [TOPLINK-8024] (Oracle TopLink Essentials - 2.0.1 (Build b04-fcs (04/11/2008))): oracle.toplink.essentials.excepti
ons.EJBQLException
Exception Description: Syntax error parsing the query [MyObject.findAll: SELECT a FROM MyObject a WHERE ORDER BY UPPER(a.myString), a.Id], line 1, column 81: syntax error at [UPPER].
Internal Exception: line 1:81: expecting IDENT, found 'UPPER'
(Stack-Trace Included)
===========================================================================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 -
Is parameter in ORDER BY clause possible?
I'm using a function to return a ref cursor and currently pass a parameter without any problems. I would like to change the sort on the fly by passing a parameter to the order by clause, but Oracle ignores it.
CREATE OR REPLACE PACKAGE pkg_agent_appt_status AS
TYPE rcur IS REF CURSOR;
FUNCTION f_agent_appt_status (ssn IN VARCHAR2, sort_str IN VARCHAR2) RETURN rcur;
END pkg_agent_appt_status;
CREATE OR REPLACE PACKAGE BODY pkg_agent_appt_status AS
FUNCTION f_agent_appt_status (ssn IN VARCHAR2, sort_str IN VARCHAR2)
RETURN rcur
IS
retval rcur;
BEGIN
OPEN retval FOR
SELECT agncy.CORPORATE_NAME "Agency Name",
agnt_state.APPT_STATE "State",
agnt.AGENT_STATUS "Appt Status",
TO_CHAR(agnt_state.APPT_STATE_EFF_DT,'mm/dd/yyyy') "Effective Date",
agnt.AGENT_NUMBER "Agent ID",
agnt.AGENT_STATUS "Agent Status",
STATE.STATE_NAME
FROM AGNT_APPT_STAT_PRDCR_WRK agnt,
AGNT_APPT_STAT_WRK agncy,
AGNT_APPT_STATE_STAT_PRDCR_WRK agnt_state,
STATE
WHERE agnt.AGENT_TAX_ID = ssn
AND agnt.COMPANY_CODE = agncy.COMPANY_CODE
AND agnt.PARENT_AGENT_AGENCY_ID = agncy.AGENT_NUMBER
AND agnt.COMPANY_CODE = agnt_state.COMPANY_CODE
AND agnt.AGENT_AGENCY_ID = agnt_state.AGENT_AGENCY_ID
AND agnt.AGENT_NUMBER = agnt_state.AGENT_NUMBER
AND agnt_state.APPT_STATE = STATE.STATE_CODE
ORDER BY
sort_str;
RETURN retval;
END f_agent_appt_status;
END pkg_agent_appt_status;If you want to do this, you'd have to use dynamic SQL (execute immediate or DBMS_SQL). For the easier 'execute immediate' approach, you'd do something like
create or replace someProc( someArg varchar2 )
as
strSQL varchar2(4000)
begin
strSQL := <<string containing your SQL statement up to the order by clause>>
strSQL := strSQL || 'ORDER BY ' || someArg
execute immediate strSQL;
endJustin -
Evaluate Expression in Order By Clause
Hi,
I'm having trouble to execute following JPQL.
Select r.firstname, r.lastname, (e.distance/e.duration) from Runner r Inner Join r.event e Order BY (e.distance/e.duration) Desc;
Error suggests that '(' character is unacceptable in Order By Clause. Here is the error.
javax.servlet.ServletException: java.lang.IllegalArgumentException: An exception occurred while creating a query in EntityManager:
Exception Description: Syntax error parsing the query [Select r.firstname, r.lastname, (e.distance/e.duration) from Runner r Inner Join r.event e Order BY (e.distance/e.duration) Desc;], line 1, column 101: unexpected token [(].
Internal Exception: NoViableAltException(81!=[1328:1: orderByItem returns [Object node] : (n= stateFieldPathExpression (a= ASC | d= DESC | ) | i= IDENT (a= ASC | d= DESC | ) );])
Can someone please show me how to fix it.
Thanks,
AdamHello Adam
You cannot use "(e.distance/e.duration)" as an order by clause. The correct query would be:
"Select r.firstname, r.lastname, (e.distance/e.duration) as speed from Runner r Inner Join r.event e Order BY speed Desc"
but will not work until bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=331575 is fixed. Please vote for it.
Best Regards,
Chris -
[JPA/TOPLINK] is the function "lower" supported in "order by" clause?
In EJB-QL
I can use lower() or upper() in where clause.
But there is always a parse exception thrown when i tried to use it in main clause or order by clause.
works:
select s from Student s where lower(s.name) like 'm%'
exception thrown:
select s from Student s where s.name like 'm%' order by lower(s.name)
OR --------------
Why i am asking this is that the resultset returned from database is not alphabetical sorted but ascii sorted, which means any characters with upper case is always prior to the ones with lower case.
If EJB-QL doesn't support using lower() in order by clause, do I have any other options to avoid this problem?
BTW, it is the Oracle 10g we are using as DB
many thanks,
Xuphey
Edited by: Xuphey on Nov 29, 2007 1:33 AMIf you want to do this, you'd have to use dynamic SQL (execute immediate or DBMS_SQL). For the easier 'execute immediate' approach, you'd do something like
create or replace someProc( someArg varchar2 )
as
strSQL varchar2(4000)
begin
strSQL := <<string containing your SQL statement up to the order by clause>>
strSQL := strSQL || 'ORDER BY ' || someArg
execute immediate strSQL;
endJustin -
Using Decode in Order By clause
Hi all,
I've a prblem with the DECODE in ORDER BY clause
Could you please advise if I did something wrong with my sort order.
Actually,I've a store procedure which gather all information from several tables and return with a resultset.
Therefore I just post an example table_resultset instead of lenghthy StoreProc in forum
/* table contain the column need to be sort on the resultset */
create table table_sort (sortnum,sortname) as
select 1553, 'IDNO' from dual union all
select 1231, 'IDNAME' from dual union all
select 1001, 'CREDATE' from dual;
/* a sample of some information that resultset return by a storeproc */
create table table_resultset (idno,idname,credate) as
select 1111, 'SORT ORDER', sysdate-7 from dual union all
select 54555, 'NEED A TEST CASE', sysdate+5 from dual union all
select 10012, 'BEYOND THE LIMIT', sysdate from dual union all
select 10522, 'CONCENTRATION', sysdate+20 from dual union all
select 01231, 'A VALIDATION', sysdate-3 from dual;
/* this select statement similar to execute a storeproc with s decode in the ORDER BY clause */
select * from table_resultset
order by decode((select ltrim(rtrim(sortname)) from table_sort where sortnum=1231),'IDNO',1,'IDNAME',2,3);
OR
select * from table_resultset
order by case (select ltrim(rtrim(sortname)) from table_sort where sortnum=1231)
when 'IDNO' then 1
when 'IDNAME' then 2
else 3
end;
Thanks.Thanks for the tip Samb ... :)
I got it ... all sort field must be same datatype.
SQL> create table table_sort (sortnum,sortname) as
2 select 1553, 'IDNO' from dual union all
3 select 1231, 'IDNAME' from dual union all
4 select 1001, 'CREDATE' from dual;
Table created.
SQL> create table table_resultset (idno,idname,credate) as
2 select 1111, 'SORT ORDER', sysdate-7 from dual union all
3 select 54555, 'NEED A TEST CASE', sysdate+5 from dual union all
4 select 10012, 'BEYOND THE LIMIT', sysdate from dual union all
5 select 10522, 'CONCENTRATION', sysdate+20 from dual union all
6 select 01231, 'A VALIDATION', sysdate-3 from dual;
Table created.
SQL> select * from table_resultset;
IDNO IDNAME CREDATE
1111 SORT ORDER 08-07-03
54555 NEED A TEST CASE 08-07-15
10012 BEYOND THE LIMIT 08-07-10
10522 CONCENTRATION 08-07-30
1231 A VALIDATION 08-07-07
SQL> select * from table_sort;
SORTNUM SORTNAM
1553 IDNO
1231 IDNAME
1001 CREDATE
if I want to sort the resultset by the column IDNAME of table_resultset then
SQL> select * from table_resultset
2 order by decode((select ltrim(rtrim(sortname)) from table_sort
3 where sortnum=1231),'IDNO',to_char(idno),'IDNAME',idname,to_char(credate,'yyyymmdd'));
IDNO IDNAME CREDATE
1231 A VALIDATION 08-07-07
10012 BEYOND THE LIMIT 08-07-10
10522 CONCENTRATION 08-07-30
54555 NEED A TEST CASE 08-07-15
1111 SORT ORDER 08-07-03
if I want to sort the resultset by the column CREDATE of table_resultset then
SQL> select * from table_resultset
2 order by decode((select ltrim(rtrim(sortname)) from table_sort
3 where sortnum=1001),'IDNO',to_char(idno),'IDNAME',idname,to_char(credate,'yyyymmdd'));
IDNO IDNAME CREDATE
1111 SORT ORDER 08-07-03
1231 A VALIDATION 08-07-07
10012 BEYOND THE LIMIT 08-07-10
54555 NEED A TEST CASE 08-07-15
10522 CONCENTRATION 08-07-30
But if I want to sort the resultset by the column IDNO of table_resultset then I've a problem due to the field converted into character, and the field IDNO define as number(9) of a table in production. therefore I've to find a solution when that field convert into character must be same length in order to sort IDNO correctly. As you can see from below
SQL> select * from table_resultset
2 order by decode((select ltrim(rtrim(sortname)) from table_sort
3 where sortnum=1553),'IDNO',to_char(idno),'IDNAME',idname,to_char(credate,'yyyymmdd'));
IDNO IDNAME CREDATE
10012 BEYOND THE LIMIT 08-07-10
10522 CONCENTRATION 08-07-30
1111 SORT ORDER 08-07-03
1231 A VALIDATION 08-07-07
54555 NEED A TEST CASE 08-07-15
your suggestion always welcome.
Thanks,
DT.
Maybe you are looking for
-
Re. installing/enabling a Flash plugin 1 I'm running Firefox version 3.6.10 on Windows XP SP3. 2 Three days ago Secunia PSI informed me the installed version of Flash was a security threat so I uninstalled it then installed the latest version 10.1.85
-
My g62 was down for 2 months while I waited for money to replace the hard drive and when I replaced it with a 500 gb hitatchi it will no longer power on.. It charges fine but the power button does nothing when pressed whether the battery is in or out
-
Bluetooth and calls are not working properly
Please somebody help me!!! I have a Hyundai Elantra 2012 and a Curve 9300, the bluetooth has been working fine for a year, however my phone is having problems to connect to my car, what is funny is that I can watch a video and the sound will come out
-
Where is the location to download the software to update it? It is bundled with the latest version of iTunes? I want to download it on a mac with an internet connection and transfer it to another mac that connects to the Touch that does not have inte
-
HELP! Problems with MacBook after software updates install.
Hi Guys, I have a friend of mine that has a MacBook. After a while without doing the software updates she connected her MacBook on the internet and there was a lot of software updates to do, so she went on and install everything. She describes me tha