I would like to use a variable to represent a result set that will be used in the WHERE clause of a query.
SELECT *
FROM Table1
WHERE
Exam1_ID IN (SELECT Id FROM Exam)
OR Exam2_ID IN (SELECT Id FROM Exam)
OR Exam3_ID IN (SELECT Id FROM Exam)
OR Exam4_ID IN (SELECT Id FROM Exam)
I would like to use a variable in place of SELECT Id FROM Exam so I don't have to keep repeating the query. I tried declaring a variable but since the results of the subquery could contain multiple integers I am not sure what the declare the variable as. I went ahead and tried ...
DECLARE #SubQuery INT;
SET #SubQuery = (SELECT Id FROM Exam);
SELECT *
FROM Table1
WHERE
Exam1_ID IN (#SubQuery)
OR Exam2_ID IN (#SubQuery)
OR Exam3_ID IN (#SubQuery)
OR Exam4_ID IN (#SubQuery)
I received the following error ..
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
You'd probably find that this performs better using a WHERE EXISTS like this
SELECT *
FROM Table1 t1
WHERE EXISTS ( SELECT *
FROM Exam e
WHERE t1.Exam1_ID = e.Id
OR t1.Exam2_ID = e.Id
OR t1.Exam3_ID = e.Id
OR t1.Exam4_ID = e.Id)
You can write an exists like this.
SELECT *
FROM Table1 t1
WHERE EXISTS (
SELECT 1 FROM Exam e
WHERE e.Id in ( t1.Exam1_ID , t1.Exam2_ID , t1.Exam3_ID, t1.Exam4_ID )
)
Lets begin from error message
Subquery returned more than 1 value. This is not permitted when the
subquery follows =, !=, <, <= , >, >= or when the subquery is used as
an expression.
The reason is pretty simple, you declared your subquery as INT
So if you will change it to TABLE(Id INT) you will receive new error message.
You can use different ways to solve your problem.
1) Inner join (It can give you duplicates, so I dont know is it valid for you to use this method)
DECLARE #SubQuery TABLE (RecordId INT)
INSERT INTO
#SubQuery
SELECT
Id
FROM
Exam
SELECT *
FROM
Table1 t1
INNER JOIN #Subquery sq
ON sq.Id = t1.Exam1_ID
OR sq.Id = t1.Exam2_ID
OR sq.Id = t1.Exam3_ID
OR sq.Id = t1.Exam4_ID
2) Exist
Please find sample in Rich Benner answer. He posted it while I was typing. But I found one typo there, he used different aliases in exists block
OR t1.Exam2_ID = e.Id --t1 - correct alias
OR t2.Exam2_ID = e.Id --t2 - incorrect
OR t3.Exam2_ID = e.Id --t3 - incorrect
OR t4.Exam2_ID = e.Id --t4 - incorrect
Related
select * from ChallengeResults
where ResultPercentage =
(
select CorrectAnswers from ChallengeResults a
inner join Challenge b on b.ChallengeId = a.ChallengeId
and a.ChallengeType = '2'
)
/
(
select NumberOfQuestions
from ChallengeConfiguration a
inner join Challenge b on a.ChallengeConfigurationId = b.ChallengeConfigurationId
inner join ChallengeResults c on c.ChallengeId = b.ChallengeId
and c.ChallengeType = '2'
);
This is my query.
This is the error :
Msg 512, Level 16, State 1, Line 11
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
I want to put the division of these two columns in a single column.
This is used to calculate the percentage of correct answers for a test.
I want it to return a value in the ResultPercentage column.
That error message is because your subqueries return multiple values but are used in a way such that the DB is expecting a single value.
It sounds like you want to return a column ResultPercentage based on a calculation. The correct place to do this is in the SELECT clause of your query, not the WHERE clause.
Without table structure and sample data it might not be possible to give you the correct answer, but you likely want something along the lines of:
select CorrectAnswers / NumberOfQuestions ResultPercentage, c.*
from ChallengeConfiguration a
inner join Challenge b on a.ChallengeConfigurationId = b.ChallengeConfigurationId
inner join ChallengeResults c on c.ChallengeId = b.ChallengeId
and c.ChallengeType = '2'
This is your second subquery on its own, but note the SELECT portion is altered. It will return a column named ResultPercentage that is obtained by dividing CorrectAnswers by NumberOfQuestions for each row.
I can't find issue with below query. It keeps complaining about
cannot recognize input near 'SELECT' 'wrk_prd_dt' '.' in expression specification (state=42000,code=40000)
select tb1.name from dept tb1 LEFT JOIN emp lexp ON (lexp.id = tb1.id)
where tb1.prd_dt = (SELECT wrk_prd_dt.prd_dt FROM class wrk_prd_dt order by wrk_prd_dt.prd_dt asc limit 1);
tried Second approach but same error.
SELECT tb1.name
FROM dept tb1
WHERE tb1.prd_dt >= (select min(wrk.prd_dt) from class wrk)
limit 1
I'm getting an error
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
while executing this SQL command:
select *
from Person p
where p.reference_no like (select referencenumber from Reference)
My Reference table has a column referencenumber as below
You can use exists() for this like so:
select *
from Person p
where exists (
select 1
from Reference r
where p.reference_no like r.referencenumber
)
Also, why are you using like instead of = ? If you want to do a partial comparison, shouldn't it be like '%'+r.referencenumber+'%' ?
try something different
select * from Person p where
EXISTS
(select r.referencenumber from Reference r
WHERE p.reference_no like r.referencenumber )
You could use a join based on your condition
select p.*
from Person p
inner join Reference r on p.reference_no like (r.referencenumber )
Or (due the fact your are not using wildchar ) you could use in
select * f
rom Person p
where p.reference_no in (select referencenumber from Reference)
You can do this query using exists:
select p.*
from Person p
where exists (select 1
from Reference r
where p.reference_no like r.referencenumber
);
However, if you are not using wildcards, then in suffices.
hello guys i was create a new system on database sql then i get this is error
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.
here is a query
Declare #Weapon INT = (Select ItemID From SRO_VT_SHARD.dbo._Inventory WITH (Nolock) Where CharID = #CharID
AND Slot in (Select Slot From SRO_VT_SHARD.dbo._Inventory Where CharID = #CharID And Slot Between '6' And '7'))
Declare #WeaponType3 Tinyint =
(Select Typeid3 from SRO_VT_SHARD.dbo._RefObjCommon where ID in
(Select RefItemID from SRO_VT_SHARD.dbo._Items where ID64=#Weapon))
Declare #WeaponType4 Tinyint =
(Select Typeid4 from SRO_VT_SHARD.dbo._RefObjCommon where ID =
(Select RefItemID from SRO_VT_SHARD.dbo._Items where ID64=#Weapon))
declare #RefWeapon int = (Select top 1 ID from SRO_VT_SHARD.dbo._RefObjCommon WITH (Nolock) where
Service = 1 AND
TypeID1=3 AND --- Weapon
TypeID2=1 AND
TypeID3=#WeaponType3 AND
TypeID4=#WeaponType4
Group by SRO_VT_SHARD.dbo._RefObjCommon.ID,SRO_VT_SHARD.dbo._RefObjCommon.reqlevel1
having (MAX(ReqLevel1)<=(Select CurLevel from SRO_VT_SHARD.dbo._Char where charid = #CharID))
Order By ReqLevel1 Desc
)
if (#RefWeapon is not null) And (#Weapon is not null) And (#WeaponType3 is not null) And (#WeaponType4 is not null)
begin
Update SRO_VT_SHARD.dbo._Items set RefItemID= #refweapon where ID64= #Weapon
end
i need fix for this is query
Ok, if I'm not wrong, you are getting your error code here
declare #RefWeapon int = (Select top 1 ID from SRO_VT_SHARD.dbo._RefObjCommon WITH (Nolock) where
Service = 1 AND
TypeID1=3 AND --- Weapon
TypeID2=1 AND
TypeID3=#WeaponType3 AND
TypeID4=#WeaponType4
Group by SRO_VT_SHARD.dbo._RefObjCommon.ID,SRO_VT_SHARD.dbo._RefObjCommon.reqlevel1
having (MAX(ReqLevel1)<=(Select CurLevel from SRO_VT_SHARD.dbo._Char where charid = #CharID))
Order By ReqLevel1 Desc
)
specially in this part
having (MAX(ReqLevel1)<=(Select CurLevel from SRO_VT_SHARD.dbo._Char where charid = #CharID))
Why? Ok, MAX(ReqLevel1) is 1 value but (Select CurLevel from SRO_VT_SHARD.dbo._Char where charid = #CharID) will return a lot of values. So you cannot compare them
You may be should use
(Select TOP 1 CurLevel from SRO_VT_SHARD.dbo._Char where charid = #CharID)
Sorry for my bad english. Hope this help!
here problem is that sub query is returning more than one row:
SELECT TOP 1, OR you can switch your = to IN and return multiple rows
example :
SELECT t.ParentId,t1.parentname as parentname, t.childid as childid,
(select TOP 1 table1.childid from table1 where table1 .childname=table2.childid) as childname
FROM table1 t
left JOIN table2 t1 ON t.ParentId = t1.childid
--->use your code here
I have an Sql Query. It does not have any compilation error but in runtime it shows
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
SELECT a.UserID FROM USERGROUPS a WHERE a.GroupID IN
(CASE WHEN #group_id IS NULL THEN (select groupid from usergroups) ELSE
CASE WHEN #group_id=0 THEN (select groupid from usergroups where userid = #userid)ELSE
#group_id END END )
Thanks in advance.
Try this instead:
SELECT a.UserID
FROM USERGROUPS a
WHERE
(#groupid is NUll and a.GroupID IN (select groupid from usergroups))
or (#groupid = 0 and a.GroupID IN (select groupid from usergroups where (userid = #userid)))
or a.GroupID IN (#groupid)
according to the MSDN
[Case] evaluates a list of conditions and returns one of multiple possible
result expressions.
basically you can use a SELECT within a CASE, but it only has to return one single result and not a list (which is something that can only be evaluated at runtime, that's why your query results formally correct but fails when you launch it)
If you tell us some more about what you're trying to do, maybe I can help you rewrite your query