I'm still fairly new to SSMS, but I'm trying to implement a weekly and monthly leaderboard for AI characters, which will be updated daily based on the average steps value in the database, with a variance of 0.5x - 1.5x that value.
I also want to reset the weekly and monthly values every week and month, respectively (not worried about storing old scores for now).
How would I do this in a database?
here is my table:
Trainer Table Id, Name, AverageSteps, WeeklySteps, MonthySteps
I'm fine on the math part of getting the values, but
- How can I run an event/script to update the weekly and monthly steps with the average steps value (this would be done once a day)?
- How can I run an event/script to reset the weekly steps each week, and monthly steps each month?
I know what I want to do is possible - I'm just not sure how - does anyone know any good tutorials on this subject?
I'd create a stored procedure to perform the updates. Then I'd schedule the stored proc to run daily using a SQL Server Agent job.
In your stored proc, you'd have three sections. The first section of code would be run every time the stored proc ran, and it would update the
WeeklySteps
andMonthlySteps
values with theAverageSteps
value.The next section would be contained in an
IF
block, and would reset theWeeklySteps
value. YourIF
condition would be something like:The last section would also be contained in an
IF
block, and would reset theMonthlySteps
value. YourIF
condition would be something like: