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 6879137
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:48:33+00:00 2026-05-27T04:48:33+00:00

I am trying to write some SQL to automatically delete some records from the

  • 0

I am trying to write some SQL to automatically delete some records from the database but I’m having some trouble with the logic and want to see if someone can help me with this query.

Basically I have a table called image. In image there is a column for a main ID, a secondary ID, a type, a year, and a date.

A main ID may have multiple secondary IDs so that means multiple rows of the same main ID with different secondary IDs. Each secondary ID will for sure have two rows, a type of small and a type of large. Some of the secondary IDs have duplicate data so there may be 6 rows (picked an arbitrary number), which means one of each type small and large repeated 3 times for a single secondary ID for a single main ID. All of these can be for a single year and then repeated for the next year.

That is hard to grasp so let me create a visual example of some data. I’m not good at formatting so the data row order will be like I typed it above, main ID, secondary ID, type, a year, and date.

EX:

1000 3000 Small 2010 2010-11-28
1000 3000 Large 2010 2010-11-28
1000 3000 Small 2010 2010-11-29
1000 3000 Large 2010 2010-11-29
1000 3000 Small 2011 2010-11-30
1000 3000 Large 2011 2010-11-30

1000 3001 Small 2010 2010-11-28
1000 3001 Large 2010 2010-11-28
1000 3001 Small 2010 2010-11-28
1000 3001 Large 2010 2010-11-28
1000 3001 Small 2011 2010-11-28
1000 3001 Large 2011 2010-11-28

You can see that there may be duplicate data for a single secondary ID with the same date and year. There is also data that could be duplicate based on the same secondary ID and year even though the date is a day off or just different.

Basically what I need is a query that will go through a table filled with data like this and delete the bad records that are not needed.

I want only two records, a small and large, for each secondary ID, for each year (not date), for each main ID in this image table, keeping the newest by date.

So for example I would expect these records to be left after running this script:

1000 3000 Small 2010 2010-11-29
1000 3000 Large 2010 2010-11-29
1000 3000 Small 2011 2010-11-30
1000 3000 Large 2011 2010-11-30

1000 3001 Small 2010 2010-11-28
1000 3001 Large 2010 2010-11-28
1000 3001 Small 2011 2010-11-28
1000 3001 Large 2011 2010-11-28

Again this is only an example for a single main ID and a couple of secondary IDs, there could be x number of main IDs in this table.

How can I go about writing a query that will delete the extra rows in this image table as defined by my example?

  • 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-27T04:48:33+00:00Added an answer on May 27, 2026 at 4:48 am
    WITH cteRowNums AS (
        SELECT MainId, SecondaryId, Type, Year, DateColumn,
               ROW_NUMBER() OVER(PARTITION BY MainId, SecondaryId, Type, Year ORDER BY DateColumn DESC) AS RowNum
            FROM image
    )
    DELETE FROM cteRowNums
        WHERE RowNum <> 1;
    
    • 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 some SQL that will delete files of type '.7z' that
I am trying to write some SQL that pulls from several tables using unions,
I am trying to write a sql query but i find some difficulties.What i
I'm having a bit of headache with some SQL I'm trying to write. Basically,
I am trying to write some integration tests for some SQL server stored procedures
I'm trying to write some LINQ To SQL code that would generate SQL like
I'm trying to write some code using pure SQL using ASP.NET MVC. I assume
I'm trying to write some C# code that calls a method from an unmanaged
I'm struggling with some SQL/php syntax issues here. I'm trying to write two functions.
I am trying to write a data migration pl/sql script to transfer some of

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.