Help me on this query [message #266205] |
Mon, 10 September 2007 03:44 |
vidya2005
Messages: 36 Registered: July 2005 Location: bangalore
|
Member |
|
|
Hi Experts,
I have a table like this.
Eno sal
N1 Low
N2 Medium
N3 High
the query should display like this.
High
Medim
Low
How to write the query for it. Please help me.
Thanks & Regards
Vidya
|
|
|
|
|
|
|
|
|
|
|
|
|
Re: Help me on this query [message #266483 is a reply to message #266205] |
Tue, 11 September 2007 00:25 |
Arju
Messages: 1554 Registered: June 2007 Location: Dhaka,Bangladesh. Mobile:...
|
Senior Member |
|
|
I am newbie in sql,pl/sql.
I tried but use of decode failed.It can't match 'low' as 'low'
SQL> select * from test;
ENO SAL
---------- ----------
1 low
2 medium
3 high
3 high
1 low
10 high
10 low
7 HIGH
8 rows selected.
SQL> select eno,decode(sal,'HIGH',1,'medium',2,'low',3, 0) test,sal from test;
ENO TEST SAL
---------- ---------- ----------
1 0 low
2 0 medium
3 0 high
3 0 high
1 0 low
10 0 high
10 0 low
7 0 HIGH
8 rows selected.
|
|
|
Re: Help me on this query [message #266484 is a reply to message #266479] |
Tue, 11 September 2007 00:27 |
|
Littlefoot
Messages: 21823 Registered: June 2005 Location: Croatia, Europe
|
Senior Member Account Moderator |
|
|
Where's a DECODE in your query, combined with an ORDER BY? I'm afraid you aren't capable of figuring that out (yet), so - here's how it may be done (using sample data set from your initial post):SQL> create table test (eno varchar2(2), sal varchar2(10));
Table created.
SQL> insert all
2 into test values ('N1', 'Low')
3 into test values ('N2', 'Medium')
4 into test values ('N3', 'High')
5 select * From dual;
3 rows created.
SQL> select * From test;
EN SAL
-- ----------
N1 Low
N2 Medium
N3 High My initial response:SQL> select sal
2 from test
3 order by eno desc;
SAL
----------
High
Medium
Low Frank's suggestion:SQL> select sal
2 from test
3 order by decode(sal, 'High', 1,
4 'Medium', 2,
5 'Low', 3
6 );
SAL
----------
High
Medium
Low
|
|
|
|
|
Re: Help me on this query [message #266489 is a reply to message #266205] |
Tue, 11 September 2007 00:36 |
Arju
Messages: 1554 Registered: June 2007 Location: Dhaka,Bangladesh. Mobile:...
|
Senior Member |
|
|
I tried both in character and varchar2 data type.First create table as char data type as 10 length and then converted to varchar2 data type. So it took and remain 10 as length.
SQL> select length(sal) len , sal from test;
LEN SAL
-------------------------------------------
10 low
10 medium
10 high
.
.
.
8 rows selected.
SQL> desc test;
Name Null? Type
----------------------------------------------------- --------
ENO NUMBER
SAL VARCHAR2(100)
Oh sorry char padded spaces to length 10 and later when I alter table column to varchar2 it remain so.
Clear now.
[Updated on: Tue, 11 September 2007 00:48] Report message to a moderator
|
|
|
Re: Help me on this query [message #266499 is a reply to message #266205] |
Tue, 11 September 2007 00:59 |
Arju
Messages: 1554 Registered: June 2007 Location: Dhaka,Bangladesh. Mobile:...
|
Senior Member |
|
|
@vidya, Don't you do same mistake as mine if you have a char data type. In that case use trim.
SQL> select eno,decode(trim(sal),'high',1,'medium',2,'low',3, 0) test,sal
from test order by decode(trim(sal),'high',1,'medium',2,'low',3);
ENO TEST SAL
---------- ---------- ----------
3 1 high
10 1 high
3 1 high
2 2 medium
10 3 low
1 3 low
1 3 low
7 rows selected.
[Updated on: Tue, 11 September 2007 01:00] Report message to a moderator
|
|
|
|
Re: Help me on this query [message #266510 is a reply to message #266502] |
Tue, 11 September 2007 01:28 |
Frank
Messages: 7901 Registered: March 2000
|
Senior Member |
|
|
Michel Cadot wrote on Tue, 11 September 2007 08:06 | If I have an advice, it is never use CHAR, always use VARCHAR2 (unless you have a very good reason to do so, of course).
Regards
Michel
|
I have an even better advice: even when you have a very good reason to use char, still use varchar2!
|
|
|
|
|