Skip to content
Advertisement

How to remove rest of the ordered rows with the same ID after the first row which occurs more than once with that ID?

I have the following structure for the table DataTable: every column is of the datatype int, RowID is an identity column and the primary key. LinkID is a foreign key and links to rows of an other table.

I’m trying to do a query which alters every LinkID batch in the following way:

  • Take every row with same LinkID (e.g. the first batch is the first 6 rows here)
  • Order them by the Order column
  • Look at Data and DataSpecifier columns as one compare unit (They can be thought as one column, called dataunit):
  • Keep as many rows from Order=1 onwards, until a dataunit comes by which appears more than one time in the batch
  • Keep that final row, but delete rest of the rows with same LinkID and greater Order value

So for the LinkID 120:

  • Sort the batch by the Order column (already sorted here, but should still do it)
  • Start looking from the top (So Order=1 here), go as long as you don’t see a value which appears more than 1 time in the batch
  • Stop at the first duplicate Order=3 (dataunit 1 10 is also on Order 5).
  • Delete everything which has the LinkID=120 AND Order>=4

After similar process for LinkID 371 (and every other LinkID in the table), the processed table will look like this:

I’ve never done an SQL query which this complicated. I know the query has to be something like this:

but I just can’t seem to wrap my head around this and get the query right. I would preferably do this in pure SQL, with one executable (and reusable) query.

 


I asked a very similar question here: How to remove rest of the rows with the same ID starting from the first duplicate?

But since I realized that my original filtering logic in the question was not actually what I needed and that question had already been answered correctly, I had to make this new question.

Advertisement

Answer

Here, my previous solution updated. A few GROUP BY should be enough. Code is simple enough to be understood by reading.

Set up:

Solution:

Result:

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement