Where clause problem in a subquery
I'll try to explain as detailed as possible.
Given the following set of data and the parameters passed the query must only return one row;
If the structure_chain is like the parameter passed, then select that row
If not then strip out the first number in both the parameter and the column,
e.i., '2.35364194.34889485' becomes '35364194.34889485'
The select is what is currently in the code and so far I've not been able to get the (new) desired results - customer changed/enhanced requirements
with TT as
(select 1 row_no, 22794978 old_child_id, 51755902 new_child_id, '1.35364194' structure_chain from dual union all
select 2, 22794978, 51755899, '1.23576950' from dual union all
select 3, 17872962, 51755893, '1.23576950' from dual union all
select 4, 17872962, 51756065, '1.35364194.34880851' from dual union all
select 5, 17872962, 51759249, '2.35364194.34880851' from dual union all
select 6, 17872962, 51759248, '1.23576950.22795468' from dual union all
select 7, 17872962, 51759250, '2.23576950.22795468' from dual )
select * from tt
where old_child_id = &1
and &2 like structure_chain ||'%'
17872962 '2.35364194.34880851' -- Return row_no 5
17872962 '1.23576950.22795468' -- Return row_no 6 - this returns more than one
17872962 '1.35364194.34880851' -- Return row_no 4
17872962 '2.23576950.22795468' -- Return row_no 7
22794978 '2.35364194.34889485' -- Return row_no 1 - does not return anything
22794978 '1.35364194.34889485' -- Return row_no 1
Hi,
I'm not sure what you're asking.
It would help if you posted the results you want as clearly as you posted the sample data. For each set of parametes, post the desired result set.
I think you want to do two kinds of pattern matches, and return the results of the strictest one that has results.
That is, if there is a match on all parts of structure_chain, then return the rows that match all parts.
But if there is no match on all parts of structure_chain, then return the rows (if any) that match if we ignore the sub-atring before the first dot.
That's an example of a Top-N Query , and here's one way to do it:
WITH got_r_num AS
SELECT tt.*
, DENSE_RANK () OVER (ORDER BY CASE
WHEN '&2' LIKE structure_chain || '%'
THEN 1
ELSE 2
END
) AS r_num
FROM tt
WHERE old_child_id = &1
AND SUBSTR ( '&2'
, INSTR ('&2', '.')
) LIKE SUBSTR ( structure_chain
, INSTR (structure_chain, '.')
) || '%'
SELECT row_no, old_child_id, new_child_id, structure_chain
FROM got_r_num
WHERE r_num = 1
;With parameters &1=17872962 and &2=1.23576950.22795468, the query above produces this output:
` ROW_NO OLD_CHILD_ID NEW_CHILD_ID STRUCTURE_CHAIN
3 17872962 51755893 1.23576950
6 17872962 51759248 1.23576950.22795468because both rows matched the given &2, including the '1.' at the beginning. (The query you posted gave the same results in this case.)
With parameters &1=22794978 and &2=2.35364194.34889485, the query above produces:
ROW_NO OLD_CHILD_ID NEW_CHILD_ID STRUCTURE_CHAIN
1 22794978 51755902 1.35364194because there was no match when considering the '2.' at the beginning of &2, but the row shown does match when we ignore the '2.'.
The WHERE clause in the sub-query narrows the result set down to rows that meet at least the looser match reuirement.
The CASE expression ranks each row as 1 if it meets the stricter requirement, and 2 if it only met the looser one.
DENSE_RANK returns 1 for the lower of those numbers that was actually found.
Similar Messages
-
[SQL] Where clause problem with subqueries
Hi,
I'm writing a query for an apex application where i have a field (P47_UNNR) for filtering. In my where clause I try to do this:
... AND qo.pk_id IN
CASE
WHEN :P47_UNNR IS NULL
THEN
(SELECT pk_id FROM quote_orders)
ELSE
(SELECT QOR.FK_QUOTE_ORDER_ID
FROM QUOTE_ORDER_RIDS QOR
JOIN
RIDS RID
ON RID.PK_ID = QOR.FK_RID_ID
WHERE RID.NAME LIKE :P47_UNNR)
END
But it gives an error "ORA-01427: single-row subquery returns more than one row"
I have tried multiple ways to get around this problem but don't seem to be able to do this. Does anyone have a suggestion on how to solve this?Hi Niels,
First try with separating the query.
First try with:
CASE
WHEN :P47_UNNR IS NULL
THEN
(SELECT pk_id FROM quote_orders)
END;check SELECT pk_id FROM quote_orders this query returning only one values or not???
Then try with
CASE
WHEN :P47_UNNR IS NULL
THEN
(SELECT pk_id FROM quote_orders)
ELSE
(SELECT QOR.FK_QUOTE_ORDER_ID
FROM QUOTE_ORDER_RIDS QOR
JOIN
RIDS RID
ON RID.PK_ID = QOR.FK_RID_ID
WHERE RID.NAME LIKE :P47_UNNR)
ENDCheck this
SELECT QOR.FK_QUOTE_ORDER_ID
FROM QUOTE_ORDER_RIDS QOR
JOIN
RIDS RID
ON RID.PK_ID = QOR.FK_RID_ID
WHERE RID.NAME LIKE :P47_UNNRquery returning one values or not??
If those returns more than one value , the check whether there is any duplication value's present, remove that duplicate value from your table.
if you cant remove just add rownum =1 condition in your where clause.
Cheers,
San -
Function in where clause being executed in subquery
Oracle version 9.2.0.8
I have a query of the form
select cola
,colb
,expensive_function(colb) as colc
from t
where cola between 1 and 100This query returns 100 rows very fast.
When I try to specify a where clause that uses the result of the expensive function the query takes 60 times longer (60 secs). Table t has a million rows.
Long Query:
select *
from (
select cola
,colb
,expensive_function(colb) as colc
from t
where cola between 1 and 100) t
where colc = 'XXX'Could it be that Oracle is executing the function for each row in table t? I only want the function executed on the resultset but I can't seem to get it to work.
Have tried many different hints and forms of the query without success, e.g.
select *
from (
select cola
,colb
from t
where cola between 1 and 100) t
where expensive_function(colb) = 'XXX'
select /*+ NO_PUSH_PRED(t) */ *
from (
select cola
,colb
from t
where cola between 1 and 100) t
where expensive_function(colb) = 'XXX'Message was edited by:
SamB<s></s>Compare planing of two SQL, and post what is difference?
@?/rdbms/admin/utlxplan
explain plan for
select cola
,colb
,expensive_function(colb) as colc
from t
where cola between 1 and 100
@?/rdbms/admin/utlxpls
explain plan for
select *
from (
select cola
,colb
,expensive_function(colb) as colc
from t
where cola between 1 and 100) t
where colc = 'XXX'
@?/rdbms/admin/utlxpls
explain plan for
select cola
,colb
from t
where cola between 1 and 100
and expensive_function(colb)='XXX'
@?/rdbms/admin/utlxpls -
Where clause problem with sub-queries in forms 6i
where is the best place to put a sub query?? I have been using the set block property, however when running a sub query an alternative method must be used. what are some other options?? I have tried to have the query directly in the data block property palette but to no avail...
There are 2 tables project and assignment
maximum resources cannot be exceeded therefore a count of employees
on the assignment table establishes that there are open spaces on the project.
This code Here is my code for my where clause on the data block
project.resources_maximum >
(Select Count(assignment.employee_ID)
From assignment
Where assignment.project_ID = project.project_ID
group by assignment.project_ID
and project.end_date>sysdate;
I was reading that the project table must be defined in the from clause
to have the query go row by row, otherwise multiple rows are returned
however I cannot define the project in the from,
could this be why this doesn't work???
original SQL that does work
Need to migrate this to forms
Select project.project_ID
from project
Where project.resources_maximum >
(Select Count(assignment.employee_ID)
From assignment
Where assignment.project_ID = project.project_ID
group by assignment.project_ID
and project.end_date>sysdate;Looking at your query that works at the bottom, I do not see any reason that you would need to include the Assignment table in the From list. Just use the same where clause in the form:
Where project.resources_maximum
> (Select Count(assignment.employee_ID)
From assignment
Where assignment.project_ID = project.project_ID
group by assignment.project_ID ) -
Dynamic where clause problem?
Hi all,
I am using 4 parameters for the attrition rate report of an employee on the selection screen.
Level
Grade
Position
Salary Range (pa0008-ansal)
i have filled the values of level, grade, position, from database tables at the initialization event.
And for the salary range i hardcoded them to 0-5000, 5001-10000 etc.. and so on.
and then showed all 4 parameters are listbox on the selection screen.
now the first 3 parameters are from infotype pa0001 and the last is from pa0008.
Now I want to select pernr, persg ,persk ,plans, begda ,endda fields from infotype pa0001, and 'ansal' field from pa0008 , with the help of a dynamic where clause, is it possible, because here it might be needed for a join as well.
Please help me out with some sample coding help.
Regards
TarunDATA: lv_query TYPE string.
lv_query = ' '.
IF position IS NOT INITIAL.
IF lv_query IS NOT INITIAL.
CONCATENATE lv_query ' and p1~plans eq position ' '' INTO lv_query.
ELSE.
CONCATENATE ' p1~plans eq position ' '' INTO lv_query.
ENDIF.
ENDIF.
IF grade IS NOT INITIAL.
IF lv_query IS NOT INITIAL .
CONCATENATE lv_query ' and p1~PERSK eq grade ' '' INTO lv_query.
ELSE.
CONCATENATE ' p1~PERSK eq grade ' '' INTO lv_query.
ENDIF.
ENDIF.
IF level IS NOT INITIAL.
IF lv_query IS NOT INITIAL .
CONCATENATE lv_query ' and p1~PERsg eq level ' '' INTO lv_query.
ELSE.
CONCATENATE ' p1~PERsg eq level ' '' INTO lv_query.
ENDIF.
ENDIF.
IF sal_slab IS NOT INITIAL.
SELECT pernr ansal begda endda FROM pa0008 INTO CORRESPONDING FIELDS OF TABLE lt_st_p8 WHERE ansal = sal_slab.
*IF lv_query IS NOT INITIAL .*
*CONCATENATE lv_query ' and p8~ansal le sal_slab ' '' INTO lv_query.*
*ELSE.*
*CONCATENATE ' p8~ansal le sal_slab ' '' INTO lv_query.*
*ENDIF.*
ENDIF.
SELECT p1~pernr
p1~persg p1~persk p1~plans p1~begda p1~endda
p8~ansal
INTO CORRESPONDING FIELDS of TABLE lt_st_p1_p8
FROM pa0001 AS p1 JOIN pa0008 AS p8 ON p1~pernr = p8~pernr
WHERE (lv_query) .
<Added code tags>
Now in case of salary slab, i have ranges like 0-5000 , 5001-10000 and so on and I showed it as a listbox on sel. screen.
So how can I read the figure after the '-'?????
And also review, if for the previous 3 parameters it is correct, or it can be shortened and accurated.
Edited by: Suhas Saha on Dec 28, 2011 1:44 PM -
Function in where clause problem
Hi, I use Oracle 9i release 2.
Imagine a Query:
select e.* from employee e
left outer join department d
on e.dep_id=d.dep_id
where
d.start_date=fn_get_start_date(some_value);
I need all employees who works in departments which started there existance on date that is return by function.
Now on Oracle 11 g R2 I can do this. But Oracle 9 can't do this. Why function make problems?
ThxHmm... doesn't say much ;-) I presume that's your client environment or developing tool that gives that error? It does not look like an ORA-xxxxx error?
What happens if you do the same statement in a SQL*PLUS session? That might give a more meaningful error?
A wild guess could be something to do with your ANSI join syntax. I have myself sometimes stumbled into bugs in version 9 when using ANSI join syntax (mostly on full outer join, though.)
But you could perhaps try it the old-fashioned Oracle join:
select e.*
from employee e, department d
where d.start_date=fn_get_start_date(some_value)
and d.dep_id(+) = e.dep_id
;Oh - and now I realize that your outer join is unnecessary?
You can just:
select e.* from employee e
join department d
on e.dep_id=d.dep_id
where
d.start_date=fn_get_start_date(some_value);Or:
select e.*
from employee e, department d
where d.start_date=fn_get_start_date(some_value)
and d.dep_id = e.dep_id
;But it is a wild guess - it will only help if you happen to have stumbled upon a bug with ANSI joins.
If that does not work, then try in SQL*PLUS to get an ORA-xxxxx error message that we can google for ;-) -
Hello all,
i think it is an old problem but i cannot find a solution.
i have the following table
CREATE TABLE XX
ID CHAR(10)NOT NULL,
other colums
the table has some rows in it, but when i do the following Select, i get no rows back:
SELECT *
FROM XX
WHERE ID >= ''
( normally the '' is replaced by a variable, but i tried it also with SQL*Plus )
Any suggestions ?
nullyou'r right, but the problem is, i have to do with Oracle, DB2, SQL Server, Informix, and with ALL the other databases the empty string is NOT NULL, so you can compare it to values.
The problem is that the value is, that i have an application which is connected through ODBC resp. JDBC to different database servers, and only Oracle does the compare in that manner.
so far
<BLOCKQUOTE><font size="1" face="Verdana, Arial">quote:</font><HR>Originally posted by Eugenio:
The empty string '' means NULL and
NULL cannot be compared with values.
select * from dba_objects where object_name >= ''
gives "no rows selected"
If you try with ' ' (blank space) you'll have thousands of rows (in my new 8i test db 22436 ;).
Ciao<HR></BLOCKQUOTE>
null -
VO - bind variable in where clause problem
Hi
I have Jdeveloper 12. I successfully connected to mySQL database which works in Oracle mode (PIPES_AS_CONCAT, ANSI_QUOTES, IGNORE_SPACE, NO_KEY_OPTIONS, NO_TABLE_OPTIONS, NO_FIELD_OPTIONS, NO_AUTO_CREATE_USER)
Next I created EO, VO (screenshot) and finally AM.
when I run Application Module and enter bind variable value I receive error (screenshot):
oracle.jbo.SQLStmtException: JBO-27122: SQL error during statement preparation. Statement: SELECT EO_TMP.create_date, EO_TMP.id, EO_TMP.name FROM autoid.advert EO_TMP WHERE EO_TMP.id = :var1
## Detail 0 ##
java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0).
Doesn anybody know hot to fix this issue?
Regards.I decided to check if it is a mySQL driver so I've performed the same query using a below code and it works. This is definitely model configuration issue.
Connection con = null;
try{
Class.forName( "com.mysql.jdbc.Driver" ).newInstance();
con = DriverManager.getConnection("jdbc:mysql://localhost/sakila?user=root&password=1qaz2wsx");
String selectSQL = "SELECT actor_id,first_name,last_name,last_update FROM actor WHERE actor_id = ?";
PreparedStatement preparedStatement = con.prepareStatement(selectSQL);
preparedStatement.setInt(1, 4);
ResultSet rs = preparedStatement.executeQuery();
while (rs.next()) {
String userid = rs.getString("ACTOR_ID");
String username = rs.getString("FIRST_NAME");
System.out.println(userid);
System.out.println(username);
}catch (Exception e){
System.out.println(e.toString()); -
Query...where clause problem in XSQL
I am getting error with following simple example: (however it works fine when the < sign changed to greater than > sign). Anybody any idea?
select * from emp
where empno < 7876
XSQL-005: XSQL page is not well-formed.
XML parse error at line , char
Expected name instead of .
nullThe "less-than" sign (also known as the "left-angle-bracket") is a special character in an XML file, since it is the start of an element like <foo>.
So, you either have to write your query like this:
<xsql:query>
select * from emp where empno &;lt; 7839
</xsql:query>or like this, escaping the entire query with a CDATA section:
<xsql:query>
<![CDATA[
select * from emp where empno < 7839
]]>
</xsql:query>A third option is that instead of writing:
empno < 7839
you exploit the fact that this is equivalent to:
7839 > empno
and write it that way. Since "greater-than" sign does not need to be escaped specially in an XML file, it gets the job done, too. -
Hello!I beg your pardon for my poor English.
Oracle prompts "invalid month" when running the following sentence:
select *
from xtcs xtcs
where xtcs.csmc like '%month'
and (xtcs.cnq,xtcs.csmc) not in (
select distinct
to_char(add_months(to_date((case when substr(yhjf1.pzh,1,2)>='50' then '19' else '20' end)||substr(yhjf1.pzh,1,4),'yyyymm'),-4),'yyyy') pzcnq
,yhjf1.pzhbs||'month'
from yhjf yhjf1
where yhjf1.pzhbs is not null);
But,oracle runs well when running only the subquery in where clause:
select distinct
to_char(add_months(to_date((case when substr(yhjf1.pzh,1,2)>='50' then '19' else '20' end)||substr(yhjf1.pzh,1,4),'yyyymm'),-4),'yyyy') pzcnq
,yhjf1.pzhbs||'month'
from yhjf yhjf1
where yhjf1.pzhbs is not null;
I have checked the data in the table yhjf.And there are some wrong about field pzh which cause oracle wrong .But the where clause 'yhjf1.pzhbs is not null' can filter them out.I'm being in puzzle now.Please tell me why and how I can do.Thanks very much!> Sorry,I'm a Chinese.The names originally were in
Chinese.I converted them into alphabets before posting.
I did not think of that option. I'm glad to hear that the names are meaningful to you :-)
> My data is very large and I had not created a
small data set on which the error happens.
To know what the problem is, you will have to identify the rows that are causing the trouble. Once identified, the solution should be simple.
Regards,
Rob. -
Urgent: Performance problem with where clause using IN and an OR condition
Select statement is:
select fl.feed_line_id
from ap_expense_feed_lines_all fl
where ((:1 is not null and
fl.feed_line_id in (select distinct r2.object_id
from xxdl_pcard_wf_routing_lists r2,
per_people_f hr2
where upper(hr2.full_name) like upper(:1||'%')
and hr2.person_id = r2.person_id
and r2.fyi_list is null
and r2.sequence_number <> 0))
or
(:1 is null))
If I modify the statement to remove the "or (:1 is null))" part at the bottom of the where clause, it returns in .16 seconds. If I modify the statement to only contain the "(:1 is null))" part of the where clause, it returns in .02 seconds. With the whole statement above, it returns in 477 seconds. Anyone have any suggestions?
Explain plan for the whole statement is:
(1) SELECT STATEMENT CHOOSE
Est. Rows: 10,960 Cost: 212
FILTER
(2) TABLE ACCESS FULL AP.AP_EXPENSE_FEED_LINES_ALL [Analyzed]
(2) Blocks: 8,610 Est. Rows: 10,960 of 209,260 Cost: 212
Tablespace: APD
(6) TABLE ACCESS BY INDEX ROWID HR.PER_ALL_PEOPLE_F [Analyzed]
(6) Blocks: 4,580 Est. Rows: 1 of 85,500 Cost: 2
Tablespace: HRD
(5) NESTED LOOPS
Est. Rows: 1 Cost: 4
(3) TABLE ACCESS FULL XXDL.XXDL_PCARD_WF_ROUTING_LISTS [Analyzed]
(3) Blocks: 19 Est. Rows: 1 of 1,303 Cost: 2
Tablespace: XXDLD
(4) UNIQUE INDEX RANGE SCAN HR.PER_PEOPLE_F_PK [Analyzed]
Est. Rows: 1 Cost: 1
Thanks in advance,
PeterThanks for the reply, but I have already checked what you are suggesting and I am pretty sure those are not causing the problem. The hr2.full_name column has an upper index and the (4) line of the explain plan shows that index being used. In addition, that part of the query executes on its own quickly.
Because the sql is not displayed in an indented format on this page it is a little hard to understand the structure so I am going to restate what is happening.
My sql is:
select a_column
from a_table
where ((:1 is not null) and a_column in (sub-select statement)
or
(:1 is null))
The :1 bind variable is set to a varchar2 entered on the screen of an application.
If I execute either part of the sql without the OR condition, performance is good.
If the :1 bind variable is null with the whole sql statement (so all rows or a_table are returned), performance is still good.
If the :1 bind variable is a not-null value with the whole sql statement, performance stinks.
As an example:
where (('wa' is not null) and a_column in (sub-select statement)) -- fast
where (('wa' is null)) -- fast
where (('' is not null) and a_column in (sub-select statement) -- fast
or
('' is null))
where (('wa' is not null) and a_column in (sub-select statement) -- slow
or
('wa' is null)) -
Problem in case of or condition in where clause in case of leftouter joi.
hi
i am encountering a wierd problem.
in a select query if i have a left outer join and a or condition in where clause the order of condition in or matters and if i use a to_char problem is solved.
see query below
select * from custsupp left outer join Salesperson s on custsupp.sales_rep = s.id ,state a where (CS_FLAG='C' or CS_flag ='B') and custsupp.state = a.id
i have a single record in table custsupp with CS_FLAG ='B' the above does not return result. but if i move CS_FLAG='B' on left of or i.e
select * from custsupp left outer join Salesperson s on custsupp.sales_rep = s.id ,state a where (CS_FLAG='B' or CS_flag ='C') and custsupp.state = a.id
i get the rsult.
also if i remove left outer join and keep order of condition as it is i get result i.e
select * from custsupp ,state a where (CS_FLAG='C' or CS_flag ='B') and custsupp.state = a.id
also if i add a to_char to co,umns CS_FLAG i get the result.
select * from custsupp left outer join Salesperson s on custsupp.sales_rep = s.id ,state a where (to_char(CS_FLAG)='C' or to_char(CS_flag ='B')) and custsupp.state = a.id
why it is so?
CS_flag is of type nchar
Shreyas
Edited by: shreyasd on Jun 9, 2010 11:07 PMFirst if you don't want to take credit of the free sample then follow the below procedure:
1. Do the GRN for all the three item and for the free item in the excise tab at the item level select the material as non cenvatable. Just remember to add base value of the item
2. If you want to capture the Part1 entry then just do the normal transaction and while posting the excise with J1IEX just remove the excise duties for the free item.
3. For paying the vendor the excise amount, just do subsequent debit in the MIRO and in the details tab add in the unplanned delivery cost the amount that you want to pay for the excise duties
Second case if you want to take credit of the duties:
1. Do the Normal GRN process, add the base value and excise values for the free item.
2. Post the Excise duites with J1iEX.
3. The with the account entry you can settle the excise payment
Hope this will help you
Enjoyyyyyyyyyyy
Akshit -
PL/SQL Evaluation problem of where clause in case of NUMBER column type
I found the following problem in Oracle® Database 2 Day Developer's Guide 11g Release 1 (11.1) B28843-04:
The sole parameter of function eval_frequency is employee_id IN employees.employee_id%TYPE.
An ORA-01422 exception occurs when the execution reaches the following select command
SELECT e.hire_date
INTO hire_date
FROM employees e
WHERE employee_id= e.employee_id;
A possible cause of the error is that the type of employee_id is NUMBER while the employees.employee_id is NUMBER(6,0) . The result of the selection is the same as there were no WHERE clause at all.
Everything worked fine, when I declared a temporary variable of NUMBER(6,0) for storing the actual parameter of function and used this variable in the where clause, but I consider this "solution" as being no solution.
It is pointless to use %TYPE parameter of a function for flexibility if I must degrade this flexibility by a fixed declaration of a temporary variable of the same type as the column in question.
What is wrong?
The Developer'Guide I used, the Oracle Sql Developer I used or the PL/SQL version ?Hi,
Welcome to the forum!
user8949829 wrote:
A possible cause of the error is that the type of employee_id is NUMBER while the employees.employee_id is NUMBER(6,0) . The result of the selection is the same as there I don't think so. The variable employee_id is defined as having the exact same type as the eponymous column. Even if it didn't, I believe Oracle will always implicity convert between datatypes when possible, rounding if necessary. That may cause errors, but it isn't causing this error.
No, the error has nothing to do with the data type. It has to do with the ambiguity of employee_id: is it a column, or is it a variable?
The default is that it means the column name, so
WHERE employee_id = e.employee_idis equivalent to saying
WHERE e.employee_id = e.employee_idwhich isn't quite the same thing as not having a WHERE clause; rows with NULL employee_id would still be excluded, if there were any.
I think it's best not to use variable names that are the same as column names. You could call the variable v_employee_id, or, since it's an IN-argument, in_employee_id.
If you must use a variable that can be mistaken for a column, then qulaify it with the name of the procedure, like this:
WHERE eval_frequency.employee_id = e.employee_id
Everything worked fine, when I declared a temporary variable of NUMBER(6,0) for storing the actual parameter of function That makes sens. You probably gave that variable a name that couldn't be mistaken for a column in the table.
Edited by: Frank Kulash on Jan 12, 2011 8:27 PM -
Problem with DECODE block in WHERE clause
Hi,
I'm facing problem with DECODE statement. I just simulated my problem in the simple way as follows. If I execute this following query, I should get "hello", but I'm not getting anything (ZERO rows returned).
SELECT 'hello' FROM DUAL
WHERE 'sample1' in (DECODE(1, 1, '''sample1'', ''sample2'', ''sample3''',
2, '''sample4'', ''sample5'', ''sample6'''
I think some problem is there in my WHERE clause.
But When I'm exeucting the following query as a seperate query, then I'm getting the value of DECODE block properly, but didn;t understnad why its not returning the same way when I'm putting the same DECODE statement in WHERE clause.
SELECT DECODE(1, 1, '''sample1'', ''sample2'', ''sample3''',
2, '''sample4'', ''sample5'', ''sample6'''
FROM DUAL;
Please help me to get out of this problem. Thank you so much in advance.
Thanks,
Ramji.The value returned by SELECT DECODE(1, 1, '''sample1'', ''sample2'', ''sample3''',2, '''sample4'', ''sample5'', ''sample6''') FROM DUAL;
'sample1', 'sample2', 'sample3' is a single string. Consider it x.
SELECT 'hello' FROM DUAL WHERE 'sample1' in ( DECODE(1, 1, '''sample1'', ''sample2'', ''sample3''',2, '''sample4'', ''sample5'', ''sample6'''));
is like SELECT 'hello' FROM DUAL WHERE 'sample1' in ('x');
or
SELECT 'hello' FROM DUAL WHERE 'sample1' in ('''sample1'', ''sample2'', ''sample3''') and not
SELECT 'hello' FROM DUAL WHERE 'sample1' in ('sample1', 'sample2', 'sample3');
For this same reason SELECT 'hello' FROM DUAL WHERE 'sample1' in (select '''sample1'', ''sample2'', ''sample3''' from dual);
also does'nt work.
Please use INSTR to find whether 'sample1' exists in the string 'sample1', 'sample2', 'sample3'. -
DECODE is not working in WHERE clause when subquery returns more rows
Hi Gurus,
I want to write a query on CCENTERS table(Script given below) and expect the following result:
1. When I pass a value of 0 for ID, It returns all the rows given in the table.
2. When I pass a value other than 0, It returns the row for the given value as well as all its child records.
CCENTER has parent-child relationship in ID and BASE column. I am using a query with DECODE function. but DECODE function in WHERE clause is not capable of handling sub-query with multiple rows.
VARIABLE ParaCCenter NUMBER
BEGIN
:paraccenter:=0;
END;
CREATE TABLE ccenters
(id NUMBER,
name VARCHAR2(20),
base number);
INSERT INTO ccenters VALUES(1,'NUST',null);
INSERT INTO ccenters VALUES(2,'SEECS',1);
INSERT INTO ccenters VALUES(3,'NBS',1);
commit;
SELECT * FROM ccenters
WHERE id IN DECODE(:ParaCCenter, 0, id,
(SELECT id FROM ccenters
START WITH base=:ParaCCenter
CONNECT BY PRIOR id = base
UNION
SELECT :ParaCCenter FROM dual
BEGIN
:paraCCenter:=1;
END;
SELECT * FROM ccenters
WHERE id IN DECODE(:ParaCCenter, 0, id,
(SELECT id FROM ccenters
START WITH base=:ParaCCenter
CONNECT BY PRIOR id = base
UNION
SELECT :ParaCCenter FROM dual))
The result is
(SELECT id FROM ccenters
ERROR at line 3:
ORA-01427: single-row subquery returns more than one row
How this query can be rewritten for the given functionality. Any response will be highly appreciated.
ThanksAnd if you want to use DECODE:
SQL> BEGIN
2 :paraccenter:=0;
3 END;
4 /
PL/SQL procedure successfully completed.
SQL> select *
2 from ccenters
3 where :paraccenter = decode(:paraccenter,0,0,id)
4 /
ID NAME BASE
1 NUST
2 SEECS 1
3 NBS 1
SQL> BEGIN
2 :paraccenter:=2;
3 END;
4 /
PL/SQL procedure successfully completed.
SQL> select *
2 from ccenters
3 where :paraccenter = decode(:paraccenter,0,0,id)
4 /
ID NAME BASE
2 SEECS 1
SQL> SY.
Maybe you are looking for
-
Hi, I have set a Rule in PE51 and is not working, what I want to do is to set BETRG to 0.00 if this is less than 0 so my rule is 01 1 XRT BETRG N LT '0 '0.00 01 2 XRT BETRG N GT '0 XRT BETRG Or how can I do to set BETRG to 0 if it comes with a nega
-
Wishing icons in finder were in color
I knew Lion was in that washed out grey but I thought for sure by now they would have changed it or someone would have made some kind of tweek for it. I watched a few ML movies and never saw the finder sidebar. Bummer. Does anyone have any ideas? tha
-
Converting To Excel File.
Hi all, I hv developed a application for Reporting purpose using Swings. I hv queried and got my result in a vector. Now i want to save them in Excel Format. How do i do this. If i save using FileOutputStream with the .xls extension it is not working
-
Installing PS CC 2014 on multiple computers
Is it permissible to install on more than one computer?
-
Excise Invoice only part 1 posted
All SAP Gurus, In J1I7, we can get the list of Excise invoices whose part 1 have been posted and part II not posted. From here we can know the Basic Excise Duty, EXD and SED for thaat excise invice. Is there any report available from which we can kno