admin管理员组文章数量:1289483
I am new to prisma-client
and I want to return the latest value register by each user of a given pany.
This is my schema.prisma
:
model Locations {
id Int @id @default(autoincrement())
user String
pany String
latitude String
longitude String
timestamp String
}
This is what I have tried so far:
const lastLocations = await db.locations.findMany({
where: {
pany,
},
orderBy: {
id: 'desc',
},
take: 1,
});
But I need to get 1 value for each user, I solved this previously in sql with:
WITH ranked_messages AS ( SELECT m.*, ROW_NUMBER() OVER (PARTITION BY userID ORDER BY timestamp DESC) AS rn FROM locations AS m ) SELECT * FROM ranked_messages WHERE rn = 1 AND panyID = "${panyID}";`;
But I have no idea how to do proceed in prisma. Is there an "each" method?
I appreciate any help. Thank you.
I am new to prisma-client
and I want to return the latest value register by each user of a given pany.
This is my schema.prisma
:
model Locations {
id Int @id @default(autoincrement())
user String
pany String
latitude String
longitude String
timestamp String
}
This is what I have tried so far:
const lastLocations = await db.locations.findMany({
where: {
pany,
},
orderBy: {
id: 'desc',
},
take: 1,
});
But I need to get 1 value for each user, I solved this previously in sql with:
WITH ranked_messages AS ( SELECT m.*, ROW_NUMBER() OVER (PARTITION BY userID ORDER BY timestamp DESC) AS rn FROM locations AS m ) SELECT * FROM ranked_messages WHERE rn = 1 AND panyID = "${panyID}";`;
But I have no idea how to do proceed in prisma. Is there an "each" method?
I appreciate any help. Thank you.
Share Improve this question edited Jan 24, 2022 at 13:46 programandoconro asked Jan 24, 2022 at 13:20 programandoconroprogramandoconro 2,7292 gold badges24 silver badges41 bronze badges1 Answer
Reset to default 11I solved it using the distinct
option:
const lastLocations = await db.locations.findMany({
where: {
pany,
},
distinct: ['user'],
orderBy: {
id: 'desc',
},
});
Thanks.
本文标签: javascriptPrisma client query for latest values of each userStack Overflow
版权声明:本文标题:javascript - Prisma client query for latest values of each user - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741394139a2376273.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论