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,
Adam
Hello 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
Similar Messages
-
Can I use an expression in order by clause
SELECT
PERIOD_NUM, period_num *12,
period_name,
--TO_DATE(SUBSTR(PERIOD_NAME,1,3)| |SUBSTR(PERIOD_NAME,INSTR(PERIOD_NAME,'-'),3),'MON-RR'),
TO_NUMBER(TO_CHAR(TO_DATE((SUBSTR(PERIOD_NAME,1,3)| |SUBSTR(PERIOD_NAME,INSTR(PERIOD_NAME,'-')+1,2)),'MONRR'),'RRRRMM'))
FROM GL.GL_PERIODS
order by 2--TO_NUMBER(TO_CHAR(TO_DATE(SUBSTR(PERIOD_NAME,1,3)| |SUBSTR(PERIOD_NAME,INSTR(PERIOD_NAME,'-')+1,2),'MONRR'),'RRRRMM'))
desc
gl_periods is a table, period_name consists of values like oct-01_fy-01 etc and oct01_01_fy-01
I want to sort on this column based on period_name, so I am trying to convert into number like for oct-01_fy-01, the query gives
200110 and so on. the recent period comes first, But my question is I can't use the same to_number(.....) in the order by clause. oracle gives an error
ORA-01843: not a valid month.
Let me know whether anyone come across with this kind of situationIf you are sure the expression is correct, since you are using it in the select clause, give an alias and use the alias in the order by clause.
null -
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? -
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. -
Hi,
Can we use order by clause in Sub query?
While using the order by clause, I am getting the "missing expression error" . If I remove order by clause query executing fine.
Here is my query:
select *
from emp_mstr
where emp_no in(select
emp_no
from emp_mstr
order by branch_no);
Thanks & Regards,
MahiMay be you miss some required spaces also, other than wrong use of ORDER BY
select *
from emp_mstr
where emp_no in
( select e2.emp_no
from emp_mstr e2
-- order by e2.branch_no
);Why do you want to ORDER BY in the subquery, which you use with IN clause? That will not make any difference in the result..Means the result you get with ORDER BY will be same as without that.. And in this case, ORDER by is a unncessary overhead.. And Ordering is very costly..
And why do you want to have the IN clause at all in your query? You are referring the same tables in the main query and sub query..
The below will give the same result
select *
from emp_mstr
where emp_no is not nullIf you want to use another table in the subquery, always use aliasess...
select *
from emp_mstr
where emp_no in
( select e2.emp_no
from emp_mstr2 e2
-- order by e2.branch_no
); -
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). -
[10g] Need help with order by clause in hierarchical query
I have the following sample data:
CREATE TABLE bill_test1
( parent_part CHAR(25)
, child_part CHAR(25)
, line_nbr NUMBER(5)
, qty_per NUMBER(9,5)
INSERT INTO bill_test1 VALUES ('ABC-1','ABC-10',100,1);
INSERT INTO bill_test1 VALUES ('ABC-1','ABC-20',200,2);
INSERT INTO bill_test1 VALUES ('ABC-1','ABC-30',300,3);
INSERT INTO bill_test1 VALUES ('ABC-1','HARDWARE-1',401,10);
INSERT INTO bill_test1 VALUES ('ABC-1','HARDWARE-2',402,5);
INSERT INTO bill_test1 VALUES ('ABC-10','ABC-155',100,2);
INSERT INTO bill_test1 VALUES ('ABC-10','HARDWARE-1',200,1);
INSERT INTO bill_test1 VALUES ('ABC-155','RAW-2',100,4.8);
INSERT INTO bill_test1 VALUES ('ABC-155','HARDWARE-3',200,3);
INSERT INTO bill_test1 VALUES ('ABC-20','RAW-1',100,10.2);
INSERT INTO bill_test1 VALUES ('ABC-30','RAW-3',100,3);And the query below gives me exactly what I want, in the order I want it. However, I am wondering if there is a way to get this order without creating the SEQ column, since I don't need it in my results
SELECT part_nbr
, parent_part
, child_part
FROM (
SELECT CONNECT_BY_ROOT b.parent_part AS part_nbr
, b.parent_part
, b.child_part
, SYS_CONNECT_BY_PATH(b.line_nbr,' ') AS seq
FROM bill_test1 b
, dual
CONNECT BY parent_part = PRIOR child_part
WHERE part_nbr = 'ABC-1'
ORDER BY seq
Results of above query, except with SEQ included in SELECT (just to show what I'm sorting off of):
PART_NBR PARENT_PART CHILD_PART SEQ
ABC-1 ABC-1 ABC-10 100
ABC-1 ABC-10 ABC-155 100 100
ABC-1 ABC-155 RAW-2 100 100 100
ABC-1 ABC-155 HARDWARE-3 100 100 200
ABC-1 ABC-10 HARDWARE-1 100 200
ABC-1 ABC-1 ABC-20 200
ABC-1 ABC-20 RAW-1 200 100
ABC-1 ABC-1 ABC-30 300
ABC-1 ABC-30 RAW-3 300 100
ABC-1 ABC-1 HARDWARE-1 401
ABC-1 ABC-1 HARDWARE-2 402Hi,
As long as there's only one root, you can say ORDER SIBLINGS BY, but you can't do that in a sub-query (well, you can, but usually there's no point in doing it in a sub-query). If the CONNECT BY is being done in a sub-query, there is no guarantee that the main query will preserve the hierarchical order that the sub-query provides.
The query you posted doesn't require a suib-query, so you can say:
SELECT CONNECT_BY_ROOT b.parent_part AS part_nbr
, b.parent_part
, b.child_part
--, SYS_CONNECT_BY_PATH(b.line_nbr,' ') AS seq
FROM bill_test1 b
WHERE CONNECT_BY_ROOT b.parent_part = 'ABC-1'
CONNECT BY parent_part = PRIOR child_part
ORDER SIBLINGS BY b.line_nbr
;I said the query you posted doesn't require a sub-query. It also doesn't require dual, so I suspect what you posted is a simplification of what you're really doing, and that may need a sub-query. In particular, if you intend to GROUP BY part_nbr, then you need the sub-query. We can repeat the CONNECT_BY_ROOT expression in the WHERE clause (or, now that I think about it, use a START WITH clause instead of WHERE), but, for some reason, we can't use CONNECT_BY_ROOT in a GROUP BY clause; we need to compute CONNECT_BY_ROOT in a sub-query, give it a name (like part_nbr), and GROUP BY that column in a super-query.
This assumes that there is only one root node. ORDER SIBLINGS BY means just that: children of a common parent will appear in order, but the root nodes, who have no parents, will not necessarily be in order.
Here's what I meant by using START WITH instead of WHERE:
SELECT CONNECT_BY_ROOT b.parent_part AS part_nbr
, b.parent_part
, b.child_part
--, SYS_CONNECT_BY_PATH(b.line_nbr,' ') AS seq
FROM bill_test1 b
START WITH b.parent_part = 'ABC-1'
CONNECT BY parent_part = PRIOR child_part
ORDER SIBLINGS BY b.line_nbr
;This should be much more efficient, because it narrows down the results before you waste time getting their descendants.
Using a START WITH clause here is analagous to me sending you an e-mail, saying "Come to a meeting a my office at 3:00."
Using a WHERE clause here is analagous to me sending an e-mail to everyone in the company, saying "Come to a meeting a my office at 3:00", and then, as people get here, telling everyone except you that they can go back.
ORDER SIBLINGS BY was introduced in Oracle 9.
Edited by: Frank Kulash on Dec 9, 2010 2:39 PM
Added version with START WITH clause -
Order by clause...
Hi everyone,
How can i achieve this with order by clause
If i have the following data in my table. I need one record (which has the data 'Last record')
to be the last record for my select statement, and order the other records according to "no"
whatever data i have i need to arrange that record last
select * from
select 'A' order_by_test,5 no from dual
union all
select 'B' order_by_test,1 no from dual
union all
select 'C' order_by_test,8 no from dual
union all
select 'Last record' order_by_test,3 no from dual
union all
select 'Z' order_by_test,7 no from dual
union all
select null order_by_test,null no from dual
union all
select ' ' order_by_test,null no from dual
output:
=======
order_by_test no
B 1 -->other records ,order by no nulls last
A 5
Z 7
C 8
null null
' ' null
Last record 3 ----> everytime this should be the last record of my select statement,I am sorry if it is a dumb question to ask.
Thanks in advanceHi,
Use CASE (or equivalents) to map your values into sortable values.
ORDER BY CASE
WHEN order_by_test = 'Last record'
THEN 2
ELSE 1
END
, noIf the sort order depends on two different columns, it's often best to have two different expressions in the ORDER BY clause.
SET NULL [NULL]
select * from
select 'A' order_by_test,5 no from dual
union all
select 'B' order_by_test,1 no from dual
union all
select 'C' order_by_test,8 no from dual
union all
select 'Last record' order_by_test,3 no from dual
union all
select 'Z' order_by_test,7 no from dual
union all
select null order_by_test,null no from dual
union all
select ' ' order_by_test,null no from dual
ORDER BY CASE
WHEN order_by_test = 'Last record'
THEN 2
ELSE 1
END
, no
ORDER_BY_TE NO
B 1
A 5
Z 7
C 8
[NULL]
[NULL] [NULL]
Last record 3Edited by: Frank Kulash on Mar 31, 2010 1:20 PM
Added full query and output -
[10g] Complex Order By clause...
I can manually sort my data, and can provide the logic I use to do that, but I need help actually putting it into an order by clause.
I am working in XE, 10g...
Here's some sample data to start with:
CREATE TABLE inventory_history
( time_stamp CHAR(20)
, part_nbr CHAR(25)
, act_date DATE
, ord_nbr CHAR(10)
, ln_nbr CHAR(3)
, code_pre CHAR(3)
, code_sfx CHAR(3)
, user_id CHAR(36)
, qty NUMBER(15,4)
, adj_qty NUMBER(15,4)
, cnt_qty NUMBER(15,4)
INSERT INTO inventory_history
VALUES ('20080716100442488094','N174X53A',TO_DATE('07/16/2008','mm/dd/yyyy'),NULL,NULL,'MTL','INS','user2',0,0,31);
INSERT INTO inventory_history
VALUES ('20080717135714597592','N174X53A',TO_DATE('07/17/2008','mm/dd/yyyy'),NULL,NULL,'MTL','INS','user2',31,0,0);
INSERT INTO inventory_history
VALUES ('20100401145927954563','N174X53A',TO_DATE('04/01/2010','mm/dd/yyyy'),NULL,NULL,'MTL','INS','user1',0,0,31);
INSERT INTO inventory_history
VALUES ('20100917085814607499','N174X53A',TO_DATE('09/17/2010','mm/dd/yyyy'),NULL,NULL,'MTL','INS','user1',30,0,0);
INSERT INTO inventory_history
VALUES ('20080715093021657035','N174X53A',TO_DATE('07/15/2008','mm/dd/yyyy'),'P000012345','001','POR','LIN','user5',31,0,0);
INSERT INTO inventory_history
VALUES ('20080716100406218646','N174X53A',TO_DATE('07/16/2008','mm/dd/yyyy'),'P000012345','001','MTL','INS','user2',31,0,0);
INSERT INTO inventory_history
VALUES ('20080819111303812239','N174X53A',TO_DATE('08/19/2008','mm/dd/yyyy'),'P000012345','002','POR','LIN','user5',31,0,0);
INSERT INTO inventory_history
VALUES ('20100504083427334677','N174X53A',TO_DATE('05/04/2010','mm/dd/yyyy'),'P000012345','002','POR','RTV','user3',31,0,0);
INSERT INTO inventory_history
VALUES ('20100512095750285578','N174X53A',TO_DATE('05/12/2010','mm/dd/yyyy'),'P000012345','002','POR','LIN','user6',30,0,0);
INSERT INTO inventory_history
VALUES ('20100614130143734676','N174X53A',TO_DATE('06/14/2010','mm/dd/yyyy'),'P000012345','002','MTL','INS','user1',0,0,30);
INSERT INTO inventory_history
VALUES ('20100412101824624708','N174X53A',TO_DATE('04/12/2010','mm/dd/yyyy'),'P000012345','004','POR','LIN','user4',31,0,0);
INSERT INTO inventory_history
VALUES ('20100422150057876187','N174X53A',TO_DATE('04/22/2010','mm/dd/yyyy'),'P000012345','004','MTL','INS','user2',0,0,31);
INSERT INTO inventory_history
VALUES ('20100504081750020711','N174X53A',TO_DATE('05/04/2010','mm/dd/yyyy'),'P000012345','004','POR','RTV','user3',31,0,0);
INSERT INTO inventory_history
VALUES ('20100512095303832580','N174X53A',TO_DATE('05/12/2010','mm/dd/yyyy'),'P000012345','004','POR','LIN','user6',31,0,0);
INSERT INTO inventory_history
VALUES ('20100712133833381506','N174X53A',TO_DATE('07/12/2010','mm/dd/yyyy'),'P000012345','004','MTL','INS','user1',31,0,0);
INSERT INTO inventory_history
VALUES ('20091124130834762244','015-279A',TO_DATE('11/24/2009','mm/dd/yyyy'),'P000045557','002','POR','LIN','user12',10,0,0);
INSERT INTO inventory_history
VALUES ('20091124131110241744','015-279A',TO_DATE('11/24/2009','mm/dd/yyyy'),'P000045557','002','POR','ADJ','user12',0,10,0);
INSERT INTO inventory_history
VALUES ('20091124131850641554','015-279A',TO_DATE('11/24/2009','mm/dd/yyyy'),'P000045557','002','POR','ADJ','user12',0,-20,0);
INSERT INTO inventory_history
VALUES ('20091124131916249134','015-279A',TO_DATE('11/24/2009','mm/dd/yyyy'),'P000045557','002','POR','LIN','user12',10,0,0);
INSERT INTO inventory_history
VALUES ('20091124135601215291','015-279A',TO_DATE('11/24/2009','mm/dd/yyyy'),'P000045557','002','POR','ADJ','user12',0,10,0);
INSERT INTO inventory_history
VALUES ('20091124140318980761','015-279A',TO_DATE('11/24/2009','mm/dd/yyyy'),NULL,NULL,'MTL','INS','user13',10,0,0);
INSERT INTO inventory_history
VALUES ('20091124142344235001','015-279A',TO_DATE('11/24/2009','mm/dd/yyyy'),'P000045557','002','POR','ADJ','user12',0,-10,0);
INSERT INTO inventory_history
VALUES ('20100701123833300000','test',TO_DATE('07/01/2010','mm/dd/yyyy'),'P000011111','001','POR','LIN','user12',5,0,0);
INSERT INTO inventory_history
VALUES ('20100705964833300000','test',TO_DATE('07/05/2010','mm/dd/yyyy'),'P000011111','001','MTL','INS','user2',5,0,0);
INSERT INTO inventory_history
VALUES ('20100702123456700000','test',TO_DATE('07/02/2010','mm/dd/yyyy'),'P000011111','002','POR','LIN','user6',5,0,0);
INSERT INTO inventory_history
VALUES ('20100704987654300000','test',TO_DATE('07/04/2010','mm/dd/yyyy'),NULL,NULL,'MTL','INS','user1',5,0,0);And this is what I'm looking to do ... this is how I want the above data sorted:
TIME_STAMP PART_NBR ACT_DATE ORD_NBR LN_NBR CODE_PRE CODE_SFX USER_ID QTY ADJ_QTY CNT_QTY
20091124130834762244 015-279A 11/24/2009 P000045557 002 POR LIN user12 10 0 0
20091124131110241744 015-279A 11/24/2009 P000045557 002 POR ADJ user12 0 10 0
20091124131850641554 015-279A 11/24/2009 P000045557 002 POR ADJ user12 0 -20 0
20091124131916249134 015-279A 11/24/2009 P000045557 002 POR LIN user12 10 0 0
20091124135601215291 015-279A 11/24/2009 P000045557 002 POR ADJ user12 0 10 0
20091124140318980761 015-279A 11/24/2009 MTL INS user13 10 0 0
20091124142344235001 015-279A 11/24/2009 P000045557 002 POR ADJ user12 0 -10 0
20080715093021657035 N174X53A 7/15/2008 P000012345 001 POR LIN user5 31 0 0
20080716100406218646 N174X53A 7/16/2008 P000012345 001 MTL INS user2 31 0 0
20080716100442488094 N174X53A 7/16/2008 MTL INS user2 0 0 31
20080717135714597592 N174X53A 7/17/2008 MTL INS user2 31 0 0
20080819111303812239 N174X53A 8/19/2008 P000012345 002 POR LIN user5 31 0 0
20100401145927954563 N174X53A 4/1/2010 MTL INS user1 0 0 31
20100512095750285578 N174X53A 5/12/2010 P000012345 002 POR LIN user6 30 0 0
20100614130143734676 N174X53A 6/14/2010 P000012345 002 MTL INS user1 0 0 30
20100412101824624708 N174X53A 4/12/2010 P000012345 004 POR LIN user4 31 0 0
20100422150057876187 N174X53A 4/22/2010 P000012345 004 MTL INS user2 0 0 31
20100512095303832580 N174X53A 5/12/2010 P000012345 004 POR LIN user6 31 0 0
20100712133833381506 N174X53A 7/12/2010 P000012345 004 MTL INS user1 31 0 0
20100917085814607499 N174X53A 9/17/2010 MTL INS user1 30 0 0
20100701123833300000 test 7/1/2010 P000011111 001 POR LIN user12 5 0 0
20100705964833300000 test 7/5/2010 P000011111 001 MTL INS user2 5 0 0
20100702123456700000 test 7/2/2010 P000011111 002 POR LIN user6 5 0 0
20100704987654300000 test 7/4/2010 MTL INS user1 5 0 0This is the logic I use to sort the data manually:
If the record has ord_nbr/ln_nbr (they aren't NULL), then sort by part_nbr, ord_nbr, ln_nbr, and time_stamp, in that order.
And here's the complicated part...
If the record does not have ord_nbr/ln_nbr (they are NULL, and this can only happen on MTL-INS transactions), then it needs to be placed into the sort I just described via this logic:
It must be placed with the other records with the same part_nbr, after the closest previous POR LIN transaction by time_stamp (for that part_nbr), but before the next chronological MTL-INS transaction by time_stamp (for that part_nbr), and between those 2 constraints, fit in by time_stamp.
Any suggestions??
Edited by: user11033437 on Sep 23, 2010 3:13 PM (Fixed order of sample data, so part_nbr was ascending, though really the overall sort doesn't matter, just the sorting within each part_nbr group.)Hi,
user11033437 wrote:
... And this is what I'm looking to do ... this is how I want the above data sorted:
TIME_STAMP PART_NBR ACT_DATE ORD_NBR LN_NBR CODE_PRE CODE_SFX USER_ID QTY ADJ_QTY CNT_QTY
20091124130834762244 015-279A 11/24/2009 P000045557 002 POR LIN user12 10 0 0
20091124131110241744 015-279A 11/24/2009 P000045557 002 POR ADJ user12 0 10 0
20091124131850641554 015-279A 11/24/2009 P000045557 002 POR ADJ user12 0 -20 0
20091124131916249134 015-279A 11/24/2009 P000045557 002 POR LIN user12 10 0 0
20091124135601215291 015-279A 11/24/2009 P000045557 002 POR ADJ user12 0 10 0
20091124140318980761 015-279A 11/24/2009 MTL INS user13 10 0 0
20091124142344235001 015-279A 11/24/2009 P000045557 002 POR ADJ user12 0 -10 0
20080715093021657035 N174X53A 7/15/2008 P000012345 001 POR LIN user5 31 0 0
20080716100406218646 N174X53A 7/16/2008 P000012345 001 MTL INS user2 31 0 0
20080716100442488094 N174X53A 7/16/2008 MTL INS user2 0 0 31
20080717135714597592 N174X53A 7/17/2008 MTL INS user2 31 0 0
20080819111303812239 N174X53A 8/19/2008 P000012345 002 POR LIN user5 31 0 0
20100401145927954563 N174X53A 4/1/2010 MTL INS user1 0 0 31
20100512095750285578 N174X53A 5/12/2010 P000012345 002 POR LIN user6 30 0 0
20100614130143734676 N174X53A 6/14/2010 P000012345 002 MTL INS user1 0 0 30
20100412101824624708 N174X53A 4/12/2010 P000012345 004 POR LIN user4 31 0 0
20100422150057876187 N174X53A 4/22/2010 P000012345 004 MTL INS user2 0 0 31
20100512095303832580 N174X53A 5/12/2010 P000012345 004 POR LIN user6 31 0 0
20100712133833381506 N174X53A 7/12/2010 P000012345 004 MTL INS user1 31 0 0
20100917085814607499 N174X53A 9/17/2010 MTL INS user1 30 0 0
20100701123833300000 test 7/1/2010 P000011111 001 POR LIN user12 5 0 0
20100705964833300000 test 7/5/2010 P000011111 001 MTL INS user2 5 0 0
20100702123456700000 test 7/2/2010 P000011111 002 POR LIN user6 5 0 0
20100704987654300000 test 7/4/2010 MTL INS user1 5 0 0This is the logic I use to sort the data manually:
If the record has ord_nbr/ln_nbr (they aren't NULL), then sort by part_nbr, ord_nbr, ln_nbr, and time_stamp, in that order.
And here's the complicated part...
If the record does not have ord_nbr/ln_nbr (they are NULL, and this can only happen on MTL-INS transactions), then it needs to be placed into the sort I just described via this logic:
It must be placed with the other records with the same part_nbr, after the closest previous POR LIN transaction by time_stamp (for that part_nbr), but before the next chronological MTL-INS transaction by time_stamp (for that part_nbr), and between those 2 constraints, fit in by time_stamp.Sorry, I don't see how you get those results from that data.
In normal string stort order '015-279A' comes before 'N174X53A', which comes before 'test'. How do you get the order you want?
Then within each part_nbr, your explanation seems to say you want the rows with NULL ord_nbr to come right after the preceding POR-LIN row (where "preceding" means according to time_stamp), which would result in this order:
TIME_STAMP PART_NBR ACT_DATE ORD_NBR LN_ COD
20091124130834762244 015-279A 11/24/2009 P000045557 002 POR
20091124131110241744 015-279A 11/24/2009 P000045557 002 POR
20091124131850641554 015-279A 11/24/2009 P000045557 002 POR
20091124131916249134 015-279A 11/24/2009 P000045557 002 POR
20091124140318980761 015-279A 11/24/2009 MTL
20091124135601215291 015-279A 11/24/2009 P000045557 002 POR
20091124142344235001 015-279A 11/24/2009 P000045557 002 POR
20080715093021657035 N174X53A 7/15/2008 P000012345 001 POR
20080716100442488094 N174X53A 7/16/2008 MTL
20080717135714597592 N174X53A 7/17/2008 MTL
20080716100406218646 N174X53A 7/16/2008 P000012345 001 MTL
20080819111303812239 N174X53A 8/19/2008 P000012345 002 POR
20100401145927954563 N174X53A 4/1/2010 MTL
20100504083427334677 N174X53A 5/4/2010 P000012345 002 POR
20100512095750285578 N174X53A 5/12/2010 P000012345 002 POR
20100917085814607499 N174X53A 9/17/2010 MTL
20100614130143734676 N174X53A 6/14/2010 P000012345 002 MTL
20100412101824624708 N174X53A 4/12/2010 P000012345 004 POR
20100422150057876187 N174X53A 4/22/2010 P000012345 004 MTL
20100504081750020711 N174X53A 5/4/2010 P000012345 004 POR
20100512095303832580 N174X53A 5/12/2010 P000012345 004 POR
20100712133833381506 N174X53A 7/12/2010 P000012345 004 MTL
20100701123833300000 test 7/1/2010 P000011111 001 POR
20100705964833300000 test 7/5/2010 P000011111 001 MTL
20100702123456700000 test 7/2/2010 P000011111 002 POR
20100704987654300000 test 7/4/2010 MTLCan you explain the sorting rules agian, using different words.
Explain in particular why the rows that are in the wrong place above should be changed.
The code I used to get the results above was:
WITH got_r_num AS
SELECT time_stamp
, part_nbr
, act_date
, ord_nbr
, ln_nbr
, code_pre
, code_sfx
, ROW_NUMBER () OVER ( PARTITION BY part_nbr
ORDER BY ord_nbr
, ln_nbr
, time_stamp
) AS r_num
FROM inventory_history
SELECT time_stamp
, part_nbr
, act_date
, ord_nbr
, ln_nbr
, code_pre
FROM got_r_num
ORDER BY part_nbr
, CASE
WHEN ord_nbr IS NOT NULL
THEN r_num
ELSE LAST_VALUE ( CASE
WHEN code_pre = 'POR'
AND code_sfx = 'LIN'
THEN r_num
END
IGNORE NULLS
) OVER ( PARTITION BY part_nbr
ORDER BY time_stamp
END
, r_num
;It's a good thing that you're using Oracle 10; earlier versions didn't have the IGNORE NULLS feature in LAST_VALUE. That can be worked around, but it's complicated, and this is complicated enough already.
By the way, if you want to experiment with this kind of ORDER BY expression, I suggest you iput it in the SELECT clause, like this:
WITH got_r_num AS
SELECT time_stamp
, part_nbr
, act_date
, ord_nbr
, ln_nbr
, code_pre
, code_sfx
, ROW_NUMBER () OVER ( PARTITION BY part_nbr
ORDER BY ord_nbr
, ln_nbr
, time_stamp
) AS r_num
FROM inventory_history
SELECT time_stamp
, part_nbr
, act_date
, ord_nbr
, ln_nbr
, code_pre
, code_sfx
, CASE
WHEN ord_nbr IS NOT NULL
THEN r_num
ELSE LAST_VALUE ( CASE
WHEN code_pre = 'POR'
AND code_sfx = 'LIN'
THEN r_num
END
IGNORE NULLS
) OVER ( PARTITION BY part_nbr
ORDER BY time_stamp
END
as O_NUM
, r_num
FROM got_r_num
ORDER BY part_nbr
, o_num
, r_num
;Edited by: Frank Kulash on Sep 23, 2010 5:39 PM
Sorry, I left off some columns of output. I'll fix it when I have time, which is not now. -
Sys_context and Order by clause
Hi All,
Im using Oracle 11g R2.
Is it somehow possible to use Context variable (to store column name) in ORDER BY clause? My ultimate aim is to construct Order by at runtime without using concatenation.
e.g. i created context as my_ctx for some pkg and its variable as v_ctx having value set as "TABLE_NAME"
so is it possible to use it as
select * from all_tables
order by sys_context('my_ctx', 'v_ctx');
if no, is there any work around??
Thanks,
VivekHi, Vivek,
Vivek wrote:
Hi All,
Im using Oracle 11g R2.
Is it somehow possible to use Context variable (to store column name) in ORDER BY clause? My ultimate aim is to construct Order by at runtime without using concatenation.Sure, you can use SYS_CONTEXT in an ORDER BY clause, but it's unclear what you're trying to do, so I don't know if SYS_CONTEXT will help you.
e.g. i created context as my_ctx for some pkg and its variable as v_ctx having value set as "TABLE_NAME"
so is it possible to use it as
select * from all_tables
order by sys_context('my_ctx', 'v_ctx');
if no, is there any work around??Post a complete test script hat people can run to re-create the problem and test their ideas. Show the output you want from the parameters an data you give.
Think about using a CASE expression in the ORDER BY clause. You can use SYS_CONTEXT in the CASE expression if you want to. -
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?
WJHHi,
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; -
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 -
HAVING clause, or ORDER BY clause ....ERROR
hi i have following query which throws error plz find the solution for the query to go safe..
Select C.src_cd,
D.src_nm,
count(*) supp_clm_count,
(CASE WHEN A.clm_ttladjamt > 0 then A.clm_ttladjamt else A.clm_ttlreqamt END) as amount
From IWOWNER.WC_clm A,
IWOWNER.WC_clm_srvc B,
SHOWNER.WC_SRC C,
SHOWNER.WC_SRC_lang D
where A.clm_id = B.clm_id
and A.rcv_loc_id = C.src_id
and C.src_id = D.src_id
and TRANSLATE(A.clm_rqst_type_cd) = 'SUPPLIERCLAIM'
and TRANSLATE(A.clm_typ_cd) in ('WARRANTY','PRE-DELIVERY')
and DATE(A.clm_create_dt) between '01/01/2000' and '01/01/2010'
Group by C.src_cd, D.src_nm
ERROR:
SQLSTATE: 42803, SQLERRMC: CLM_TTLADJAMT
Message: An expression starting with "CLM_TTLADJAMT" specified in a SELECT clause, HAVING clause, or ORDER BY clause is not specified in the GROUP BY clause or it is in a SELECT clause, HAVING clause, or ORDER BY clause with a column function and no GROUP BY clause is specifiedHi,
With analytic function
/* Formatted on 2009/07/22 10:47 (Formatter Plus v4.8.8) */
SELECT DISTINCT c.src_cd, d.src_nm,
COUNT (1) OVER (PARTITION BY c.src_cd, d.src_nm)
supp_clm_count,
SUM (CASE
WHEN a.clm_ttladjamt > 0
THEN a.clm_ttladjamt
ELSE a.clm_ttlreqamt
END
) OVER (PARTITION BY c.src_cd, d.src_nm) AS amount
FROM iwowner.wc_clm a,
iwowner.wc_clm_srvc b,
showner.wc_src c,
showner.wc_src_lang d
WHERE a.clm_id = b.clm_id
AND a.rcv_loc_id = c.src_id
AND c.src_id = d.src_id
AND a.clm_rqst_type_cd = 'SUPPLIERCLAIM'
AND a.clm_typ_cd IN ('WARRANTY', 'PRE-DELIVERY')
AND a.clm_create_dt BETWEEN TO_DATE ('01/01/2000', 'dd/mm/yyyy')
AND TO_DATE ('01/01/2010', 'dd/mm/yyyy') -
ViewObjects Order by clause with DECODE
Hello!
I am using Jdeveloper 11g, version 11.1.1.2.0.
The problem I'm having is this.
If I use a DECODE statement in view objects ORDER BY clause, I get an error: "java.util.regex.PatternSyntaxException: Unmatched closing ')' near index 2".
Let me give an example. I'll be using EmployeesView view object, which is using Employees entity from HR schema.
This is a part of view objects XML.
<ViewObject
xmlns="http://xmlns.oracle.com/bc4j"
Name="EmployeesView"
Version="11.1.1.55.36"
SelectList="Employees.EMPLOYEE_ID,
Employees.FIRST_NAME,
Employees.LAST_NAME,
Employees.EMAIL,
Employees.PHONE_NUMBER,
Employees.HIRE_DATE,
Employees.JOB_ID,
Employees.SALARY,
Employees.COMMISSION_PCT,
Employees.MANAGER_ID,
Employees.DEPARTMENT_ID"
FromList="EMPLOYEES Employees"
BindingStyle="OracleName"
CustomQuery="false"
PageIterMode="Full"
UseGlueCode="false"
OrderBy="Employees.MANAGER_ID">
As you can see in this case, the Order by clause is very simple. This works like a charm.
But, if put something like this "DECODE(Employees.MANAGER_ID, NULL, 1, 2)" in the Order by, I get an internal parsing error.
I replicated this error on my home machine as well as on my work machine. I'm using the same version of Jdeveloper on both.
Has anyone else stumbled upon this problem and solved it?
Any thoughts would be greatly appreciated :)
KristjanThe second example works, but the first one doesn't, unfortunately :/
Also, the example I gave is unfortunately just that, a proof-of-concept example that there is a problem with DECODE if it is written inside the Order by clause.
My real DECODE use case is a bit different. Like this: "DECODE(attribute, 'N', 1, 2) ASC".
Since posting my original question, I did some research-by-example work and I discovered that this is not just a problem of DECODE, but more like a problem of brackets and commas.
No database function that uses more than one parameter can be used in Order by clause.
The reason is, if a function with more than one parameter is used, commas inside brackets have to be used. Something along the lines of: "database_function(param1, param2, ...)".
The parser seems to have a problem with this kind of expressions.
Is there a work around?
Kristjan
p.s.: Thank you for your quick response. -
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 advanceuser10636796 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.
Maybe you are looking for
-
Move contacts/calendars from Google to ICloud?
Up to now I synched my calendars and contacts with google via an exchange account. How can I move all information to iCloud? ICloud only synchs with own calendars on the iPhone and own contact group, all of them empty. Thanks for your help.
-
Online PDF exporting issue - Hyperlinks & Transparencies
We just started putting our printed magazine (approx. 65 pages) online in digital format with hyperlinks. The problem we are having is with transparencies and effects from all the graphic elements. With so many problems, our digital mag vendor said t
-
Oracle Express on Ubuntu 7.10 not starting up..
I install the .deb package (as root) and all seems well.. it says its starting up.. but when I try to connect to the web interface on port 8080 or the listener on 1521.. nothing seems to be running.. What logs can I check out to see what the scoop is
-
What is credit worth? I have one credit and want to know how to use it
I have one credit to use . What is one credit worth? How do i use it?
-
Xsql setup problem. 'Cannot acquire a database connection to process page.'
This is probably a really basic problem, but I am getting the following error when I try to use the xsql demos. Oracle XDK Java 9.2.0.2.0 Production XSQL-007: Cannot acquire a database connection to process page. Io exception: Connection refused(DESC