Click here to Skip to main content
15,886,809 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
When i am running this code ,it is giving me this error,


Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.


the code is like below,please resolve my problem ASAP.

SQL
Select ttt.FdDmCd,ttt.Col,ttt.CFP,sum(ttt.Qty)as FgQty,sum(ttt.gwt) as GldWt,sum(ttt.swt)as SilWt From
(Select  Fdbyy,fdbchr,fdbno,(select OdDmCol from OrdDsg Where Odtc=FdPrdOdtc and Odyy=FdPrdOdyy and odchr=FdPrdOdChr and
Odno=FdPrdOdNo and odsr=FdPrdOdSr)as col, (Select OlScd FRom OrdLab where OlMcd='CFP' and  Oltc=FdPrdOdtc and
 Olyy=FdPrdOdyy and olchr=FdPrdOdChr and Olno=FdPrdOdNo and olSr=FdPrdOdSr)as CFP, Max(FdDmCd)as FdDmCd,max(FdQty) as Qty,
 (Select Sum(case when frrmctg ='G' and frrmsctg='-' then  FrRmWt else 0 end) from fgrm Where FrFdIdNo =Max(FDIdNo)
Group by FrFdIdNo)as GWt, (Select Sum(case when frrmctg ='S' and frrmsctg='-'  then  FrRmWt else 0 end) from fgrm
Where FrFdIdNo =Max(FDIdNo) Group by FrFdIdNo)as SWt from fgd Join (Select Distinct fdbyy as fdbyy1,fdbchr as fdbchr1 ,
fdbno as fdbno1,Count(*)as Count1 from fgd Where fddt between '01/04/14' and '02/04/14' and fdtc='FB' and fdsubloc = 'exp'
 and FdPrdOdChr in ('JKS','KPS','JOB')
 Group by fdbyy,fdbchr,fdbno ) as tab on fdbyy=tab.fdbyy1 and fdbchr=tab.fdbchr1 and fdbno=tab.fdbno1
Where fddt between '01/04/14' and '02/04/14' and fdtc='FB'  and FdPrdOdChr in ('JKS','KPS','JOB')
group by Fdbyy,fdbchr,fdbno ,FdPrdOdtc ,FdPrdOdyy,FdPrdOdChr,FdPrdOdNo,FdPrdOdSr
Union all
Select  Fdbyy,fdbchr,fdbno,(select OdDmCol from OrdDsg Where Odtc=FdPrdOdtc and Odyy=FdPrdOdyy and odchr=FdPrdOdChr and
 Odno=FdPrdOdNo and odsr=FdPrdOdSr)as col, (Select OlScd FRom OrdLab where OlMcd='CFP' and  Oltc=FdPrdOdtc and
Olyy=FdPrdOdyy and olchr=FdPrdOdChr and Olno=FdPrdOdNo and olSr=FdPrdOdSr)as CFP, FdDmCd,FdQty,
(Select Sum(case when frrmctg ='G' and frrmsctg='-'  then  FrRmWt else 0 end) from fgrm Where FrFdIdNo =FDIdNo
Group by FrFdIdNo)as GWt, (Select Sum(case when frrmctg ='S' and frrmsctg='-'  then  FrRmWt else 0 end) from fgrm
Where FrFdIdNo =FDIdNo Group by FrFdIdNo)as SWt  from fgd  Join (Select Distinct fdbyy as fdbyy1,fdbchr as fdbchr1 ,
fdbno as fdbno1 from fgd Where fddt between '01/04/14' and '02/04/14'
and fdtc='FB'  and FdPrdOdChr in ('JKS','KPS','JOB') group by Fdbyy,fdbchr,fdbno ,FdPrdOdtc ,FdPrdOdyy,FdPrdOdChr,
FdPrdOdNo,FdPrdOdSr ) as tab on fdbyy=tab.fdbyy1 and fdbchr=tab.fdbchr1 and fdbno=tab.fdbno1
 Where fddt between '01/04/14' and '02/04/14' and fdtc='FB'  and fdsubloc = 'exp' and
 FdPrdOdChr in ('JKS','KPS','JOB') ) as ttt Group by ttt.FdDmCd,ttt.Col,ttt.CFP
Posted

The error meassage says exactly what is wrong! The data from subquery are not related with main query and return more then 1 record!

Using too many subqueries to produce columns could be the reason of application crash or performance issue at least.

Redesign your query! Start with basics: check what result returns each subquery! Use Joins! Be careful using subqueries!

Follow this instruction: Scalar Subqueries[^]
 
Share this answer
 
Comments
Abhinav S 2-May-14 4:11am    
5.
Maciej Los 2-May-14 4:22am    
Thank you, Abhinav ;)
You have used many subqueries.
Check each of these to ensure they don't return more than one query.
 
Share this answer
 

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