Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more: , +
XML
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

while running this query
SQL
select (PRICE) as Closingprice,(select PRICE from Dataupload  where  id in(
select id  from(
select  MAX(D.ID)as id,D.Date,D.BREMPROCODE from dataupload D
 where D.BREMPROCODE IN(SELECT BREMPROCODE FROM Dataupload
 WHERE REGIONCODE='005')  AND MONTH(date)=MONTH(GETDATE())
 and day(date)=day(getdate()) GROUP BY D.Date,D.BREMPROCODE   )x) ) as CurrentPrice
 from Dataupload where REGIONCODE='005'
 and MONTH(date)=MONTH(GETDATE()-1)and day(date)=day(getdate()-1) and time='7 PM'
Posted
Comments
gregorio89 8-Jul-15 8:15am    
Abhinav's answer is correct .
But what you have to do with this query?

1 solution

The sub query i.e.
SQL
(
select  MAX(D.ID)as id,D.Date,D.BREMPROCODE from dataupload D
 where D.BREMPROCODE IN(SELECT BREMPROCODE FROM Dataupload
 WHERE REGIONCODE='005')  AND MONTH(date)=MONTH(GETDATE())
 and day(date)=day(getdate()) GROUP BY D.Date,D.BREMPROCODE   )x) ) as CurrentPrice
 from Dataupload where REGIONCODE='005'
 and MONTH(date)=MONTH(GETDATE()-1)and day(date)=day(getdate()-1) and time='7 PM'

returns more than value.

Pick the first one.
E.g.
SQL
(
select  top 1 MAX(D.ID)as id,D.Date,D.BREMPROCODE from dataupload D
 where D.BREMPROCODE IN(SELECT BREMPROCODE FROM Dataupload
 WHERE REGIONCODE='005')  AND MONTH(date)=MONTH(GETDATE())
 and day(date)=day(getdate()) GROUP BY D.Date,D.BREMPROCODE   )x) ) as CurrentPrice
 from Dataupload where REGIONCODE='005'
 and MONTH(date)=MONTH(GETDATE()-1)and day(date)=day(getdate()-1) and time='7 PM'
 
Share this answer
 
Comments
Herman<T>.Instance 8-Jul-15 11:10am    
'top 1 MAX(D.ID)' can be costly. Better:
Create an index on ID that is descending. and SELECT TOP 1 D.ID .... FROM ..... ORDER BY D.ID DESC. The query should not have to scan the complete table to find the MAX. It pinpoints straight to the highest number value for D.ID

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