Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Problem getting my subquery to work

Author  Topic 

debshutts
Starting Member

1 Post

Posted - 2010-09-23 : 13:21:09
Hi all,

I am needing to import data from a csv file into a SQL 2000 table. The file has 92 rows of duplicate data, which I would like to delete prior to importing. I have imported the csv file into a temporary table, where I want to delete the duplicate rows, then copy the data into its destination table. When I run the query below I receive the following error: Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.

Here's my query:

Delete
FROM dbo.PlanDefImport
WHERE dbo.PlanDefImport.PLAN_ID
in (select dbo.PlanDefImport. PLAN_ID, Col001, Col002
from dbo.PlanDefImport, tai.TAIPlan
where tai.TAIPlan.Plan_Co = dbo.PlanDefImport.Col001
and tai.TAIPlan.Plan_Plan = dbo.PlanDefImport.Col002)

I need to include both columns in my subquery in order to isolate the duplicates, so I can't just get rid of one of these columns. I know there are a number of ways to resolve this, but my brain is tired of staring at it, and I'm hoping someone can help me find the simplest way to get this data dropped. For only 92 rows I could have done this manually about 100 times by now, but it's the principle of the thing, and I really need to learn this.

Any help is appreciated.

Thanks!

Deb

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-09-23 : 13:24:33
[code]
DELETE t
FROM YourTable t
INNER JOIN (SELECT Col001, Col002,MIN(Plan_ID) AS MinID
FROM YourTable
GROUP BY Col001, Col002
)t1
ON t1.Col001 = t.Col001
AND t1.Col002 = t.Col002
AND t1.MinID = t.Plan_ID
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-09-23 : 13:35:34
Please post a quick data sample so that we can write your delete command.

You'll likely use GROUP BY with HAVING COUNT(*).

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -