Home » SQL & PL/SQL » SQL & PL/SQL » Re-write SQL in Single Query? (merged)
| Re-write SQL in Single Query? (merged) [message #301542] |
Thu, 21 February 2008 00:38  |
ap_karthi
Messages: 87 Registered: October 2007 Location: Bangalore
|
Member |
|
|
Hi All,
select empid, substr(ename, 1, 5), sal, date
from emp
union all
select empid, ename, sal, date
from emp
Is there any other way to re-write the above query in single query, but only four columns, because this is inner query.
I dont want like below query...
select empid, substr(ename, 1, 5), ename, sal, date from emp;
Thanks in advance
bye
Karthik
|
|
|
|
|
|
| Re: Re-write SQL? [message #301553 is a reply to message #301545] |
Thu, 21 February 2008 00:48   |
ap_karthi
Messages: 87 Registered: October 2007 Location: Bangalore
|
Member |
|
|
Let me repeat the question, some more added...
Hi All,
select empid, substr(ename, 1, 5), sal, date
from emp
where substr(empid, 1, 1) between 1 and 9
union all
select empid, ename, sal, date
from emp
where substr(empid, 1, 1) not between 1 and 9
Is there any other way to re-write the above query in single query?, but only with four columns (diff. where condn.)
I don't want like below query...
select empid, substr(ename, 1, 5), ename, sal, date .....
Thanks in advance
bye
Karthik
|
|
|
|
|
|
| Re: Re-write SQL? [message #301567 is a reply to message #301542] |
Thu, 21 February 2008 01:04   |
ap_karthi
Messages: 87 Registered: October 2007 Location: Bangalore
|
Member |
|
|
Ok michel, I read the guide, now my question is...
select empid, substr(ename, 1, 5), sal, date
from emp
where substr(empid, 1, 1) between 1 and 9
union all
select empid, ename, sal, date
from emp
where substr(empid, 1, 1) not between 1 and 9
How to write the above query in single query?
|
|
|
|
|
|
| Re: Re-write SQL? [message #301696 is a reply to message #301567] |
Thu, 21 February 2008 07:04   |
 |
Littlefoot
Messages: 21826 Registered: June 2005 Location: Croatia, Europe
|
Senior Member Account Moderator |
|
|
| ap_karthi |
select empid, substr(ename, 1, 5), sal, date
from emp
where substr(empid, 1, 1) between 1 and 9
|
It is a little bit unclear what you are dealing with here.
How does the 'emp' table look like? Precisely, what is the 'empid' column's data type?
If it is a number (that's what the right side of the equity sign tells us: "between 1 and 9" -> both 1 and 9 are NUMBERS), then - if you are using the SUBSTR against it, it would be better to convert it to a character. So, you'd have
substr(TO_CHAR(empid), 1, 1) instead of what you have now.
On the other hand, if 'empid' is a character (that's what "substr(empid, 1, 1)" suggests), why don't you enclose 1 and 9 into single quotes, aswhere substr(empid, 1, 1) between '1' and '9'
Here's what I think: 'empid' is a positive number. Checking whether the first digit is "between 1 and 9" is just opposite to checking whether the first digit is equal to 0 (zero). But, if 'empid' really IS a number, then the whole story doesn't make any sense because the leading zero will not exist at all, so you'll always get SUBSTR(ename, 1, 5) as a result.
But, if 'empid' is a character, does it contain alphanumerics or just numbers? If it is a number only, you could kick out UNION you use and have something like this:select empid,
decode(substr(to_char(empid), 1, 1), '0', ename,
substr(ename, 1, 5)) name,
sal, "date"
from emp; If there are characters in there, it seems that you'd better check use of the CASE as MarcS suggested.
By the way, I hope that you didn't name a column 'date' as it is a reserved word. If you did, oh my, you must have used double quotes with the column name and this table is a complete mess.
Perhaps you should start over, from the scratch.
|
|
|
|
| in Single Query? [message #301833 is a reply to message #301542] |
Thu, 21 February 2008 23:19   |
ap_karthi
Messages: 87 Registered: October 2007 Location: Bangalore
|
Member |
|
|
Hi All,
Select a, b, c
from x
where d < '01-Feb-2008'
union all
Select to_number(e), b, c
from x
where d between '01-Feb-2008' and '15-Feb-2008';
I want to write the above query in single query, without using CASE. Is there any other method? Thanks in advance.
bye
karthik
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Goto Forum:
Current Time: Sun Nov 09 09:57:32 CST 2025
|