Click here to Skip to main content
15,892,480 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Msg 512, Level 16, State 1, Line 1
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

What I have tried:

select
first_name,salary,
case when (commission*2)>0 then (select (commission*2) from employees)
else 'nocommission'
end
from employees
Posted
Updated 17-Dec-17 17:53pm

Which employee the query trying to select? (select (commission*2) from employees) --> this can be 1, 10, 100, 1000 records. Try something like below.

SQL
select
e1.first_name,e1.salary,
case 
	when (e1.commission*2)>0 then (select TOP 1 (e2.commission*2) from employees e2 WHERE e1.EmployeeId = e2.EmployeeId)
	else 'nocommission'
end
from employees e1
 
Share this answer
 
Your subquery is returning the commission * 2 for every record in the employee table. It should only return a single record, not all of them. You don't appear to have a primary key stated anywhere, so you really can't use a subquery.

But, why are you even doing a subquery? Your case statement doesn't really make any sense. If commission * 2 is greater than 0 go do a select? Why? Your entire query should only be this:
SELECT first_name, salary, commission * 2 AS commission FROM employees

Of course, that means every commission of zero would return a 0, not "nocommission". A column should ever only return a single data type, not a numeric value for one record and a char value for another.

Of course, you could.
SELECT first_name, salary,
CASE
    WHEN commission > 0 THEN commission * 2
    ELSE 'No Commission'
END
FROM employees
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900