Hello, I would like to build a table capturing data starting from 1st day of current year till next 3 years. Once the table reaches 3 full years, first day of data should fall off as the next day data adds to the table after 3 years. Which means after completion of 3 years, any given day table should only hold exact previous 3 years of data. I need help coming up with this logic. Thank you in advance!
March 31, 2025 at 6:17 pm
By fall off, do you mean get deleted?
Why not keep all the data and simply have a view which select the last three years?
Otherwise, create a daily SQL Agent job to perform the trimming.
March 31, 2025 at 6:28 pm
Thank you Phil for responding on this.
Yes, delete the oldest rows. I am creating a stored procedure which creates this table and a job that runs everyday for daily inserts.
Don't want to pull everything into the table as the data will be in millions after 3 years.
You can add to your SP after records are created or execute another SP that deletes the records over 3 years old. You need to have a date for when the records are loaded, e.g. "LoadDate"
-- Delete records older than three years from today.
DELETE from YourTableName
WHERE LoadDate< DATEADD(YEAR, -3, GETDATE());
March 31, 2025 at 7:51 pm
You can add to your SP after records are created or execute another SP that deletes the records over 3 years old. You need to have a date for when the records are loaded, e.g. "LoadDate"
-- Delete records older than three years from today.
DELETE from YourTableName WHERE LoadDate< DATEADD(YEAR, -3, GETDATE());
If you decide to use this code, please make sure that you are happy with how it handles leap years.
DECLARE @Day1 DATE = '20240228'
,@Day2 DATE = '20240229';
SELECT D28Feb = @Day1
,D28FebLess3 = DATEADD (YEAR, -3, @Day1)
,Day29Feb = @Day2
,D29FebLess3 = DATEADD (YEAR, -3, @Day2);
March 31, 2025 at 8:01 pm
Good point Phil. I do believe DATEADD(year, -3, GETDATE()) Takes leap year into account, I'm too lazy to load test data. also I would not delete data from a table I would move the over 3 year data to an Archive Table just in case.
April 1, 2025 at 7:11 am
Also keep lock escalation in mind, so you don't lock the full table during these deletes ! (and e.g. prevent inserts being able to be processed )
a while loop handling "delete top(n) .." will help out if needed
I hope you have an index that helps with this process
Johan
Learn to play, play to learn !
Dont drive faster than your guardian angel can fly ...
but keeping both feet on the ground wont get you anywhere :w00t:
- How to post Performance Problems
- How to post data/code to get the best help[/url]
- How to prevent a sore throat after hours of presenting ppt
press F1 for solution, press shift+F1 for urgent solution
Need a bit of Powershell? How about this
Who am I ? Sometimes this is me but most of the time this is me
April 1, 2025 at 7:27 am
Good point Phil. I do believe DATEADD(year, -3, GETDATE()) Takes leap year into account, I'm too lazy to load test data. also I would not delete data from a table I would move the over 3 year data to an Archive Table just in case.
You don't need test data, just run the T-SQL example I provided and you will see what happens with leap years (nothing gets archived on 29 Feb, because data on 28 Feb three years before has already been removed).
And also, consider what happens three years after a leap year.
On 2027-02-28, data for 2024-02-28 gets archived.
On 2027-03-01, data for 2024-03-01 gets archived.
Data for 2024-02-29 is left in place until 2027-03-01.
April 1, 2025 at 9:36 am
Your are correct regarding "Data for 2024-02-29 is left in place until 2027-03-01."
April 7, 2025 at 6:47 pm
This sounds like a good place for monthly partitions. The monthly switch out takes nearly zero time or resources and then the switched out part can just be dropped as a single unit.
--Jeff Moden
Change is inevitable... Change for the better is not.
April 8, 2025 at 6:07 am
This sounds like a good place for monthly partitions. The monthly switch out takes nearly zero time or resources and then the switched out part can just be dropped as a single unit.
And got so simple over the different sql server versions.
Nowadays it starts with truncate table at partition level.
Johan
Learn to play, play to learn !
Dont drive faster than your guardian angel can fly ...
but keeping both feet on the ground wont get you anywhere :w00t:
- How to post Performance Problems
- How to post data/code to get the best help[/url]
- How to prevent a sore throat after hours of presenting ppt
press F1 for solution, press shift+F1 for urgent solution
Need a bit of Powershell? How about this
Who am I ? Sometimes this is me but most of the time this is me
Viewing 12 posts - 1 through 12 (of 12 total)
You must be logged in to reply to this topic. Login to reply
This website stores cookies on your computer.
These cookies are used to improve your website experience and provide more personalized services to you, both on this website and through other media.
To find out more about the cookies we use, see our Privacy Policy