Python ORM query to get data based on duplicate numbers - python

Using python Djengo ORM. I want to create a query to get all data from table having less than 10 duplicate mobile numbers in column.
Below is the sql query which working fine but i want this in ORM.
Select *, count() over(pqrtition by mobile_number) as cnt From table_name Where cnt <=10
Below is the ORM query which i have created but not getting correct result
get_data = ResponseMangement.objects.annotate(count=Count('mobile_number')).filter(count__lt=10).order_by('-id')

Related

How to write insert query in postgresql using python for inserting datas into multiple tables with different number of columns

I want to insert multiple tables in postgresql using python.
I'm using a for loop for iterating tables having different number of columns.
So How can I write insert query dynamically for different number of fields
for table_name in tables:
query = f"""INSERT INTO {table_name} VALUES()"""
value=data_list
...........................
cursor.execute(query,value)

Convert a basic sql query to django ORM

I want to display the course name along with the question count in a table. Need help to convert below query to a django ORM:
SELECT DISTINCT exam_course.course_name,
COUNT(exam_question.question)
FROM exam_course
INNER JOIN exam_question ON exam_question.course_id = exam_course.id
GROUP BY exam_question.course_id
Use annotate with count as commented...use in this manner, replace accordingly your requirements:
invoices = Invoice.objects.annotate(total_amount=Sum('order__order_items__amount'),number_of_invoices=Count('pk', distinct=True))

How to make a subquery in sqlalchemy

SELECT *
FROM Residents
WHERE apartment_id IN (SELECT ID
FROM Apartments
WHERE postcode = 2000)
I'm using sqlalchemy and am trying to execute the above query. I haven't been able to execute it as raw SQL using db.engine.execute(sql) since it complains that my relations doesn't exist... But I succesfully query my database using this format: session.Query(Residents).filter_by(???).
I cant not figure out how to build my wanted query with this format, though.
You can create subquery with subquery method
subquery = session.query(Apartments.id).filter(Apartments.postcode==2000).subquery()
query = session.query(Residents).filter(Residents.apartment_id.in_(subquery))
I just wanted to add, that if you are using this method to update your DB, make sure you add the synchronize_session='fetch' kwarg. So it will look something like:
subquery = session.query(Apartments.id).filter(Apartments.postcode==2000).subquery()
query = session.query(Residents).\
filter(Residents.apartment_id.in_(subquery)).\
update({"key": value}, synchronize_session='fetch')
Otherwise you will run into issues.

Python-Eve query filter to find records in a range of values

I am using Python-Eve for my API (PostgresQL is my DB)
right now i am able to do > OR < operations individually as below
example.com/api?max_results=15&sort=-_id&where={"_id":">50"}
How can i query for _ids between two values like _id>50 and _id<100 ?

Django query with AVG and GROUP BY

My Django-foo isn't quite up to par to translate certain raw sql into the ORM.
Currently I am executing:
SELECT avg(<value_to_be_averaged>), <id_to group_on>
FROM <table_name>
WHERE start_time >= <timestamp>
GROUP BY <id_to group_on>;
In Django I can do:
Model.objects.filter(start_time__gte=<timestamp>).aggregate(Avg('<value_to_be_averaged>'))
but that is for all objects in the query and doesn't return a query set that is grouped by the id like in the raw SQL above. I've been fiddling with .annotate() but haven't made much progress. Any help would be appreciated!

Categories

Resources