So we have a database. Every so often we'll add a new data set and the oldest dataset should be removed.
Say the DB is Early June: 18 records, Late June: 15 records, Aug: 23 records. When we add the next records we want to remove the Early June records. However adding new data isn't totally regular.
My second thought is to have an "oldness" field for each record and before adding a new data set, increment all of the "oldness" fields by 1, then removing all the data that has "oldness" of 3.
I'm worried that this is not the optimal way to do this, but I'm not sure it matters since it's a fairly small database that is updated infrequently. That said, I'm certainly open to suggestions for a better algorithm to accomplish this.
I'm assuming your data is stored such that it has a timestamp (date) column for each report, and that you always want to remove data that is x (in this case, 3) months old. If so, you might think about using mysql's DATEDIFF function.
For example, it might look something like this:
DELETE from table1 WHERE datediff(CURRENT_DATE(), datecol) > 89
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_datediff
Is it always by month?
UPDATE table SET status = "oldness" WHERE date_inserted <= NOW() - INTERVAL 3 MONTH
With a little help from user937146 I think I've got the solution. Basically
$query = "SELECT MIN(timestamp) FROM new_items WHERE type = 'Book' ";
Then it's just a matter of taking that timestamp, adding an hour (since the data can take more than a second to upload, probably one minute would be sufficient) and then deleting everything from the database older than that timestamp.
Related
I have a table that I use for statistical purposes.
Its columns are id and 1,2,3,..,31 and pivot.
This table gives the number of views on each day for the last 31 days.
1 gives the number of views for yesterday.
14 gives the number of views for 14 days ago.
etc ...
(pivot is just used to calculate the number of views)
I would use a cron job every day to update this table, but how would I go about "shifting" all the values to the side ( value column 15 would become value column 16; new value for column 1; delete value for column 31)
Define a table with only two columns — "date" and "views"
INSERT a new row in the table with the view count for that day when the CRON job runs
Modify your application query to read through this new table over a custom date range, which could be 31 days or anything else either — please have a look at this link to get an idea:
MySQL Query - Records between Today and Last 30 Days
Not really sure how pivot is being used here. However, I'm almost certain that if you're using it to store the sum of the views, it could as well be computed by using SUM() or GROUP BY without having to need a separate column in the table
As far as data archival / removal is concerned, your daily CRON job could be modified to include a DELETE query (as the last step) which cleans up records older than a certain date. Again, you could use the link above to get your "target" date
.
I apologise that this might sound like a little too long a solution to what you've asked for. However, I feel, this approach should help you organise and maintain the table in question in a better way.
Thank you all in advance for any help. I'm Still very new to access and have no idea where to start to find a solution.
What I am trying to do is to auto populate a field in my table called "Period". I would like to have it use the "Activity_Date" to look into a different table that has date ranges that reference to the correct period. Based on which "Period" the "Activity_Date" falls under will return the correct "Period". I've tried using calculated data type and queries and I feel no closer to an answer than when I started.
Thanks again for your time.
I would question why you NEED to populate the field period in your table.
In short, I wouldn't bother.
The period it is in can be derrived from the activity date field that is in the same record.
So you can write select statements that calc the period for the record in your MyTable as required.
SELECT TableWithPeriods.period, MyTable.activity_date
FROM MyTable
LEFT JOIN TableWithPeriods
ON MyTable.activity_date
BETWEEN TableWithPeriods.StartDate
AND TableWithPeriods.EndDate
If you need to access the period a lot then there is an argument for keeping a period value in the MyTable in step with the TableWithPeriods.
Keeping in step could be akward though as what if someone changes one of the period 's dates?
Keeping in step might mean writing a bit of SQL to update ALL MyTable rows that wither do not have the period set or when the period is now different.
A VBA update statement will look a bit like the SELECT above.
Or
you could use database the onchange macros that respond to data being added or updated in the MyTable (and the TableWithPeriods, if users can change dates).
Anyway, there's my opinion. I would NOT copy the value over.
PS I'm not 100% sure about the SQl I gave above, this might work though
SELECT TableWithPeriods.period, MyTable.activity_date
FROM MyTable
LEFT JOIN TableWithPeriods
ON ( MyTable.activity_date >= TableWithPeriods.StartDate
AND MyTable.activity_date <= TableWithPeriods.EndDate )
I know a typical timestamp in any format readable or otherwise is always equivalent to a date time second day month year etc. However I want to be able to search by hours minutes seconds where the day month year are irrelevant. So I am trying to wrap my head around that ability and what would be the best method of storing time so I can create searches around that alone without m-d-y getting in my way.
Try using the TIME field type. The TIMESTAMP field type should only be used anyway when you want MySQL to update the field when updating the row.
$hour = date("H",$date); $minute = date("i",$date); $second = date("s",$date);
and save them on your table as hour,minute and second
I've got a table with a datetime column that has records for every minute (logging data every minute)
I want to get only the records that occurred at the top of the hour(1pm, 2pm, 3pm, 4pm, etc).
What would be the best way to get this?
You could use extract to find the values which have zero for the minutes:
top_of_the_hours = Model.where('extract(minute from your_datetime) = 0')
That will also work with PostgreSQL but you'd need to switch to strftime for SQLite.
I have this mySQL syntax which updates the table users and column skills_mod_time every time the page is reloaded. $sql = "UPDATE users SET skills_mod_time = CURRENT_TIMESTAMP";
How can i change from instead of using the CURRENT_TIMESTAMP to using a counting timestamp.
For example the column skills_mod_time is defaulted as 0. If the page was reloaded 10 sec later, i want the seconds part to be 10. Instead of the current way where it changes 0 to this present time.
It sounds like you want to log "seconds since login", or something similar, rather than "last activity." To do this, you should create a column called login_time, or similar, and only update it when a user logs in. You probably still want to track the last-activity time, as you do now, for the purpose of session expiry.
Then if you have these two columns, login_time, and last_activity, you can get the seconds between login_time and last_activity by subtracting the one from the other. Or you can get the time since login by subtracting login_time from NOW().
I hope this answers your question--if not, please clarify your question.
you can use timestampdiff function to take the difference of two timestamp values
something like
UPDATE users SET skills_mod_time = TIMESTAMPDIFF(MINUTE,skills_mod_time,CURRENT_TIMESTAMP);