Delete datetime from SQL database based on hour - python

I'm a python dev, I'm handling an SQL database through sqlite3 and I need to perform a certain SQL query to delete data.
I have tables which contain datetime objects as keys.
I want to keep only one row per hour (the last record for that specific time) and delete the rest.
I also need this to only happen on data older than 1 week.
Here's my attempt:
import sqlite3
c= db.cursor()
c.execute('''DELETE FROM TICKER_AAPL WHERE time < 2022-07-11 AND time NOT IN
( SELECT * FROM
(SELECT min(time) FROM TICKER_AAPL GROUP BY hour(time)) AS temp_tab);''')
Here's a screenshot of the table itself:

First change the format of your dates from yyyyMMdd ... to yyyy-MM-dd ..., because this is the only valid text date format for SQLite.
Then use the function strftime() in your query to get the hour of each value in the column time:
DELETE FROM TICKER_AAPL
WHERE time < date(CURRENT_DATE, '-7 day')
AND time NOT IN (SELECT MAX(time) FROM TICKER_AAPL GROUP BY strftime('%Y-%m-%d %H', time));

Related

How to query date values from sql using python where I only have the year and month

I have these records in my database:
and I want to select them using python, however I only have the year and month which is stored in a variable:
Here is my code:
However this does not work, and I've only gotten it to work when using a fully specific date, e.g. 2022-06-11.
The values of the column Date in the table are strings in the proper format yyyy-MM-dd and you can use the operator LIKE:
c.execute("SELECT Category, Amount FROM transactions WHERE Date LIKE ? || '%'", (month_selected,))
or, with the function strftime():
c.execute("SELECT Category, Amount FROM transactions WHERE strftime('%Y-%m', Date) = ?", (month_selected,))

Get deta datewise in hive/impala sql,

I have one column collection_date :- data type timestamp, having date time entry in yyyy-mm-dd hr:mm:ss but i am trying to fetch the record day wise, so I have to ignore hr:mm:ss, how can I apply in where apply only date in impala sql
You can truncate the time from timestamp using below function. And this is a timestamp too so you can compare with the date.
select trunc(now(),'DDD') a;
a
2021-09-02 00:00:00
SELECT FROM_TIMESTAMP(NOW(), 'yyyy/MM/dd');

Comparing dates from from two different sources

I am using Tweepy API and storing tweets in a sqlite3 database. I am able to get the date and time using a function in Tweepy. it stores the value in the database as
2015-06-06 23:06:19
I then need to determine if the age of this date is greater than 30 days if so it needs to delete the row in the database.
c.execute ("DELETE FROM storedTweets WHERE tweetDate < DATE() - 30")
This is what I have so far but the query isn't deleting entries.
I'm sure this is simple, but I'm new to this. Thanks in advance
DATE()-30 does not return a date:
sqlite> select DATE();
2015-06-06
sqlite> select DATE()-30;
1985
But using the date function,
sqlite> select date('now','-30 days');
2015-05-07
Therefore,
c.execute("DELETE FROM storedTweets WHERE tweetDate < date('now','-30 days')")
Alternatively, you could compute the date in Python:
import datetime as DT
date = DT.date.today()-DT.timedelta(days=30)
c.execute("DELETE FROM storedTweets WHERE tweetDate < ?", [date])

MYSQL Datetime, remove seconds

I've been attempting to do this for quite sometime.
I have a program which periodically writes rows to a table.
(table1)
ID Date Text Number
The Date column is in format yyyy-mm-dd hh:mm:ss ("2013-08-03 06:26:27")
The script which reads the data matches it to another set of data with the date in the same format except that the seconds are exactly 0.
"2013-08-03 06:26:00"
I need to change the Date data column in (Table 1) so that the seconds column is exactly zero. Currently it is just random values.
I have changed it on script level so that it writes the data to the MYSQL table so that the seconds is 0. However I have a lot of existing data which I can not loose which does not have the seconds at 0.
This is just a matter of updating the corresponding column.
Depending on ... hum ... your mood (?) you might try:
update tbl set datetime_column = substr(datetime_column, 1, 16);
Or
update tbl set datetime_column = date_format(datetime_column, '%Y-%m-%d %H:%i:00');
Or
update tbl set datetime_column = datetime_column - second(datetime_column);
MySQL? try this:
SELECT DATE_SUB(datetime_column,
INTERVAL EXTRACT(SECOND_MICROSECOND FROM datetime_column)
SECOND_MICROSECOND) as no_second_datetime
FROM table_name;

Python Count number of records within a given date range

We have a backend table that stores details of transaction including seconds since epoch. I am creating a UI where I collect from-to dates to display counts of transaction occurred in-between the dates.
Assuming that the date range is from 07/01/2012 - 07/30/2012, I am unable to establish a logic that will increment a counter for records that happened within the time period. I should hit the DB only once as hitting for each day will give poor performance.
I am stuck at a logic:
Convert 07/01/2012 & 07/30/2012 to seconds since epoch.
Get the records for start date - end date [as converted to seconds since epoch]
For each record get the month / date
-- now how will we add counters for each date in between 07/01/2012 - 07/30/2012
MySQL has the function FROM_UNIXTIME which will convert your seconds since epoch into datetime and you can then extract the DATE part of it (YYYY-MM-DD format) and group according to it.
SELECT DATE(FROM_UNIXTIME(timestamp_column)), COUNT(*)
FROM table_name
GROUP BY DATE(FROM_UNIXTIME(timestamp_column))
This will return something like
2012-07-01 2
2012-07-03 4
…
(no entries for days without transactions)

Categories