user image

AKY King of Life
Published in : 2022-03-05

Select from a query with peewee

General

have some troubles implementing the following query with peewee:

SELECT *FROM ( SELECT datas.*, (rank() over(partition by tracking_id order by date_of_data DESC)) as rank_result FROM datas WHERE tracking_id in (1, 2, 3, 4, 5, 6))WHERE rank_result < 3;

I have tried to do the following:

subquery = (Datas.select(Datas.tracking, Datas.value, Datas.date_of_data, fn.rank().over(partition_by=[Datas.tracking], order_by=[Datas.date_of_data.desc()]).alias('rank')) .where(Datas.tracking.in_([1, 2, 3, 4, 5, 6])))result = (Datas.select() .from_(subquery) .where(SQL('rank') < 3))

but since I'm doing "Model.select()" i'm getting all the fields in the SQL SELECT which i don't want and which doesn't make my query work. Here is the schema of my table:

CREATE TABLE IF NOT EXISTS "datas" ( "id" INTEGER NOT NULL PRIMARY KEY, "tracking_id" INTEGER NOT NULL, "value" INTEGER NOT NULL, "date_of_data" DATETIME NOT NULL, FOREIGN KEY ("tracking_id") REFERENCES "follower" ("id"));CREATE INDEX "datas_tracking_id" ON "datas" ("tracking_id");

Thanks!

Comments

There is no comments yet

Leave a comment

Join us

Join our community and get the chance to solve your code issues & share your opinion with us

Sign up Now

Related posts

Choose default editor from filezilla on Ubuntu
Publish date: 2022-02-28 | Comments: 4

Tag: General

Chrome shows cursor everywhere where I click?
Publish date: 2022-03-11 | Comments: 2

Tag: General

How do i r etrieve users?
Publish date: 2022-03-04 | Comments: 0

Tag: General

Help with sliding an image from the top of a stack
Publish date: 2022-02-12 | Comments: 1

Tag: General

Keycloak db query in source codes [closed]
Publish date: 2022-03-05 | Comments: 0

Tag: General

Difference between frontend and backend user authentication
Publish date: 2022-02-12 | Comments: 2

Tag: General

docker elastic builder exited with code 127?
Publish date: 2022-02-27 | Comments: 2

Tag: General