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 :)
Kristjan
The 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.
Similar Messages
-
Decode in order by clause with desc
I want to user order by clause with decode + desc order.
somthing like
ORDER BY DECODE ('SALE', e.sale, 'SALE DESC' ????)
????-> How to use desc order with decode
Thanks in advanceI thought smart people in this OTN community will understand that I am trying to order by the thisdate column that is timestamp datatype:). My apologize for not being that specific.
The query I gave is a simple version of the stored procedure I am using. The point is I need to order by - depending on one of the parameters that is passed to the procedure. In simplest decode statements, its something like
order by decode(p_in_var,'ABC','thisdate asc','DEF','thisdate desc',thisdate asc)
Here p_in_var is varchar input parameter to the stored procedure.
Please let me know if there is any more information needed.
Thx! -
Writing order by clause with user parameter
I have a user parameter where the user is supposed to choose what column they want to sort by.
And they can choose between k.personnr and k.personnavn. I am trying to do this with a decode function.
Now this would work fine if there was no union between these select statements.
But with the union I get a ORA-01785, where they tell me to put in a number.
Here is the sql:
SELECT
k.personnr as PERSON ,
k.personnavn as NAVN,
r.belop as SUM,
concat(to_char(r.bilagdato,'MM'), to_char(r.bilagdato,'YYYY')) AS MND,
PERIODE(concat(to_char(r.bilagdato,'MM'), to_char(r.bilagdato,'YYYY'))) AS PERIODE
FROM
reskontro r,
klient k,
kontoplan p,
konto c
WHERE
r.distriktnr='21'
and r.distriktnr=c.kontoid
and r.distriktnr=k.distriktnr
and r.personnr=k.personnr
and r.kontonr=p.kontonr
and (((r.bilagdato between :p_fra_dato and :p_til_dato) and (:p_fra_dato <= c.overfort) and (:p_til_dato <= c.overfort))
or ((r.bilagdato between :p_fra_dato and c.overfort) and (:p_fra_dato <= c.overfort) and (:p_til_dato >= c.overfort)))
UNION ALL
SELECT
k.personnr as PERSON ,
k.personnavn as NAVN,
(t.belop/100) as SUM,
concat(to_char(t.bilagdato,'MM'), to_char(t.bilagdato,'YYYY')) AS MND,
PERIODE(concat(to_char(t.bilagdato,'MM'), to_char(t.bilagdato,'YYYY')),:P_HORISONTAL) AS PERIODE
FROM
transaksjon t,
klient k,
kontoplan p
WHERE
t.distriktnr='21'
and t.distriktnr=k.distriktnr
and t.personnr=k.personnr
and t.kontonr=p.kontonr
and t.bilagdato between :p_fra_dato and :p_til_dato
This is the order by clause I would like to use:
order by decode(:p_order_by,'Personnavn',k.personnavn,k.personnr)
I also get an error when I use:
order by decode(:p_order_by,'Personnavn',2,1)
Can anybody tell me how to work around this problem?Hi,
One way you can do to get around this problem could be :
select * from (
SELECT
k.personnr as PERSON ,
k.personnavn as NAVN,
r.belop as SUM,
concat(to_char(r.bilagdato,'MM'), to_char(r.bilagdato,'YYYY')) AS MND,
PERIODE(concat(to_char(r.bilagdato,'MM'), to_char(r.bilagdato,'YYYY'))) AS PERIODE
FROM
reskontro r,
klient k,
kontoplan p,
konto c
WHERE
r.distriktnr='21'
and r.distriktnr=c.kontoid
and r.distriktnr=k.distriktnr
and r.personnr=k.personnr
and r.kontonr=p.kontonr
and (((r.bilagdato between :p_fra_dato and :p_til_dato) and (:p_fra_dato <= c.overfort) and (:p_til_dato <= c.overfort))
or ((r.bilagdato between :p_fra_dato and c.overfort) and (:p_fra_dato <= c.overfort) and (:p_til_dato >= c.overfort)))
UNION ALL
SELECT
k.personnr as PERSON ,
k.personnavn as NAVN,
(t.belop/100) as SUM,
concat(to_char(t.bilagdato,'MM'), to_char(t.bilagdato,'YYYY')) AS MND,
PERIODE(concat(to_char(t.bilagdato,'MM'), to_char(t.bilagdato,'YYYY')),:P_HORISONTAL) AS PERIODE
FROM
transaksjon t,
klient k,
kontoplan p
WHERE
t.distriktnr='21'
and t.distriktnr=k.distriktnr
and t.personnr=k.personnr
and t.kontonr=p.kontonr
and t.bilagdato between :p_fra_dato and :p_til_dato)
order by decode(:p_order_by,'Personnavn', NAVN, PERSON)
/Uffe -
Hi,
I wanted to know if this is possible with order by clause and union. some how union messes up the ordering of the left part of the union statement.
assume the data is as below in table tab1
c1 c2
4 1
4 2
5 1
5 2
7 1
7 2
8 3
9 4
the expected output must be for all c1 < 7 ordering must be on c1 and for all c1 > 7 ordering must be on c2.
so the query i tried was
select c1,c2 from tab1 where c1 <7 order by c1
union
select c1,c2 from tab1 where c1 >=7 order by c2
it is giving the message 'Sql command not properly ended'.
Suggestions are welcomeSQL>
SQL> With t As
2 (
3 Select 4 c1, 1 c2 From dual Union All
4 Select 4 ,2 From dual Union All
5 Select 5 ,1 From dual Union All
6 Select 5 ,2 From dual Union All
7 Select 7 ,1 From dual Union All
8 Select 7 ,2 From dual Union All
9 Select 8 ,3 From dual Union All
10 Select 9 ,4 From dual
11 ), t1 As (Select c1,c2 From t Where c1 <7 Order By c1)
12 ,t2 As (select c1,c2 from t where c1 >=7 Order By c2)
13 Select * From t1 Union All
14 Select * From t2;
C1 C2
4 1
4 2
5 1
5 2
7 1
7 2
8 3
9 4
8 rows selected
SQL> /*-- Union All
2 select c1,c2 from t where c1 >=7
3 Order By c1,c2;
4 */
5
with union clause order by must be placed in the end,,
select c1 from dual union all
select c2 from dual
order by c1 -
Order By Clause with Empty Field values !
Hello,
In Oracle , the order by clause does not return the expected query result, if any of the field value in the order by clause has an empty string.
Oracle treats the empty string as null value and ORDER BY gives a result with the empty string field values listed at last.
For example :
test is a sample table containing "name" field.
Query: "select name from test order by name"
In SQL Server/Access
Result (1)
NAME
(blank)
(blank)
User1
User2
User3
In Oracle
Result (2)
NAME
User1
User2
User3
(blank)
(blank)
I know some of the Work arounds for this as listed below :
1) To use NVL in Order By Clause.
i.e., the modified query
"select name from test order by nvl(name,0)"
gives the result same as Result (1).
2) To have single blank space in the field value if it is empty.
I dont want to use either of these two options b'se it would lead to a mass change in my existing code.
Is there any way i can do a
collation order settings in the Oracle databases to get the results as in MS SQL/Access.
Can Any help me out in Solving this?
Thanks
Devi Shankar
nullBharath,
I am moving this question to the SQL forum.
Regards,
Geoff -
Order by clause using decode function
Hi everybody,
i need below order in my report.
Connecticut
greenwich
stamford
bridgeport
New York
NYC
wrestcher
byram
Georgia
atlanta
athens
oconny
first i need above order in my view out put.
so in order by clause i used first decode function for State ordering
and in second decode function for city ordering.
i do not need order by ascending or descending.
so pls anybody can help me.
any help is greatly appreciated.
thanks.add asc after the decode. default is desc
-
Conditional order by clause with multiple columns
I would like to know whether it is possible to include multiple columns in a conditional order by clause.
Ex: I have written the following PL / SQL :
CREATE PROCEDURE GetProducts
@OrderBy VARCHAR(50),
@Input2 VARCHAR(30)
AS
BEGIN
SET NOCOUNT ON
SELECT Id, ProductName, Description, Price, Quantity
FROM Products
WHERE ProductName LIKE @Input2
ORDER BY
CASE
WHEN @OrderBy = 'ProductNameAsc' THEN ProductName
END ASC,
CASE
WHEN @OrderBy = 'ProductNameDesc' THEN ProductName
END DESC
END
Now I want to include when the Orderby - "Productnameasc" then order by productname, price, stdate
else
orderby productname, crdate, category
Could this be done? I tried it, but couldn't get this to work.
Any help is greatly appreciated.Since matching ORDER BY columns have different data types (first column for CLERK is sal - number, first column for MANAGER deptno is number but first column for "else" is a string) we need to convert number to string but preserve number sort order. Since DEPTNO is NUMBER(2) we will use TO_CHAR with format 'S00'. Since SAL is NUMBER(7,2) we will use TO_CHAR with format model 'S00000D00'. For EMPNO we will use TO_CHAR with format model 'S0000'.
select deptno,
empno,
ename,
job,
sal
from emp
order by case job
when 'CLERK' then to_char(sal,'S00000D00')
when 'MANAGER' then to_char(deptno,'S00')
else ename
end,
case job
when 'CLERK' then to_char(deptno,'S00')
else job
end,
case job
when 'CLERK' then job
when 'MANAGER' then to_char(empno,'S0000')
end
DEPTNO EMPNO ENAME JOB SAL
20 7369 SMITH CLERK 800
30 7900 JAMES CLERK 950
20 7876 ADAMS CLERK 1100
10 7934 MILLER CLERK 1300
10 7782 CLARK MANAGER 2450
20 7566 JONES MANAGER 2975
30 7698 BLAKE MANAGER 2850
30 7499 ALLEN SALESMAN 1600
20 7902 FORD ANALYST 3000
10 7839 KING PRESIDENT 5000
30 7654 MARTIN SALESMAN 1250
DEPTNO EMPNO ENAME JOB SAL
20 7788 SCOTT ANALYST 3000
30 7844 TURNER SALESMAN 1500
30 7521 WARD SALESMAN 1250
14 rows selected.
SQL> SY. -
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? -
Order by clause with Named Query
hi
i have to give order by clause in Named Query
how we have to specify is can any body help
thanks
HarishAssuming an Entity called Handset:
select h from Handset h order by h.description -
Between clause with DECODE function.
Hi All,
I have one table which has object, date and quantity columns, Now based on date range I can find out the objects and quantity but the question is: I want a query which returns all the objects and should show quantity 0(zero or null) for those objects which don't falls under that date range. So I thought to use a decode function but it only uses the relational operation, I thought something like this but it's wrong,
select object,decode(created_date,BETWEEN '01/01/2008' TO '08/01/2008',quantity,0) from table;
Does anyone have any idea how can I make this work in single sql statement?
Thanks for your help.select object,decode(created_date,BETWEEN '01/01/2008' TO '08/01/2008',quantity,0) from table;
Does anyone have any idea how can I make this work in single sql statement?If you're still interested in using DECODE over Case when, please try this.
Check for employees hired between 6/17/1987 and 9/30/1987
SQL> select first_name||' '||last_name as emp_name, hire_date,
2 decode(sign((to_date('06/17/1987','MM/DD/YYYY')-1)-hire_date)
3 + sign((to_date('09/30/1987','MM/DD/YYYY')+1)-hire_date)
4 ,0,'Between','Not between') as IsBetween
5 from employees
6 where hire_date <to_date('01/01/1991','MM/DD/YYYY')
7 /
EMP_NAME HIRE_DATE ISBETWEEN
Steven King 17-JUN-87 Between
Neena Kochhar 21-SEP-89 Not between
Alexander Hunold 03-JAN-90 Not between
Jennifer Whalen 17-SEP-87 BetweenMessage was edited by:
Bobbydj -
Database Settings for order By Clause
Hi,
In oracle if we do a "order by" in SQL statement for field values whose values are empty, then they would be listed at the last if we dont specify any NVL() for it.
Is there any other option by which the funcionality of Order by clause with Empty field values can be changed.
Is there any settings that can be made in the existing database , so that the Order by clause does not list the empty field values at the bottom.
Thanx in advance.
Bharathyou can do wonders with decode and nvl
for instance:
order by decode(nullable_field, null, 1, 2)would make all your null fields come first, whereas
order by nvl(nullable_field, 'M')would make them appear in the middle of an alphabetical list.
Basically NULLs are not something. You have to give the ORDER BY something to replace the null.
null -
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. -
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') -
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. -
Is it possible to use a decode function in the order by clause, when you have two select statements with a union between? I seem to be getting an error-message telling me to type a number.
Can anybody help me?Tove,
Is this what you mean?
SQL> SELECT
2 *
3 FROM
4 (SELECT
5 empno no
6 , ename name
7 , job description
8 FROM emp
9 UNION
10 SELECT
11 deptno no
12 , dname name
13 , loc description
14 FROM dept
15 )
16 ORDER BY
17 DECODE(1
18 , 1,TO_CHAR(no)
19 , 2,name
20 , 3,description)
21 /
NO NAME DESCRIPTION
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
7369 SMITH CLERK
7499 ALLEN SALESMAN
7521 WARD SALESMAN
7566 JONES MANAGER
7654 MARTIN SALESMAN
7698 BLAKE MANAGER
7782 CLARK MANAGER
7788 SCOTT ANALYST
7839 KING PRESIDENT
7844 TURNER SALESMAN
7876 ADAMS CLERK
7900 JAMES CLERK
7902 FORD ANALYST
7934 MILLER CLERK
18 rows selected.Now, to order by the second column, I set the first parameter of the
DECODE to "2":
SQL> SELECT
2 *
3 FROM
4 (SELECT
5 empno no
6 , ename name
7 , job description
8 FROM emp
9 UNION
10 SELECT
11 deptno no
12 , dname name
13 , loc description
14 FROM dept
15 )
16 ORDER BY
17 DECODE(2
18 , 1,TO_CHAR(no)
19 , 2,name
20 , 3,description)
21 /
NO NAME DESCRIPTION
10 ACCOUNTING NEW YORK
7876 ADAMS CLERK
7499 ALLEN SALESMAN
7698 BLAKE MANAGER
7782 CLARK MANAGER
7902 FORD ANALYST
7900 JAMES CLERK
7566 JONES MANAGER
7839 KING PRESIDENT
7654 MARTIN SALESMAN
7934 MILLER CLERK
40 OPERATIONS BOSTON
20 RESEARCH DALLAS
30 SALES CHICAGO
7788 SCOTT ANALYST
7369 SMITH CLERK
7844 TURNER SALESMAN
7521 WARD SALESMAN
18 rows selected.
SQL> SELECT
2 *
3 FROM
4 (SELECT
5 empno no
6 , ename name
7 , job description
8 FROM emp
9 UNION
10 SELECT
11 deptno no
12 , dname name
13 , loc description
14 FROM dept
15 )
16 ORDER BY
17 DECODE(3
18 , 1,TO_CHAR(no)
19 , 2,name
20 , 3,description)
21 /
NO NAME DESCRIPTION
7788 SCOTT ANALYST
7902 FORD ANALYST
40 OPERATIONS BOSTON
30 SALES CHICAGO
7369 SMITH CLERK
7934 MILLER CLERK
7900 JAMES CLERK
7876 ADAMS CLERK
20 RESEARCH DALLAS
7566 JONES MANAGER
7698 BLAKE MANAGER
7782 CLARK MANAGER
10 ACCOUNTING NEW YORK
7839 KING PRESIDENT
7499 ALLEN SALESMAN
7844 TURNER SALESMAN
7654 MARTIN SALESMAN
7521 WARD SALESMAN
18 rows selected.I needed to put the TO_CHAR in the DECODE so all of the columns
by which I could potentially order are VARCHAR2's, else I was
getting
SQL> /
DECODE(1
ERROR at line 17:
ORA-01785: ORDER BY item must be the number of a SELECT-list expressionHTH
T.
Maybe you are looking for
-
Ipod remote app and apple tv sync vs. stream problem
Have been using ATV 1st gen streaming from a shared itunes library for some time. Now I want to use the ipod remote app and can make it work only if I sync content to the ATV. If I stream content to the ATV, the remote app says there is no content. H
-
Preserving styles when copying and pasting?
Howdy folks...still running CS3 on my system here...but just wanted to know what your take was... In the magazine I work for, we've previously been generating PDFs for press one at a time (ie: take a 4 page InDesign file and generate 4 1-page PDFs),
-
Apple logo constantly blinking, cannot open itunes or use phone at all
Please tell me how to fix this issue without losing my contacts, media, data, etc. I would prefer not to restore my phone to factory settings. I have a 3GS, not jailbroken, have had it for about a year and a half. I can't even turn the phone off or o
-
How to put multiple targets in ODI
Hi All, Can i know how to put multiple targets in Oracle Data Inegrator. Thanks in Advance!!
-
Run and Depoly error on portal
Hello, What does this error mean? com.sap.engine.services.webservices.jaxrpc.exceptions.InvalidResponseCodeException: Invalid Response Code: (401) Unauthorized. The requested URL was:"http://rrisep1p01:50100/AdobeDocumentServices/Config?style=do