Skip to content
Advertisement

ORA-01427 Single row subquery returns more than 1 row

This error seems to be popular and there are many related answers. However, the existing answers do not seem to apply to my situation.

I am simplifying my case using 2 tables: Test1 and Test3 (see illustration)

What I am trying to do is attempting to find the records in test3 table that does not match the value in field value1 (if the field check_condition1 = 1 if it is 0 then I do not care)

so basically the result should be similar to this query in this particular scenario:

enter image description here

enter image description here

enter image description here

However, if I use this statement:

I got this message:

I thought my subquery “select value1 from test1 where department=t3.DEPARTMENT” should return a set for t3.value1 to check against.

How should the statement be corrected? My goal is to use Test1 table as a control table, the fields Check_condition1, check_condition2 are the “switches” that could be turn on and off without having to change the main query. Please advise if my thought make sense.

Attached are the script to create the tables test1 and test3 for easier duplication of my issue.

Advertisement

Answer

From the CASE expression documentation:

For both simple and searched CASE expressions, all of the return_exprs must either have the same datatype (CHAR, VARCHAR2, NCHAR, or NVARCHAR2, NUMBER, BINARY_FLOAT, or BINARY_DOUBLE) or must all have a numeric datatype. If all return expressions have a numeric datatype, then Oracle determines the argument with the highest numeric precedence, implicitly converts the remaining arguments to that datatype, and returns that datatype.

The return_expr of a CASE statement expects a single value so your sub-query:

is what is raising the exception.

Instead use the filter on the sub-query:

Which, for your test data, outputs:

DEPARTMENT | VALUE1 | VALUE2 | VALUE3  
---------: | :----- | :----- | :-------
         1 | Z      | Y      | Whatever

db<>fiddle here

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement