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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:45:13+00:00 2026-06-13T07:45:13+00:00

I have a table that looks like the following where what should be a

  • 0

I have a table that looks like the following enter image description here

where what should be a unique identifier,drg, is not, so I started on the personal challenge of fixing it. I could do this easily a myriad of ways but I’m trying to do some things in a different fashion to learn some new techniques. What needs to be deleted from the table is any row whose drg is not unique and whose eff_date is **less than ** the max eff_date.

To find what I needed I used

select d1.drg, MAX(d1.eff_date) as maxEffDate,d2.drgdesc
    from drgtable d1
    inner join drgtable d2 on d1.drg=d2.drg 
    group by d1.drg,d2.drgdesc
    having MAX(d1.eff_date) = MAX(d2.eff_date)

Instead of just selecting this into a new table and dropping the old one, I’d like to do it in a delete statement.

My though process on the strange looking query below was to find the rows that weren’t in the table from the above query I used and delete them. I got close, but am stuck. Also, if there’s a way easier way to do this, please let me know. Thanks

–the purpose of this was to find the rows in d3 that were not in d4 and delete them. didn’t quite get that far

delete from 
drgtable
where 
(
select * from drgtable as d3
left join 
(
select d1.drg, MAX(d1.eff_date) as maxEffDate,d2.drgdesc
    from drgtable d1
    inner join drgtable d2 on d1.drg=d2.drg 
    group by d1.drg,d2.drgdesc
    having MAX(d1.eff_date) = MAX(d2.eff_date)

) d4 on d4.drg =d3.drg and d4.maxEffDate = d3.eff_date and d3.drgdesc = d4.drgdesc
) 
  • 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-06-13T07:45:14+00:00Added an answer on June 13, 2026 at 7:45 am

    This should work:

    ;WITH cte As
    (
        SELECT  *,
                MAX(eff_date) OVER(partition by drg) as MaxEffDate
        FROM    drgtable
    )
    DELETE 
    FROM    cte
    WHERE   eff_date <> MaxEffDate
    

    Plus, plenty of new tricks in there for you to learn. 🙂

    (note: this does assume that you are on SQL Server 2005 or higher)

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

Sidebar

Related Questions

I have a table that looks like the following: <table class=theClass> <tr> <td class=anotherClass><strong>Label1:</strong></td>
I have a table in MySQL That looks like the following: date |storenum |views
I have a scenario outline table that looks like the following: Scenario Outline: Verify
I have some data in a table that looks roughly like the following: table
Greetings, I have a table that looks like the following: date value 2007-11-05 134
I have data in a table that looks like the following sample data: varchar(20)
I have a basic table that looks like the following: SQL> desc comments Name
I currently have a table that looks like the following: [BUSINESS_PROCESS_ID] [Error Type 1]
I have a table that looks like this: id value AGA 0.211 AGA 0.433
I have a table that looks like this: CREATE TABLE foobar ( id SERIAL

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.