Help in order by clause
Hi,
i have the following query..
SELECT 'EXERCISE_TYPE' AS entity_name,
TO_CHAR(et.EXR_TYP_C) AS entity_code, et.EXR_TYP_T AS entity_description
FROM EXERCISE_TYPE et, COMPANY_GROUP_EXERCISE_TYPE cget
WHERE et.EXR_TYP_C = cget.EXR_TYP_C
AND cget.ORG_GRP_I = 2
UNION
select entity_name,entity_code,entity_description from(
SELECT 'TICKER' AS entity_name,
'-999' AS entity_code,'ALL' AS entity_description
FROM dual
union
SELECT 'TICKER' AS entity_name,
to_char(org_grp_i) AS entity_code,org_grp_x AS entity_description
FROM company_group order by entity_code,entity_description) order by entity_name
and the output is
EXERCISE_TYPE 0 Cash Exercise
EXERCISE_TYPE 1 Stock Swap
EXERCISE_TYPE 2 Same-Day Sale
EXERCISE_TYPE 3 Restricted Lapse
EXERCISE_TYPE 4 SAR
TICKER -999 ALL
TICKER 1 KFT_MO
TICKER 10 Cardinal H
TICKER 11 MAR1FAS
TICKER 12 MIKEFAS
TICKER 13 J 8/7
TICKER 14 DADE
TICKER 15 jsdu1
TICKER 16 vishfas
TICKER 2 ICOS
TICKER 2000 KFT
TICKER 2002 FRE_FASB
TICKER 2003 FRE
TICKER 2005 jhfuser
TICKER 2006 FASNT
I want the output to be
EXERCISE_TYPE 0 Cash Exercise
EXERCISE_TYPE 1 Stock Swap
EXERCISE_TYPE 2 Same-Day Sale
EXERCISE_TYPE 3 Restricted Lapse
EXERCISE_TYPE 4 SAR
TICKER -999 ALL
TICKER 3004 ABC
TICKER 3003 ABC
TICKER 3041 ACN
TICKER -999 ALL
TICKER 3007 BRCM
TICKER 10 Cardinal H
TICKER 14 DADE
TICKER 3045 DADE2
TICKER 3065 DELL
TICKER 33 FASDADE1
TICKER 2006 FASNT
TICKER 3047 FNM
TICKER 567 FRE
TICKER 2003 FRE
TICKER 2002 FRE_FASB
TICKER 2012 FRE_FASB
TICKER 2 ICOS
TICKER 3025 IR
TICKER 13 J 8/7
TICKER 555 J's CHK
is it possible..if so please tell me how...
thanks in advance
Something like this ?
SQL> select 'ENTITY_NAME' as col1, rownum as col2, dname as col3 from dept
2 union all
3 select 'TICKET', rownum, ename from emp
4 /
COL1 COL2 COL3
ENTITY_NAME 1 ACCOUNTING
ENTITY_NAME 2 RESEARCH
ENTITY_NAME 3 SALES
ENTITY_NAME 4 OPERATIONS
TICKET 1 SMITH
TICKET 2 ALLEN
TICKET 3 WARD
TICKET 4 JONES
TICKET 5 MARTIN
TICKET 6 BLAKE
TICKET 7 CLARK
TICKET 8 SCOTT
TICKET 9 KING
TICKET 10 TURNER
TICKET 11 ADAMS
TICKET 12 JAMES
TICKET 13 FORD
TICKET 14 MILLER
18 rows selected.
SQL> select * from (
2 select 'ENTITY_NAME' as col1, rownum as col2, dname as col3 from dept
3 union all
4 select 'TICKET', rownum, ename from emp
5 )
6 order by col1, decode(col1, 'ENTITY_NAME', col2, null),
7 decode(col1,'ENTITY_NAME', null, col3)
8 /
COL1 COL2 COL3
ENTITY_NAME 1 ACCOUNTING
ENTITY_NAME 2 RESEARCH
ENTITY_NAME 3 SALES
ENTITY_NAME 4 OPERATIONS
TICKET 11 ADAMS
TICKET 2 ALLEN
TICKET 6 BLAKE
TICKET 7 CLARK
TICKET 13 FORD
TICKET 12 JAMES
TICKET 4 JONES
TICKET 9 KING
TICKET 5 MARTIN
TICKET 14 MILLER
TICKET 8 SCOTT
TICKET 1 SMITH
TICKET 10 TURNER
TICKET 3 WARD
18 rows selected.Rgds.
Similar Messages
-
[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 -
Need help with ORDER BY clause
Hey,
I have a table:
Name: Year:
Eagle 2000
Tiger 2001
Eagle 2002
Lion 2006
Lion 1999
Fox 1991
Lion 1995
I need a query which will return in such order:
Name: Year: Position:
Eagle 2000 1
Eagle 2002 2
Fox 1991 1
Lion 1995 1
Lion 1999 2
Lion 2006 3
Tiger 2001 1
So, of course to get Name and Year in this order is quite easy:
select Name, Year from Animals order by Name, Year;
but how about Position, is there a way to count it with SQL?
any help is welcome,
SilvestrasSQL> with rt as
2 (select 'Eagle' nm, 2000 yr from dual union all
3 select 'Tiger', 2001 from dual union all
4 select 'eagle', 2002 from dual union all
5 select 'Lion', 2006 from dual union all
6 select 'Lion', 1999 from dual union all
7 select 'Fox', 1991 from dual union all
8 select 'Lion', 1995 from dual)
9 select nm,yr,row_number() over(partition by (nm) order by nm,yr) position from rt;
NM YR POSITION
Eagle 2000 1
Fox 1991 1
Lion 1995 1
Lion 1999 2
Lion 2006 3
Tiger 2001 1
eagle 2002 1
7 rows selected.
SQL> with rt as
2 (select 'Eagle' nm, 2000 yr from dual union all
3 select 'Tiger', 2001 from dual union all
4 select 'eagle', 2002 from dual union all
5 select 'Lion', 2006 from dual union all
6 select 'Lion', 1999 from dual union all
7 select 'Fox', 1991 from dual union all
8 select 'Lion', 1995 from dual)
9 select nm,yr,row_number() over(partition by lower(nm) order by nm,yr) position from rt;
NM YR POSITION
Eagle 2000 1
eagle 2002 2
Fox 1991 1
Lion 1995 1
Lion 1999 2
Lion 2006 3
Tiger 2001 1
7 rows selected.
SQL> -
Hi ,
i have a below table with data , when i run my select with order by option its gives me
1 Card
as a first row but i need this
12 atm
as a first row ,how this is possible .
create table test
(ID number ,
val varchar2(20)
insert into test values (1,'Card')
insert into test values (2,'Card')
insert into test values (3,'Card')
insert into test values (4,'Cash')
insert into test values (5,'Cash')
insert into test values (6,'Cash')
insert into test values (7,'Checque')
insert into test values (8,'Checque')
insert into test values (9,'Checque')
insert into test values (10,'atm')
insert into test values (11,'atm')
insert into test values (12,'atm')
select * from test
order by val ascYOu can also use Nlssort like
SQL> select * from v$version;
BANNER
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Prod
PL/SQL Release 10.2.0.1.0 - Production
CORE 10.2.0.1.0 Production
TNS for 32-bit Windows: Version 10.2.0.1.0 - Production
NLSRTL Version 10.2.0.1.0 - Production
SQL> select *
2 from test
3 order by nlssort(val, 'NLS_SORT = WEST_EUROPEAN_AI') asc, id desc
4 /
ID VAL
12 atm
11 atm
10 atm
3 Card
2 Card
1 Card
6 Cash
5 Cash
4 Cash
9 Checque
8 Checque
ID VAL
7 Checque
12 rows selected. -
Query hangs when given order by clause...help needed urgent
Hi ,
I have a table of size 4.1 GB.
QUERY IS :select * from rm_bil_bill order by bill_id desc;
the column Bill_id is primary key.
i have 6GB of temp tablespace.
the normal select works fine but then when it is suffixed with order by clause it is hanging for ever ...
Please help on this...
Thanks,
AshleeHI ,
Also like to add some information.
The table has been analysed on july 19th,
the explain plan for the above statement is :
SQL> SELECT * FROM TABLE( dbms_xplan.display );
PLAN_TABLE_OUTPUT
Plan hash value: 178181731
| Id | Operation | Name | Rows | Bytes |TempSpc| Cost (%CPU)|
Time |
PLAN_TABLE_OUTPUT
| 0 | SELECT STATEMENT | | 24M| 3790M| | 1006K (2)|
03:21:17 |
| 1 | SORT ORDER BY | | 24M| 3790M| 10G| 1006K (2)|
03:21:17 |
| 2 | TABLE ACCESS FULL| RM_BIL_BILL | 24M| 3790M| | 123K (5)|
00:24:41 |
PLAN_TABLE_OUTPUT
9 rows selected.
SQL> SQL> SELECT * FROM TABLE(dbms_xplan.display);
PLAN_TABLE_OUTPUT
Plan hash value: 178181731
| Id | Operation | Name | Rows | Bytes |TempSpc| Cost (%CPU)|
Time |
PLAN_TABLE_OUTPUT
| 0 | SELECT STATEMENT | | 24M| 3790M| | 1006K (2)|
03:21:17 |
| 1 | SORT ORDER BY | | 24M| 3790M| 10G| 1006K (2)|
03:21:17 |
| 2 | TABLE ACCESS FULL| RM_BIL_BILL | 24M| 3790M| | 123K (5)|
00:24:41 |
PLAN_TABLE_OUTPUT
9 rows selected.
THE TABLE DESCRIPTION:
SQL> desc rm_p01.RM_BIL_BILL
Name Null? Type
BILL_ID NOT NULL NUMBER(14)
STUDENT_ID NOT NULL NUMBER(14)
STUDENT_UIN NOT NULL VARCHAR2(20)
BILL_MONTH NOT NULL DATE
BILL_TYPE NOT NULL VARCHAR2(10)
SCHOOL_CODE NOT NULL VARCHAR2(10)
LEVEL_GRADE NOT NULL VARCHAR2(10)
CLASS_NAME VARCHAR2(10)
PUPIL_PROFILE_ID NOT NULL NUMBER(14)
BAL_BROUGHT_FORWARD NOT NULL NUMBER(10,2)
BILL_TOTAL_PAYMENT NOT NULL NUMBER(10,2)
BILL_OUTSTANDING_BAL NOT NULL NUMBER(10,2)
BILL_NET_CHARGES NOT NULL NUMBER(10,2)
BILL_TOTAL_PAYABLE NOT NULL NUMBER(10,2)
GENERATION_DATE NOT NULL DATE
GIRO_SO_IND CHAR(1)
GIRO_DEDUCTION_IND CHAR(1)
GIRO_DEDUCTION_DATE DATE
PRINT_DATA_EXTRACTION_IND CHAR(1)
PRINT_DATA_EXTRACTION_DATE DATE
REVIEW_IND CHAR(1)
REVIEW_DATE DATE
REVERSAL_MONTH DATE
VERSION_NO NOT NULL NUMBER(6)
CREATED_BY VARCHAR2(20)
CREATION_DATE DATE
LAST_UPDATED_BY VARCHAR2(20)
LAST_UPDATED_DATE DATE
BANK_ACCOUNT_NUMBER VARCHAR2(12)
BANK_ACRONYM VARCHAR2(30)
the table DDL :
CREATE TABLE "RM_P01"."RM_BIL_BILL"
( "BILL_ID" NUMBER(14,0) NOT NULL ENABLE,
"STUDENT_ID" NUMBER(14,0) NOT NULL ENABLE,
"STUDENT_UIN" VARCHAR2(20) NOT NULL ENABLE,
"BILL_MONTH" DATE NOT NULL ENABLE,
"BILL_TYPE" VARCHAR2(10) NOT NULL ENABLE,
"SCHOOL_CODE" VARCHAR2(10) NOT NULL ENABLE,
"LEVEL_GRADE" VARCHAR2(10) NOT NULL ENABLE,
"CLASS_NAME" VARCHAR2(10),
"PUPIL_PROFILE_ID" NUMBER(14,0) NOT NULL ENABLE,
"BAL_BROUGHT_FORWARD" NUMBER(10,2) NOT NULL ENABLE,
"BILL_TOTAL_PAYMENT" NUMBER(10,2) NOT NULL ENABLE,
"BILL_OUTSTANDING_BAL" NUMBER(10,2) NOT NULL ENABLE,
"BILL_NET_CHARGES" NUMBER(10,2) NOT NULL ENABLE,
"BILL_TOTAL_PAYABLE" NUMBER(10,2) NOT NULL ENABLE,
"GENERATION_DATE" DATE NOT NULL ENABLE,
"GIRO_SO_IND" CHAR(1),
"GIRO_DEDUCTION_IND" CHAR(1),
"GIRO_DEDUCTION_DATE" DATE,
"PRINT_DATA_EXTRACTION_IND" CHAR(1),
"PRINT_DATA_EXTRACTION_DATE" DATE,
"REVIEW_IND" CHAR(1),
"REVIEW_DATE" DATE,
"REVERSAL_MONTH" DATE,
"VERSION_NO" NUMBER(6,0) DEFAULT 0 NOT NULL ENABLE,
"CREATED_BY" VARCHAR2(20),
"CREATION_DATE" DATE DEFAULT SYSDATE,
"LAST_UPDATED_BY" VARCHAR2(20),
"LAST_UPDATED_DATE" DATE DEFAULT SYSDATE,
"BANK_ACCOUNT_NUMBER" VARCHAR2(12),
"BANK_ACRONYM" VARCHAR2(30),
PRIMARY KEY ("BILL_ID")
USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 NOLOGGING COMPUTE STATISTICS
STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT)
TABLESPACE "DATAMEDIUM_RM_P" ENABLE
) PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS NOLOGGING
STORAGE(INITIAL 4294967296 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT)
TABLESPACE "DATAMEDIUM_RM_P" -
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; -
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. -
In Report,order by clause is not working
Hi All,
For the report i prepared the query in this one am using the order by clause,i run the query in the sql plus working fine am getting the data based on the order by clause.when i use the same query for the report am getting the data but not in the order(i.e order by clause in not working).
Please help me how to do this one.
Thanks in Advance.
Regards,
Leelakrishna.GHi Dora,
Data is coming in the proper sequence,but in the first group data is not coming(In first group data first line only displaying,remaining data is not comming).
My req is:
The report is executing based on the creation date(i.e.,from creation date to today).
In this so many Purchase requisitions will be there for this one different approval in different times will be there.
For Ex:
we will take 2 PR number only(for the first one 3 approvals,second one 4 levels of approval).
1. one supplier 1person raised by 1PR submit
No Action
Approve
2. 2Supplier 2personraised by 2PR Submit
NO Action
Forword
Approve
with ur suggestion am trying data is gettin fine but first group first line is coming remaining data is not coing.
EX:
1. one supplier 1person raised by 1PR submit
No Action
Approve
2PR Submit
NO Action
Forword
Approve
Note:"2. 2Supplier 2personraised by " this kind of lines data is missing.
Can you pls check and suggest me how i can do this.
Thanks in Advance.
Regards,
Leelakrishna.G -
Order By clause in Receiver JDBC adapter
Hello All,
Is it possible to construct a select statement that uses an Order by clause in JDBC receiver adapter?
ex : I would like the JDBC adapter to from a query as follows:
<b>Select * from EMP where sal = 10000 order by empid</b>
We are able to construct the select statement without order by clause but not with it.
Can any one suggest me how to do it. This is bit urgent for us.
Thanks
Abinashhi,
did you try with
SQL_DML
http://help.sap.com/saphelp_nw04/helpdata/en/2e/96fd3f2d14e869e10000000a155106/content.htm
Regards,
michal -
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 -
Hello,
Sorry I am posting it for the second time
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.
Or Is there any other way that i can set the
option once for this and need not worry abt it afterwards?
Can Any help me out in Solving this?
Thanks
Devi
nullFor fun and out of curiosity, I tried :
SQL> select /*+ leading(t) use_nl(e t) */
2 e.deptno
3 , e.empno
4 from scott.emp e
5 , table(sys.odcinumberlist(30,10,20)) t
6 where e.deptno = t.column_value
7 ;
DEPTNO EMPNO
30 7499
30 7521
30 7654
30 7698
30 7844
30 7900
10 7782
10 7839
10 7934
20 7369
20 7566
20 7902
12 rows selected
It "seems" to work but of course we can't expect a consistent behaviour, or can we? ;) -
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
-
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 -
Passing parameters in order by clause of cursor
Hi friends,
I am facing a strange problem
I have a cursor with a order by clause.
OPEN cursor_nomrpt FOR
SELECT DISTINCT
CAST(plano.desc3 AS VARCHAR2(50)) category
,plano.name plan_name
,nvl(CAST(plano.desc6 AS VARCHAR2(50)),'not applicable') pcrr
,nvl(trunc(plano.value18,2),0) nominal_slm
,nvl(trunc(plano.value19,2),0) blm
,plano.dbdateeffectivefrom date_live
plano.dbkey pog_id
,case when plano.value47 < 0 THEN 0 when plano.value47 IS NULL THEN 0 ELSE plano.value47 END AS parent_id
FROM
dplapro1.ix_spc_planogram plano
INNER JOIN dplapro1.ix_spc_performance perf ON plano.dbkey = perf.dbparentplanogramkey
INNER JOIN dplapro1.ix_spc_product product ON perf.dbparentproductkey = product.dbkey
INNER JOIN dplapro1.ix_spc_product_key prodkey ON product.dbkey2 = prodkey.dbkey
AND prodkey.keylevel = 2
WHERE
plano.value50 = 0
AND plano.dbkey4 = p_subcatkey
ORDER BY ltrim(rtrim(p_orderby)) ASC;
p_orderby is the parameter being passed. But the output is not sorted. But when I run the cursor by hardcoding the parameter value it works fine...
Need your help on thisHi,
When you use a local variable in a cursor, it's as if you had hard-coded a literal in its place, so you can't use a variable for a column name.
If you know all the possible values of p_orderby, you can do something like this:
ORDER BY TRIM ( CASE p_orderby
WHEN 'DESC' THEN plano.desc
WHEN 'VALUE' THEN plano.value
END
) ASC;If you don't know all the possible values, you could use dynamic SQL.
By the way,
TRIM (x)returns the same results as
LTRIM ( RTRIM (x))
Maybe you are looking for
-
To know the amount of free space in a tablespace
hi, I'm learning oracle 9i release 9.2.0.1.0 .I'd created a tablespace 'TOOLS' for being used as the default tablespace for Statspack. I wish to know the amount of free space available in this tablespace. For this purpose I ran the following query bu
-
Sign into icloud box won't go away
As soon as I power on my ipad mini, the sign into iCloud dialogue box pops up. I type in the password, but it pops up with the box again as if I didn't type anything in. I've tried holding the home and power buttons simultaneously to restart it, t
-
Reg: Billing template experience
Hi, Any body let me know what could be the term Experinece on "Billing template needed". I have seen this term in one of the Requirements. Thanks Kris
-
This error was found in Excel 2010 32 bit but may be present in other versions. The error happens when writing specific dates to an Excel cell. The VBA code Dim datDate as Date datDate = CVDate(41586.9999999995) ActiveWorkbook.Sheets("Sheet1").Cells(
-
hi friends, i am using eclipse. i have recieved a fatal error while debugging with eclipse. i never seen this error message before. could anyone explain me this.i am pasting the error below. FATAL ERROR in native method: JDWP "util.c" (Feb 22 2004),