K
Kysely2mo ago
OverHash

Selecting result of subtraction with summation

Hey, I'm having trouble translating the effective following Postgresql code to Kysely code:
SELECT
reagent_inventory.initial_quantity -
COALESCE(SUM(reagent_inventory_usage.quantity_consumed), 0) AS remaining_quantity
SELECT
reagent_inventory.initial_quantity -
COALESCE(SUM(reagent_inventory_usage.quantity_consumed), 0) AS remaining_quantity
I tried to use this:
.select((eb) =>
eb(
eb.val("inventory.initial_quantity"),
"-",
eb.fn.coalesce(
eb.fn.sum<number>("inventory_usage.quantity_consumed"),
eb.val(0),
),
).as("remaining_quantity"),
)
.select((eb) =>
eb(
eb.val("inventory.initial_quantity"),
"-",
eb.fn.coalesce(
eb.fn.sum<number>("inventory_usage.quantity_consumed"),
eb.val(0),
),
).as("remaining_quantity"),
)
but this yields to a type error:
Argument of type 'ExpressionWrapper<Database, "inventory" | "inventory_usage", number>' is not assignable to parameter of type 'OperandValueExpressionOrList<...>, "inventory" | "inventory_usage", ExpressionWrapper<...>>'.
Property 'isSelectQueryBuilder' is missing in type 'ExpressionWrapper<..., "inventory" | "inventory_usage", number>' but required in type 'SelectQueryBuilderExpression<Record<string, string | null>>'.
Argument of type 'ExpressionWrapper<Database, "inventory" | "inventory_usage", number>' is not assignable to parameter of type 'OperandValueExpressionOrList<...>, "inventory" | "inventory_usage", ExpressionWrapper<...>>'.
Property 'isSelectQueryBuilder' is missing in type 'ExpressionWrapper<..., "inventory" | "inventory_usage", number>' but required in type 'SelectQueryBuilderExpression<Record<string, string | null>>'.
Does anyone have any ideas about how to safely get around this? Was hoping to avoid using sql. Here's a minimal repro of what I'm trying to do: https://kyse.link/EdviN
Solution:
oh oops, I think I just meant to use "inventory.initial_quantity", rather than wrapping it in eb.val 🤦‍♂️
Jump to solution
2 Replies
Solution
OverHash
OverHash2mo ago
oh oops, I think I just meant to use "inventory.initial_quantity", rather than wrapping it in eb.val 🤦‍♂️
OverHash
OverHashOP2mo ago
error was tricking me to the later part of the code.
Want results from more Discord servers?
Add your server