Suppose I have a datetime column.
"SELECT * FROM mytable WHERE thetime < INTERVAL 1 HOUR"
How do you write this in Django?
MyModel.objects.extra(where=['thetime < INTERVAL 1 HOUR'])
Related
Getting error for below how to put date in variable for sql query?
for symbol in symbolist:
datereplace1 = '2022-04-03 00:00:00'
datereplace2 = '2022-04-03 00:00:00'
query=(f"""DELETE FROM {symbol} WHERE Time >=%s AND Time <%s""", ) #we can also delete older than x days by writing thisWHERE col <= date('now', '-10 day')
cursor.execute(query, (datereplace1,datereplace2))
test.commit()
cursor.execute(query, (datereplace1,datereplace2))
TypeError: argument 1 must be str, not tuple
Hay Everyone
I am trying to translate my SQL server query into pandasql
so the task is to get the average time (in minutes) between 2 process
this is my query on the SQL server
select payment_method,cast(avg(cast(cast(DATEDIFF(second,booking_created_time,booking_paid_time) as float)/60 as float)) as decimal(20,2)) as difference_minute
from fact_flight_sales
group by payment_method
the query returns the in decimal form
so the code gonna return every avg below 1 min in decimal
this is my code on pandasql
q2 = """
select payment_method,booking_created_time,booking_paid_time,(booking_created_time-booking_paid_time)
from dffact_flight_sales
group by payment_method
"""
print(sqldf(q2, locals()))
pandas SQL only returns the day difference, not the hour and minute.
how can I make the query? work exactly like my SQL server query?
import pandas as pd
dffact_flight_sales = pd.read_csv(r"C:\Users\lixfe\Desktop\fact_flight_sales.csv")
dffact_flight_sales['time difference'] = ((pd.to_datetime(dffact_flight_sales['booking_paid_time']) -
pd.to_datetime(dffact_flight_sales['booking_created_time']))
.dt.total_seconds() / 60)
GK = dffact_flight_sales.groupby('payment_method')
GK1 = GK[['payment_method','time difference']]
GK1.first()
Hi I am developing a stocks portfolio tracker. As part of this I am pulling all my listed stocks from one website which gives a performance % of 1 week, 1 month, YTD and 1 year. Some of my other investments which I am needing to pull figures for individually only gives the current price as of that day.
I am storing all my data in a mysql database using python (pymysql library currently). What I want to know is how can I do a lookup of a value at the various time periods. I know how I could just do lookup 1 week etc. however sometimes there will be no data on that particular day and I need to just get the either the closest record or the last prior or the next record (not that fussy).
tblHistory with fields rDate, rStockID, rPrice among other fields
Query something like this but varDate1Week is an approximate which won't always have an "EXACT" match
"SELECT rPrice FROM tblHistory WHERE (rStockID = " + str(varStockID) + " AND rDate = " + varDate1Week + ")"
My Sample Code after Luuk's assistance.
import datetime
import pymysql
db = pymysql.connect(host='localhost',user='Test',password='TestPassword',database='dbInvest')
varrDate2 = datetime.datetime.now().strftime("%Y-%m-%d")
cursor2 = db.cursor(pymysql.cursors.DictCursor)
sqllookup = "SELECT rPrice FROM tblHistory WHERE rStockID = 7 and rDate >= DATE_ADD('2021-06-20', INTERVAL - 7 DAY) ORDER BY rDATE LIMIT 1"
cursor2.execute(sqllookup)
Perform1WeekList = cursor2.fetchall()
print ("1 Week Value is " + str(Perform1WeekList[0]))
cursor2.close
The output I get from this is
1 Week Value is {'ClosePrice': Decimal('86.7400')}
Would like to be able to use variable varrDate2 in place of hard coded date. Also to have clean output (ie. 86.7400). I could do it with a split and then a replace on the string but sure there is a better way.
In MySQL you can do:
SELECT
t1.rPrice,
(SELECT t2.rPrice
FROM tblHistory t2
WHERE t2.rStockID = t1.rStockID
and t2.rDate <= DATE_ADD(t1.rDate,INTERVAL -7 DAY)
ORDER BY r2.rDate DESC
LIMIT 1) "1weekago"
(SELECT t3.rPrice
FROM tblHistory t3
WHERE t3.rStockID = t1.rStockID
and t3.rDate <= DATE_ADD(t1.rDate,INTERVAL -30 DAY)
ORDER BY r3.rDate DESC
LIMIT 1) "30daysago"
FROM tblHistory t1
WHERE (t1.rStockID = 1 AND t1.rDate = '2021-06-19')
;
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])
Following Table (let's name it validity_period):
-------------------------------
id | valid_from | valid_until
-------------------------------
1 2012-11-12 2012-12-02
2 2012-12-03 NULL
3 2012-12-15 2012-12-21
(valid_from is not nullable; valid_until is nullable, but don't have to be null)
Now I want to find out which entry is valid today (2012-12-19). From the logical sight of view it has to be entry 3, because the entries can overlap each other but only one entry is valid on one day. (On 2012-12-22 it has to be entry 2 which is valid.)
Note that all entries can have a valid_until, but there can't be more than one entry where valid_until is NULL.
How would I perform this in a SQL-Query? (If possible in SQLAlchemy, but I also can translate it myself from raw SQL)
(I'm using PostgreSQL 9.1)
EDIT: Here my final resolution. Thanks to all contributors!
SELECT *
FROM validity_period
WHERE valid_from <= CURRENT_DATE AND
valid_until >= CURRENT_DATE
UNION
SELECT *
FROM validity_period
WHERE valid_from <= CURRENT_DATE AND
valid_until IS NULL
ORDER BY valid_from DESC
LIMIT 1;
You can use a WINDOW function (such as lag or lead) to refer to the previous/next record, and cap the "open" intervals:
DROP SCHEMA tmp CASCADE;
CREATE SCHEMA tmp ;
SET search_path=tmp;
CREATE TABLE daterange
( id INTEGER NOT NULL
, valid_from DATE NOT NULL
, valid_until DATE
);
INSERT INTO daterange (id, valid_from, valid_until) VALUES
(1, '2012-11-12', '2012-12-02' )
,(2, '2012-12-03', NULL)
,(3, '2012-12-15', '2012-12-21' )
,(4, '2012-12-22', NULL )
;
-- The CTE is for convenience; could be a subquery or view
WITH magic AS (
SELECT dr.id
, dr.valid_from
, COALESCE(dr.valid_until , lead(valid_from) OVER ww) AS valid_until
FROM daterange dr
WINDOW ww AS (ORDER BY dr.valid_from )
)
SELECT *
FROM magic ma
WHERE now() BETWEEN ma.valid_from AND ma.valid_until
;
Try something like this:
SELECT *
FROM validity_period
WHERE valid_from <= current_date
AND valid_to >=current_date
UNION
--Gets the last instance where valid_until is null
SELECT *
FROM validity_period
WHERE valid_from <= current_date
AND valid_to IS NULL
ORDER BY id DESC LIMIT 1
Simply remove the DESC if you want the first rather than the last null field
The valid_until is logically redundant in the data you give as an example, and you might get a faster result with this logic if an index on valid_from is present and PostgreSQL will perform a descending index scan:
select *
from validity_period
where valid_from <= current_date
order by valid_from desc
limit 1
Check the explain plan to see if it is an improvement on larger data sets.
Edit: If there can be a situation where the "last" record has a valid until date prior to today, where no rows are to be returned, try:
select most_recent_record.*
from (
select *
from validity_period
where valid_from <= current_date
order by valid_from desc
limit 1) most_recent_record
where coalesce(most_recent_record.valid_until,current_date) <= current_date
That last predicate might read better as:
where not (most_recent_record.valid_until > current_date)
You question is not very clear. Can there be more than one matches with non-null valid_until? Which to pick? What do you want to return? One column? Several? All?
Anyhow, what you posted as solution can be simplified to:
SELECT *
FROM validity_period
WHERE valid_from <= now()::date
AND (valid_until >= now()::date OR valid_until IS NULL) -- parenthesis needed!
ORDER BY valid_from DESC
LIMIT 1;