K
Kysely•11mo ago
kewp

Unions in a loop

I'm trying to do a UnionAll in a loop. My issue is I don't know how to initialise the query:
let query = ...// ???

labels.forEach( label => {
let codes = get_codes(label);
query = query.unionAll(
db.selectFrom("data")
.select(eb => [
eb.val(label).as('label'),
'data.product',
'data.period',
])
.where("codes", "in", codes)
);
})

return await query.execute()
let query = ...// ???

labels.forEach( label => {
let codes = get_codes(label);
query = query.unionAll(
db.selectFrom("data")
.select(eb => [
eb.val(label).as('label'),
'data.product',
'data.period',
])
.where("codes", "in", codes)
);
})

return await query.execute()
Thanks for any suggestions. K
Solution:
You need to initialize it with the first select query.
Jump to solution
4 Replies
Solution
koskimas
koskimas•11mo ago
You need to initialize it with the first select query.
kewp
kewp•11mo ago
Makes sense. So then I have to start the loop from 1, i.e. not 0?
koskimas
koskimas•11mo ago
Yep
kewp
kewp•11mo ago
Thanks 🙂
Want results from more Discord servers?
Add your server
More Posts