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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:51:49+00:00 2026-05-29T10:51:49+00:00

My question is similar to sql statement to delete records older than XXX as

  • 0

My question is similar to sql statement to delete records older than XXX as long as there are more than YY rows, but that question just deals with a single parent, I want to delete records for all parents in one go.

Consider this table:

CREATE TABLE Children
(
    ChildId int NOT NULL,
    ChildCreated datetime NOT NULL,
    ParentId int NOT NULL
) 

This could be any parent-child relationship, so the names are generic.

I would like to delete all children that are older than a month, but need to keep a minimum number of children for each parent regardless of their age.

I tried some statements with nested SELECTs and GROUP BYs which gave me some results but none gave me the correct result set.

Because I am using SQL Server I came up with the following solution that works great:

WITH CTE AS
(
  SELECT ROW_NUMBER() OVER (Partition BY ParentId ORDER BY ChildCreated DESC) 
  As RowNo, ChildCreated FROM Children
)

DELETE FROM CTE WHERE RowNo > 10
AND RevisionCreated < DATEADD(MONTH,-1,GetDate())

The common table expression groups all children for each parent together and adds a continuous row number based on the creation order. The newest child for each parent has
a row number of 1, the tenth newest has 10. So I can just delete all records with a row
number greater 10 as long as they are also over a month old.

My question is, what if I have to do the same thing on a system where CTEs are not supported. What is the ANSI SQL-92 solution for this problem?

  • 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-29T10:51:49+00:00Added an answer on May 29, 2026 at 10:51 am

    Based on other responses, and the relative simplicity of my query I think I might be over simplifying the issue, but I am assuming since parentID is not nullable that it does not reference childID, In which case it can be achieved as simply as the below

    DELETE  Children
    FROM    Children a
    WHERE   ChildCreated < DATEADD(MONTH, -1, GETDATE())
    AND     (   SELECT  COUNT(*)    -- NUMBER OF NEWER CHILDREN WITH THE SAME PARENT
                FROM    Children b
                WHERE   a.ParentID = b.ParentID
                AND (   a.ChildCreated < b.ChildCreated
                    OR  (a.ChildCreated = b.ChildCreated AND a.ChildID > b.ChildID)
                    )
            ) >= 10
    

    Although this exact SQL may need tweaking depending on RDBMS, I don’t know of any RDBMS where this principal cannot be applied.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There is a similar question that is unanswered on stackoverflow, but I believe I
My question is similar to this MySQL question, but intended for SQL Server: Is
I asked a similar question yesterday that was answered correctly. SQL requiring self join
I'd like to know if it's possible to execute more than one SQL statement
My question is similar to this SQL order of operations but with a little
Similar to this question only the other way of flow. Insert Picture into SQL
This is a question similar to a question here . I wonder is there
I have asked a question similar to this in the past but this is
This is probably going to be more of a discussion or hypothetical question, but
Follow up to this question This (similar version from old link) works in SQL

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.