ActiveRecord finding only hourly records - mysql

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.

Related

Is there a way to know the number of records added into the SQL database after a particular date and time

The table doesn't have any date time column. I want to if there is any inbuilt keyword which can does that.
I want to know all commits done after a particular date.
If flashback is enabled on the database you can get records on the table in an around a particular date range in Oracle.(It purely depends on if its enabled and for how long the flashback needs to be kept)
You can query to see the data in the table as of 3 days back as follows
select *
from table as of timestamp sysdate-3

Reducing the amount of results from a Django query by selecting every Nth row

I am trying to reduce the amount of events I get from a query on a table in mysql which has a lot of events stored in it. There is roughly one event each minute, each event has a datetime and then some other sensor readings. I would like to reduce the amount of data so that I'm only getting one reading every hour or so.
I realise I can do something like:
IncomingData.objects.filter(utctime__range=('2016-10-07', '2016-10-14'))[::60]
This will give me 1 event an hour (assuming they are ordered by time?) but this is still returning 60 events per hour from the database.
Potentially I might want to read a bigger date range and less events - for instance one event a day over an entire year, and this method wouldn't work because it would be reading millions of unnecessary rows.
I have seen some solutions using ROWNUM but I want to keep away from raw sql if possible (e.g. https://dba.stackexchange.com/a/56389)
I have also tried the following which I would have thought would return the first event each hour but it returns an empty queryset:
IncomingHcData.objects.filter(utctime__minute=0)
It outputs the following SQL as the generated query:
SELECT
"incoming_hc_data"."uid",
"incoming_hc_data"."utctime",
"incoming_hc_data"."temp_1",
"incoming_hc_data"."temp_2",
"incoming_hc_data"."temp_3",
FROM "incoming_hc_data"
WHERE django_datetime_extract('minute', "incoming_hc_data"."utctime", GMT) = 0
Use the extra function:
IncomingData.objects.extra(where=['minute(utctime)=0'])

sql query count daily difference / count new rows, no date column

I have a database for our local real estate listings, there are no dates or timestamp columns.
I would like to be able to get out just the rows that were added in the past day or two.
Can anyone point me in the right direction to get this data out?
EDIT:
Each new row does get a new id number which is incrementally higher, so I can ORDER the results by newest.
Would it be possible to save my query count in a file, or in another database, each day, then calculate the difference and use that as my number of new listings?
"Would it be possible to save my query count in a file, or in another database, each day, then calculate the difference and use that as my number of new listings?"
I understand that you can't change the table structure to add a date...so instead, I suggest to have a cron job at midnight that will create a record with a date and the higher ID at this moment. This way, you will be able to finde a range of ID for a specific date...
If you relayed on query count, you will get problem when you will start to delete some rows...

php mysql storing just hours minutes seconds

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

Increment a value for entire database

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.