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

Get MongoDB database support online, now!

At Percona’s web store you can order your MongoDB database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card. Handle your MongoDB database support now!

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
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 and will be exposed to the many uses the SELECT statement has.
Suggested Courses

762 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