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

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

Tag: General

Unable to Connect AWS EC2 Instance with my subdirectory through domain registrar.
Publish date: 2022-02-25 | Comments: 2

Tag: General

Where can I find cheap Java and Mysql hosting server?
Publish date: 2022-05-03 | Comments: 2

Tag: General

Is it possible to git commit files without adding a message or comments?
Publish date: 2022-03-02 | Comments: 2

Tag: General

[solved] Unable to pull updates from git "sudo git pull"
Publish date: 2021-12-20 | Comments: 1

Tag: General

How to checkout to new created branch on bitbucket
Publish date: 2022-03-03 | Comments: 1

Tag: General

what is alternative for sending mail through Gmail SMTP?
Publish date: 2022-06-08 | Comments: 1

Tag: General