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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how the fundamental information of how to create a table.

690 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