Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6368341
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:39:45+00:00 2026-05-25T00:39:45+00:00

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

  • 0

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:

subqueryA 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
)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T00:39:46+00:00Added an answer on May 25, 2026 at 12:39 am

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to delete from many files each instance of a paragraph . I
I want to delete the most recent entry in MySQL using PHP, is there
I want to delete a range of data from a text file using PHP.
I want to delete part of a record. For example, a column called message
I want to delete a table from a sqlite3 db at certain times, i.e.
I'm working in PHP to allow users to delete content and I want delete
I want to delete a directory in the repository via command line. At the
I want to delete a document in lucene 2.4 with java. My code is
I want to delete a row in the grid, but the grid always post's
I want to delete everything before the last tab (or comma). For example, in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.