开发者

Deleting at most one record for each unique tuple combination

I want to delete at most one record for each unique (columnA, columnB)-tuple in my following delete statement:

DELETE FROM tableA
WHERE columnA IN 
    (
    --some subqueryA
    )
AND columnB IN
    (
    --some subqueryB
    )

How is this accomplished? Please only consider those statements that work when used against MSS 2000 (i.e., T-SQL 2000 syntax). I can do it with iterating through a temptable but I want to write it using only sets.

Example:

subquer开发者_如何学JAVAyA returns 1
subqueryB returns 2,3

If the original table contained (columnA, columnB, columnC)

5,2,5
1,2,34
1,2,45
1,3,86

Then

1,2,34
1,3,86 

should be deleted. Each unique (columnA, columnB)-tuple will appear at most twice in tableA and each time I run my SQL statement I want to delete at most one of these unique combinations - never two.

If there is one record for a given unique (columnA, columnB)-tuple, delete it.

If there are two records for a given unique (columnA, columnB)-tuple, delete only one of them.

Delete tabA
from  TableA tabA 
Where tabA.columnC in (
      select  max(tabAA.columnC)  from TableA tabAA
      where tabAA.columnA in (1)
      and tabAA.columnB in (2,3)
      group by tabAA.columnA,tabAA.columnB
)


How often are you going to be running this that it matters whether you use temp tables or not? Maybe you should consider adding constraints to the table so you only have to do this once...

That said, in all honesty, the best way to do this for SQL Server 2000 is probably to use the #temp table as you're already doing. If you were trying to delete all but one of each dupe, then you could do something like:

  • insert the distinct rows into a separate table
  • delete all the rows from the old table
  • move the distinct rows back into the original table

I've also done things like copy the distinct rows into a new table, drop the old table, and rename the new table.

But this doesn't sound like the goal. Can you show the code you're currently using with the #temp table? I'm trying to envision how you're identifying the rows to keep, and maybe seeing your existing code will trigger something.

EDIT - now with better understood requirements, I can propose the following query. Please test it on a copy of the table first!

DELETE a 
FROM dbo.TableA AS a
INNER JOIN 
(
   SELECT columnA, columnB, columnC = MIN(columnC) 
      FROM dbo.TableA
      WHERE columnA IN
      (
        -- some subqueryA
        SELECT 1
      )
      AND columnB IN 
      (
        -- some subqueryB
        SELECT 2 UNION SELECT 3
      )
      GROUP BY columnA, columnB
) AS x
ON  a.columnA = x.columnA
AND a.columnB = x.columnB
AND a.columnC = x.columnC;

Note that this doesn't confirm that there are exactly one or two rows that match the grouping on columnA and columnB. Also note that if you run this twice it will delete the remaining row that still matches the subquery!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜