Solved

Sub-Select returning a more than 1 value Error

Posted on 2009-05-20
12
617 Views
Last Modified: 2012-05-07
I have my main query which I have been slowy adding to, to get the desired results.  I added in most recent piece and I get the follwoing error and cannot seem to get past it.

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.
Warning: Null value is eliminated by an aggregate or other SET operation.
See text file for all code.

Open in new window

0
Comment
Question by:kdeutsch
  • 6
  • 4
  • 2
12 Comments
 
LVL 9

Accepted Solution

by:
ezraa earned 500 total points
ID: 24434007
You need to make sure that the sub query you added is only returning one record.  You can use "select top 1" or "select sum()" to ensure that one value only is being returned.

Without seeing your query, we cannot offer you much more assistance.
0
 

Author Comment

by:kdeutsch
ID: 24434031
Sorry forgot to attach my code.
Help.txt
0
 

Author Comment

by:kdeutsch
ID: 24434120
Here is a follow up text file and data selecting form the querys.
Help2.txt
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 24434138
You are missing a FROM clause in the subquery.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 24434147
Never mind that is not it.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 24434201
On second thought you do appear to be missing a FROM clause in the following subquery:

(Select      (Count(ac.strAcn)) where      ac.strUic = at.UPC and ac.strPara = at.Auth_Para_Dsg and ac.strLine = at.Auth_Line_Dsg and ac.dtExpire >= getdate() and ac.dtCancelACN is null) as ACN,
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 9

Expert Comment

by:ezraa
ID: 24434273
I think the issue is the fields you are grouping in the recruitDotNet.dbo.tblACNRequest as ac table are causing the subquery to return multiple results.

You should rewrite the subquery
(Select Count(ac.strAcn) from recruitDotNet.dbo.tblACNRequest as ac where ac.strUic = at.UPC and ac.strPara = at.Auth_Para_Dsg and ac.strLine = at.Auth_Line_Dsg and ac.dtExpire >= getdate() and ac.dtCancelACN is null) as ACN,
 

also you will need to remove the ac table from the main query and remove references to it in the GROUP BY clause

Open in new window

0
 

Author Comment

by:kdeutsch
ID: 24434374
HI,
Ok I changed to this line
(Select Count(ac.strAcn) from recruitDotNet.dbo.tblACNRequest as ac where ac.strUic = at.UPC and ac.strPara = at.Auth_Para_Dsg and ac.strLine = at.Auth_Line_Dsg and ac.dtExpire >= getdate() and ac.dtCancelACN is null) as ACN,

deleted this line
recruitDotNet.dbo.tblACNRequest as ac on ac.strUIC = at.UPC Left Outer Join
and all references in the group by clause, but I still got this 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.

minus the null thing
0
 

Author Comment

by:kdeutsch
ID: 24434559
ok,
did further research and I found what it is doing here is what I returned from my smaller query when I changed it to as requested.  Id is duplicating the counts twice for the same UIC, Para and Line.  It is all depended on the UIC, Para and Line matching to get the count.  
UIC              para             line                 count
TSZHD      600      02      1
TSZHD      600      02      1
TSZHD      600      03      4
TSZHD      600      03      4
0
 

Author Comment

by:kdeutsch
ID: 24434994
OK,
I have the small one working as it needs to counting and no duplication, its all in the group by clause that was why duplication.
Select      at.UPC as UIC,
      at.Auth_Para_Dsg as PARA,
      at.Auth_Line_Dsg as LINE,
      (Select(Count(ac.intAcnId)) from tblACNREquest as ac where ac.strUic = at.UPC and ac.strPara = at.Auth_Para_Dsg and ac.strLine = at.Auth_Line_Dsg and dtExpire >= getdate() and dtCancelACN is null) as ACN
from      SIDPERS..SIDPERS.PERS_AUTH_STR_TBL as at Left Outer join
      tblACNRequest as ac on ac.strUIC = at.UPC
Group by at.UPC, at.Auth_Para_Dsg, at.Auth_Line_Dsg
Order by at.UPC

But the problem is when I put it into the big query it goes loopy and tells me to add every filed to the group by clause which then makes it through the error.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 24436940
So why don't you change this:
Select Count(*) from recruitDotNet.dbo.tblACNRequest ac where ac.strUic = at.UPC and ac.dtExpire >= getdate() and ac.dtCancelACN is null

Or depending if you want to include para as well::
Select Count(*) from recruitDotNet.dbo.tblACNRequest ac where ac.strUic = at.UPC and ac.strPara = at.Auth_Para_Dsg and ac.dtExpire >= getdate() and ac.dtCancelACN is null
0
 

Author Closing Comment

by:kdeutsch
ID: 31583602
After adding top 1 to code and redoing the group by it worked fine
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.‚Äč
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how the fundamental information of how to create a table.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now