Oracle query throwing exception via python - python

Below is the query and for which i am facing issue in python. I am using oracledb package.
Query:
SELECT SUM(COUNT) COUNT FROM (
SELECT COUNT(1) COUNT FROM abc WHERE datecol1 >= ((CAST(SYS_EXTRACT_UTC(SYSTIMESTAMP) AS DATE) - TO_DATE('01/01/1970','DD/MM/YYYY')) * 24 * 60 * 60 - 3600)
UNION ALL
SELECT COUNT(1) COUNT FROM xyz WHERE datecol > ((CAST(SYS_EXTRACT_UTC(SYSTIMESTAMP) AS DATE) - TO_DATE('01/01/1970','DD/MM/YYYY')) * 24 * 60 * 60 - 3600)
) mno;
When i try to execute above query in sqlpus client, Query is executed successfully.
But same query is being fired via python script. I am getting below exception.
oracledb.exceptions.DatabaseError: ORA-00920: invalid relational operator
When i try execute the subquery in python it executes successfully.
SELECT COUNT(1) COUNT FROM abc WHERE datecol1 >= ((CAST(SYS_EXTRACT_UTC(SYSTIMESTAMP) AS DATE) - TO_DATE('01/01/1970','DD/MM/YYYY')) * 24 * 60 * 60 - 3600)
UNION ALL
SELECT COUNT(1) COUNT FROM xyz WHERE datecol > ((CAST(SYS_EXTRACT_UTC(SYSTIMESTAMP) AS DATE) - TO_DATE('01/01/1970','DD/MM/YYYY')) * 24 * 60 * 60 - 3600)

Related

FROM SQL SERVER QUERY INTO PANDASQL DATEDIFF RETURN IN MINUTE

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()

Mongodb do a find and filter by difference of days

I want to get some documents that have a difference of days greater than 4
I want compare the currect day with 'fechaActualizacion'
example (using js and Moment):
moment().diff(fechaActualizada, 'days') > 4
This is my schema:
You can do it like this:
db.collection.find({
fechaActualizacion: { $lte: new Date(Date.now() - 4 * 24 * 60 * 60 * 1000).toISOString() }
});

Flask-sqlalchemy filter by unix interval

I store the created_at column as a unix integer on my Post table. I need to filter all posts that have a created_at unix timestamp that is between 23h59 and 24h00 old, from the start of that specific day.
In other words, I need to filter all of the posts that are in their last minute of that that 24h interval since they have been created.
This query works for posts that are less than a day old
post_min = now - 86400
post_max = now - 86400 + 60
posts = Post.query.filter(Post.created_at >= post_min, Post.created_at < post_max).all()
How do I do this for posts that are older than a day?
You can use modulo arithmetic:
(now % (60 * 60 * 24)) >= (60 * 60 * 24 - 60)
This spells out the multiplication. You can of course write this as:
(now % 86400) >= (86400 - 60)
It might not be obvious to most people (or even you in the future) that 60*60*24 = 86400.

Rounding the duration of two date values

I have two date values in Python which Im trying to get a rounded duration from.
For example: 01-01-2000 to 12-31-2099 is really "100 years", not "99 years".
I have an example, in Java, but Im not sure how to port this to Python speak:
round(endDateEpochInMilliseconds -startDateEpochInMilliseconds /(365.25 * 24 * 3600 * 1000))
Im sure something similar is doable in Python.
import datetime
date1 = datetime.date(2000,1,1)
date2 = datetime.date(2099, 12, 31)
delta = date2-date1
print round(delta.days/365.25,0)
You defently must put endDateEpochInMilliseconds -startDateEpochInMilliseconds into brackets!
round( (endDateEpochInMilliseconds -startDateEpochInMilliseconds) /(365.25 * 24 * 3600 * 1000))

How do I write this query in Django?

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'])

Categories