Solved

How do I delete specific older rows?

Posted on 2008-06-10
5
231 Views
Last Modified: 2010-04-21
Hi,

I have 2 tables.

1. current_rate
2. past_rates

my past rate table is really big and its slow when I query it so I want to clean it up ONCE a month by backing up useless/older rows from past rate to a 3rd table that I will call backuprates.

I created the backuprates by

select *
into backuprates
from past_rate

I have carrier, country,rates, effective_dates as the important columns in all 3 tables

you can have multiple countries for the same carrier and multiple effective_dates for the same carrier

I want to have only the 2 most recent effective_dates per country in the past_rates table.


example:

select carrier, country, rate,  effective_date
from past_rate
group by carrier, country, rate, effective_date
order by carrier, country, effective_date desc

gives me:

carrier        country rate  effective_date
asiatelecom france 0.51   2008-06-8
asiatelecom france 0.62   2008-05-12
asiatelecom france 0.72   2007-02-4
asiatelecom france 0.81   2005-03-24
asiatelecom egypt  1.5    2008-06-8
asiatelecom egypt  1.62  2008-01-22
.
.(lots of rows here)
.
.
zambiatele  india      0.4  2008-05-4
zambiatele  india      0.8  2007-06-2
zambiatele  italy      0.6  2005-03-2

I want to keep the 2 most recent effective dates for all carriers and countries . .

If you could Please Help give a shot at my query, that will be fantastic!

If something is not clear , Please ask!

THANK YOU.
0
Comment
Question by:Bigbears
  • 2
  • 2
5 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 21756761
something like this:
--- first the select to see what will be deleted:
SELECT t.*
  FROM past_rate t
 WHERE t.effective_date NOT IN ( SELECT TOP 2 i.effective_date FROM past_rate i WHERE i.country = t.country and i.carrier = t.carrier ) 
--- if the select seems fine, run the delete: 
DELETE t
  FROM past_rate t
 WHERE t.effective_date NOT IN ( SELECT TOP 2 i.effective_date FROM past_rate i WHERE i.country = t.country and i.carrier = t.carrier )

Open in new window

0
 

Author Comment

by:Bigbears
ID: 21756990
Hi Angellll,

Thanks for looking into this. The code you provided is almost right. the SELECT TOP 2 i.effective_date ..
selects the  highest ones for that subquery instead of the the most recent ones.

If we were allowed  to put 'order by effective_date desc' in that subquery, it would work..
unless im missing something?
0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 125 total points
ID: 21757026
indeed:

--- first the select to see what will be deleted:
SELECT t.*
  FROM past_rate t
 WHERE t.effective_date NOT IN ( SELECT TOP 2 i.effective_date FROM past_rate i WHERE i.country = t.country and i.carrier = t.carrier ORDER BY i.effective_date DESC ) 
--- if the select seems fine, run the delete: 
DELETE t
  FROM past_rate t
 WHERE t.effective_date NOT IN ( SELECT TOP 2 i.effective_date FROM past_rate i WHERE i.country = t.country and i.carrier = t.carrier ORDER BY i.effective_date DESC  )

Open in new window

0
 

Author Closing Comment

by:Bigbears
ID: 31465979
Thanks again!
0
 
LVL 4

Expert Comment

by:roshkm
ID: 21757929
A small correction to what Angellll has specified:

DELETE t
  FROM past_rate t
 WHERE t.effective_date NOT IN ( SELECT TOP 2 i.effective_date FROM past_rate i WHERE i.country = t.country and i.carrier = t.carrier order by  i.effective_date desc)

Order by in inner query is required since author requires latest 2 records by time.

0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to enforce inte 8 51
Testing connection to sql 7 59
SQl Agent job fails--SSIS package looses password 6 46
Email Notifications for SQL 2005 9 28
by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
I am showing a way to read/import the excel data in table using SQL server 2005... Suppose there is an Excel file "Book1" at location "C:\temp" with column "First Name" and "Last Name". Now to import this Excel data into the table, we will use…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question