Solved

Sub-Select returning a more than 1 value Error

Posted on 2009-05-20
12
669 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
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
 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

724 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