Noext
DTDrizzle Team
•Created by Noext on 11/21/2023 in #help
group by inside relations ?
i want use groupBy inside a relations
i want to group by scanUrlHistory on the field day from my extras fields
const tire = await ctx.db.query.tire.findFirst({
where: (tire, { eq }) => eq(tire.id, Number(input.id)),
with: {
model: {
with: {
brand: true,
},
},
scanUrls: {
where: (scanUrls, { eq }) =>
eq(scanUrls.disabled, false),
with: {
website: true,
scanUrlHistory: {
extras: {
day: sql`concat(day(${schema.scanUrlHistory.createdAt}),'/',month(${schema.scanUrlHistory.createdAt}),'/',year(${schema.scanUrlHistory.createdAt}))`.as(
"day"
),
avg_price:
sql`AVG(${schema.scanUrlHistory.price})`.as(
"avg_price"
),
},
where: and(
gt(
schema.scanUrlHistory.createdAt,
dayjs().subtract(7, "day").format()
),
isNull(schema.scanUrlHistory.error)
),
},
},
},
},
});
const tire = await ctx.db.query.tire.findFirst({
where: (tire, { eq }) => eq(tire.id, Number(input.id)),
with: {
model: {
with: {
brand: true,
},
},
scanUrls: {
where: (scanUrls, { eq }) =>
eq(scanUrls.disabled, false),
with: {
website: true,
scanUrlHistory: {
extras: {
day: sql`concat(day(${schema.scanUrlHistory.createdAt}),'/',month(${schema.scanUrlHistory.createdAt}),'/',year(${schema.scanUrlHistory.createdAt}))`.as(
"day"
),
avg_price:
sql`AVG(${schema.scanUrlHistory.price})`.as(
"avg_price"
),
},
where: and(
gt(
schema.scanUrlHistory.createdAt,
dayjs().subtract(7, "day").format()
),
isNull(schema.scanUrlHistory.error)
),
},
},
},
},
});
5 replies