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

  • Home
  • SEARCH
  • 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 6109813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:26:46+00:00 2026-05-23T14:26:46+00:00

I am trying to write a query to removed duplicates records from the following

  • 0

I am trying to write a query to removed duplicates records from the following table (valid_columns) and keep only the records with the lowest possible [order] number.

For example in the following table I would like to remove duplicate rows, region 2,3 and job 3 and keep the records with the lowest possible [order].

E.g. The input table, valid_columns looks like this:

name    col_order
-------------
job     1   
job     3   
status  2   
cust    2   
county  1   
state   1   
region  1
region  2
region  3
so      4

Desired Output:

name    col_order
-------------
job     1       
status  2   
cust    2   
county  1   
state   1   
region  1
so      4

I am trying to fix a bug and I can’t figure out the SQL. Currently it uses a delete statment and a subquery. The query used at the moment looks like this:

— 3) Remove duplicated columns

DELETE 
FROM valid_columns
WHERE   NOT ( col_order = ( SELECT  TOP 1 col_order 
            FROM    valid_columns   firstValid
            WHERE   name = firstValid.name
            AND col_order = firstValid.col_order
            ORDER BY col_order ASC ))

However, this only returns the following, which is incorrect:

name    col_order
-------------
job     1
county  1
state   1
region  1

Many thanks

  • 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-23T14:26:47+00:00Added an answer on May 23, 2026 at 2:26 pm
    DELETE FROM t1
    FROM valid_columns t1
    WHERE col_order > 
        (SELECT MIN(col_order) from valid_columns t2 WHERE t1.name = t2.name)
    

    EDIT:
    can be simplified to this:

       DELETE FROM valid_columns 
       WHERE col_order > 
            (SELECT MIN(col_order) from valid_columns t2 WHERE valid_columns.name = t2.name)
    

    The DELETE statement can have a FROM clause to delete a record based on the value of a related record in a second table. In this case the FROM is not really required (I sometimes use the FROM to alias the table name because I don’t like the extra typing.)

    DELETE FROM TableA
    FROM TableA 
    JOIN TableB On TableA.CriteriaA = TableB.CriteriaA
    

    You could also try this example (might be faster if you have to do this a lot):

    DELETE FROM valid_columns 
    WHERE EXISTS
        (SELECT * FROM valid_columns t1 
         WHERE t1.name = valid_columns.name AND valid_columns.col_order > t1.col_order);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a query that extracts and transforms data from a table
I'm trying write a query to find records which don't have a matching record
I'm trying to write a query that updates rows in a table if a
I am trying to write a query that connects 3 tables. The first table
Im trying to write query in linq Select UserId, UserNumber FROM User where UserNumber
I am trying to write a query to create a 'table' of data as
I am trying to write a query that looks through all combo_items and only
I'm trying to write a query along these lines: UPDATE Table i2 SET value
I have trying to write a query in Access 2010. I have a table:
I'm trying to write a query in php that will select a row from

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.