Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.

Posted on 2007-11-30
4
Medium Priority
?
469 Views
Last Modified: 2012-06-27
What's wrong with this code?  I'm getting the above error message.  I'm trying to delete all records that are duplicates except for the one with the largest date.  


DELETE FROM NIPRLicenseInfo
WHERE (NPN IN
(SELECT NPN, State, LicenseID
FROM NIPRLicenseInfo
GROUP BY NPN, State, LicenseID
HAVING (COUNT(NPN) > 1))
AND NOT [ExpirationDate] IN (
SELECT MAX([ExpirationDate]) as MaxOfExpire
FROM NIPRLicenseInfo
GROUP BY NPN, State, LicenseID
HAVING ((COUNT(NPN))>1)))

Open in new window

0
Comment
Question by:PSIUnit
[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
  • 2
4 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 20385463
here we go:

DELETE n
FROM NIPRLicenseInfo n
WHERE EXISTS (
   SELECT NULL 
   FROM NIPRLicenseInfo i
   WHERE i.NPN = n.NPN
     AND i.ExpirationDate > n.ExpirationDate
  )

Open in new window

0
 

Author Comment

by:PSIUnit
ID: 20385496
You are so awesome.  Why didn't I think of that?  I think I always try to overcomplicate everything...  Thanks!
0
 
LVL 15

Expert Comment

by:JimFive
ID: 20385508
What's wrong is:

WHERE (NPN IN
(SELECT NPN, State, LicenseID
FROM NIPRLicenseInfo

A SELECT Statment inside the IN must only return 1 field.

You need:
WHERE (NPN IN
(SELECT NPN
FROM...)
0
 

Author Comment

by:PSIUnit
ID: 20385524
Ok - that makes sense.  Thanks!
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

610 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