Michael
06/24/2021, 7:22 PMPurchases
like this:
id product price date
1 1 10 2014-01-01
2 1 20 2014-02-17
3 1 5 2014-03-28
4 2 25 2014-01-05
5 2 12 2014-02-08
6 2 30 2014-03-12
And I want to get a result that contains only the record/row that has the max date, by product
. Thus I want to get a result that is like this:
product price date
1 5 2014-03-28
2 30 2014-03-12
(because above for product
id 1 the most recent date is 2014-03-23 and for product
2 the most recent date is 2014-03-12.
Could anyone help me with constructing a query in Prisma to do this (is it even possible?). I assume I'll be issuing a groupBy
? Thank you.Michael
06/24/2021, 9:37 PMMichael
06/24/2021, 9:59 PMSELECT
p.product,
p.price,
p.date
FROM
products p INNER JOIN (
SELECT
product,
MAX(date) AS max_date
FROM
products
GROUP BY
product) m
ON p.product = m.product AND p.date = m.max_date
Joey
06/25/2021, 5:44 AMgroupBy
product
i.e
prisma.product.groudBy({
by: ["product"]
})
Joey
06/25/2021, 5:46 AMorderBy
and distinc
something like;
prisma.product.groudBy({
by: ["product"],
orderBy: {
date: 'desc',
},
distinct: ['product']
})
Michael
06/25/2021, 5:32 PM