There have been similar(ish) questions, but this is a bit more specific...
I have 10 different sensors. I want to only keep the most recent 5 readings for each sensor. The problem I am having is that when it reaches 5 readings, it will delete all the readings for that particular sensor and then start filling up again, until it reaches 5 again... and so on. On reaching 5 readings, I want it to just "replace" the oldest with the newest. Been at this a while and can't fathom it out. I would prefer a reasonably easy-to-understand method as this is to help some kids who are creating a school project. Thanks.
Here is the basic code so far...
Python/SQL codeblock to delete the oldest entry for the given sensor and insert a new one (if greater than 5 readings)
cursor = db.cursor()
cursor.execute("DELETE FROM readings WHERE Sensor IN (SELECT Sensor FROM readings WHERE Sensor = '%s' GROUP BY Sensor HAVING COUNT(Sensor)>5 and MIN(Timestamp))" % sensorName)
db.commit()
PS: I can see why it isn't working - it's because the SELECT statement is only going to select anything when the count is greater than 5, then it will delete all of the readings for that sensor, but I can't work out how to get it to only select the oldest and then delete that.
Aucun commentaire:
Enregistrer un commentaire