Peewee 3 - Python - Rank query results - python-3.x

I have a self-referencing db table with notes (id, title, parent).
[Note: the self-referencing property of the table is not relevant to the question]
I would like to order them alphabetically by title and find the rank (= order) of a specific note by its id. I'm using Peewee 3 as ORM.
DB Model:
class Note(Model):
title = CharField()
parent = ForeignKeyField('self', backref='children', null = True)
Code:
noteAlias = Note.alias()
subquery = (noteAlias.select(noteAlias.id, fn.RANK().over(partition_by=[noteAlias.title], order_by=[noteAlias.title]).alias('rank')).where(noteAlias.parent.is_null()).alias('subq'))
query = (Note.select(subquery.c.id, subquery.c.rank).from_(subquery).where(subquery.c.id == 5))
print("Rank of element is: " + str(query.rank))
This code gives me the following error:
cursor.execute(sql, params or ())
sqlite3.OperationalError: near "(": syntax error
SQLite test
If I simply run this sqlite code directly against my db:
SELECT (title, ROW_NUMBER() OVER (ORDER BY title) AS placement) FROM note
I get the error: near ",": syntax error:

Your version of SQLite perhaps doesn't support window functions, as this was added relatively recently in 3.25 I believe.

Related

Use Common Table Expression with Pony ORM

I have the following query that contains a common table expression:
WITH example AS (
SELECT unnest(ARRAY['foo', 'bar', 'baz']) as col
)
SELECT *
FROM example
Trying to use it in database.select(query) throws pony.orm.dbapiprovider.ProgrammingError: syntax error at or near "WITH", and database.select(raw_sql(query)) throws TypeError: expected string or bytes-like object.
How can I select data using a CTE with ponyorm?
To use a query containing a CTE, call the execute function on the database and fetch the rows with the returned cursor:
cursor = database.execute("""
WITH example AS (
SELECT unnest(ARRAY['foo', 'bar', 'baz']) as col
)
SELECT *
FROM example
""")
rows = cursor.fetchall()
Note: The cursor is a class from psycopg2, so while this solution does use the pony library the solution may differ depending on the database being used.

How to apply DISTINCT on only date part of datetime field in sqlalchemy python?

I need to query my database and return the result by applying Distinct on only date part of datetime field.
My code is:
#blueprint.route('/<field_id>/timeline', methods=['GET'])
#blueprint.response(field_timeline_paged_schema)
def get_field_timeline(
field_id,
page=1,
size=10,
order_by=['capture_datetime desc'],
**kwargs
):
session = flask.g.session
field = fetch_field(session, parse_uuid(field_id))
if field:
query = session.query(
func.distinct(cast(Capture.capture_datetime, Date)),
Capture.capture_datetime.label('event_date'),
Capture.tags['visibility'].label('visibility')
).filter(Capture.field_id == parse_uuid(field_id))
return paginate(
query=query,
order_by=order_by,
page=page,
size=size
)
However this returns the following error:
(psycopg2.errors.InvalidColumnReference) for SELECT DISTINCT, ORDER BY expressions must appear in select list
The resulting query is:
SELECT distinct(CAST(tenant_resson.capture.capture_datetime AS DATE)) AS distinct_1, CAST(tenant_resson.capture.capture_datetime AS DATE) AS event_date, tenant_resson.capture.tags -> %(tags_1)s AS visibility
FROM tenant_resson.capture
WHERE tenant_resson.capture.field_id = %(field_id_1)s
Error is:
Query error - {'error': ProgrammingError('(psycopg2.errors.InvalidColumnReference) SELECT DISTINCT ON expressions must match initial ORDER BY expressions\nLINE 2: FROM (SELECT DISTINCT ON (CAST(tenant_resson.capture.capture...\n ^\n',)
How to resolve this issue? Cast is not working for order_by.
I am not familiar with sqlalchemy but this resulting query works as you expect. Please note the DISTINCT ON.
Maybe there is a way in sqlalchemy to execute non-trivial parameterized queries? This would give you the extra benefit to be able to test and optimize the query upfront.
SELECT DISTINCT ON (CAST(tenant_resson.capture.capture_datetime AS DATE))
CAST(tenant_resson.capture.capture_datetime AS DATE) AS event_date,
tenant_resson.capture.tags -> %(tags_1)s AS visibility
FROM tenant_resson.capture
WHERE tenant_resson.capture.field_id = %(field_id_1)s;
You can order by event_date if your business logic needs.
The query posted by #Stefanov.sm is correct. In SQLAlchemy terms it would be
query = (
session.query(
Capture.capture_datetime.label('event_date'),
Capture.tags['visibility'].label('visibility')
).distinct(cast(Capture.capture_datetime, Date))\
.filter(Capture.field_id == parse_uuid(field_id))
)
See the docs for more information
I needed to add order_by to my query. Now it works fine.
query = session.query(
cast(Capture.capture_datetime, Date).label('event_date'),
Capture.tags['visibility'].label('visibility')
).filter(Capture.field_id == parse_uuid(field_id)) \
.distinct(cast(Capture.capture_datetime, Date)) \
.order_by(cast(Capture.capture_datetime, Date).desc())

python oracle where clause containing date greater than comparison

I am trying to use cx_Oracle to query a table in oracle DB (version 11.2) and get rows with values in a column between a datetime range.
I have tried the following approaches:
Tried between clause as described here, but cursor gets 0 rows
parameters = (startDateTime, endDateTime)
query = "select * from employee where joining_date between :1 and :2"
cur = con.cursor()
cur.execute(query, parameters)
Tried the TO_DATE() function and Date'' qualifiers. Still no result for Between or >= operator. Noteworthy is that < operator works. I also got the same query and tried in a sql client, and the query returns results. Code:
#returns no rows:
query = "select * from employee where joining_date >= TO_DATE('" + startDateTime.strftime("%Y-%m-%d") + "','yyyy-mm-dd')"
cur = con.cursor()
cur.execute(query)
#tried following just to ensure that some query runs fine, it returns results:
query = query.replace(">=", "<")
cur.execute(query)
Any pointers about why the between and >= operators are failing for me? (my second approach was in line with the answer in Oracle date comparison in where clause but still doesn't work for me)
I am using python 3.4.3 and used cx_Oracle 5.3 and 5.2 with oracle client 11g on windows 7 machine
Assume that your employee table contains the field emp_id and the row with emp_id=1234567 should be retrieved by your query.
Make two copies of your a program that execute the following queries
query = "select to_char(:1,'YYYY-MM-DD HH24:MI:SS')||' >= '||to_char(joining_date,'YYYY-MM-DD HH24:MI:SS')||' >= '||to_char(:2,'YYYY-MM-DD HH24:MI:SS') resultstring from employee where emp_id=1234567"
and
query="select to_char(joining_date,'YYYY-MM-DD HH24:MI:SS')||' >= '||to_char(TO_DATE('" + startDateTime.strftime("%Y-%m-%d") + "','yyyy-mm-dd'),'YYYY-MM-DD HH24:MI:SS') resultstring from employee where emp_id=1234567"
Show us the code and the value of the column resultstring
You are constructing SQL queries as strings when you should be using parameterized queries. You can't use parameterization to substitute the comparison operators, but you should use it for the dates.
Also, note that the referenced answer uses the PostgreSQL parameterisation format, whereas Oracle requires you to use the ":name" format.

Spotfire : Syntax issue or Window functions not allowed?

I am using Data functions in Spotfire.
I have the sqldf package installed.
Here is the query:
#Package to run sqls
library(sqldf)
#Input data frame
op1 <- sqldf("SELECT Prod_parnt,prodct_grop,year,month,week,
count(distinct id) as prd_cnt,
Sum(Count(distinct id))
over (partition by modlty,prodct_grop order by year,month,week
rows between 12 preceding and current row) as cumu_prd_cnt,
avg(rate) as sal_rate
FROM ip1
group by Prod_parnt,prodct_grop,year,month,week")
The error I am facing:
"TIBCO Spotfire Statistics Services returned an error: 'Error: error in statement: near "(": syntax error'."
Now the point to note here is that when I remove the Window function statement i.e cumu_prd_cnt field; the code works fine.
Need your help.

LINQ to nHibernate - translating SQL "NOT IN" expression to LINQ

I tried to translate SQL "NOT IN" expression to LINQ, and I found that I should use the "Contains" option.
I have 2 tables:
ProductsGroups Products
-------------- ---------
id product_id
product_id product_name
My queries looks like this:
var innerQuery = from pg in Session.Query<ProductsGroups>
select pg.product_id;
var Query = from p in Session.Query<Products>
where !innerQuery.Contains(p.product_id)
select new {p.product_id, p.product_name};
But the sql that nHibernate generates is wrong:
select p.product_id, p.product_name
from Products p
where not (exists (select product_id
from ProductsGroups pg
where p.product_id = pg.id))
The "where" clause is not on the right field, it compares product_id to progucts group id.
Does anybody knows how can I solve it?
The solution that I found for meanwhile is to convert first query to list, and then
use this list in second query:
var innerQuery = (from pg .....).ToList();
Then, the nHibernate translates the "Contains" expression to "NOT IN", as I want:
select p.product_id, p.product_name
from Products p
where not (p.product_id in (1,2,3,4))
I am not sure, but I think you're running into a problem b/c contains determines if an element is in the collection by "using the default equality comparer." (MS documentation) I assume your productgroup mapping specifies it's Id as the Id property. So from nHibernate's perspective that is the value to use to determine equality.

Resources