T
TypeDB•15h ago
Joshua

Reducing with 0 when value is not there

question for the non-existent values: when we sum over an attribute, say payment and for some records that this value is non-existent (which i believe typedb doesn't create any instance of that attribute for that particular entity?), how can i set 0 for those cases in my sum? I don't care about my entity just in my sum i want to replace n/e with 0. here is a query, where $pa could be non-existent for some records:
match
$p isa payment_detail,
has loan_id $li,
has payment_amount $pa;
reduce $payments = sum($pa) groupby $li;
sort $li;
match
$p isa payment_detail,
has loan_id $li,
has payment_amount $pa;
reduce $payments = sum($pa) groupby $li;
sort $li;
(originally from @Reza )
3 Replies
Joshua
JoshuaOP•15h ago
@Reza sneak peak preview of optionals coming in 3.7.0 - but already available in the match patterns in 3.5.x (undocumented until fully implemented):
match
$p isa payment_detail,
has loan_id $li;
try {
$p has payment_amount $pa;
};
reduce $payments = sum($pa) groupby $li;
sort $li;
match
$p isa payment_detail,
has loan_id $li;
try {
$p has payment_amount $pa;
};
reduce $payments = sum($pa) groupby $li;
sort $li;
try that, im not entirely sure how stable it is since we added tests for it for the next major release
Reza
Reza•14h ago
thanks josh at least i didn't get any syntax error 🙂 does that ignore non-existent values or set it to 0? i don't want them to be ignored
Joshua
JoshuaOP•14h ago
try running
match
$p isa payment_detail,
has loan_id $li;
try {
$p has payment_amount $pa;
};
match
$p isa payment_detail,
has loan_id $li;
try {
$p has payment_amount $pa;
};
$pa will be None when not present, which in reduce is treated as 0

Did you find this page helpful?