Prerequisites
- TerminusDB running locally — see Docker setup for instructions
- A database with numeric data
- Familiarity with WOQL basics (getting started)
What you'll achieve By the end of this guide, you will know how to perform mathematical operations in WOQL queries.
WOQL has a number of mathematical operations that can be performed. These include, plus, minus, divide, times, div (for integer division), exp and floor.
To use these operations you need to evaluate an arithmetic expression, and then you will be able to bind the result to a variable.
For instance:
Example: JavaScript
let v = Vars("result");
evaluate(times(2,3), v.result)This will store the value of 2 times 3 in the variable result. The bindings which result from this query are:
Example: JSON
[ {"result": {"@type":"xsd:decimal", "@value":6}} ]You can also chain these together, to build up more complicated computations, or use the results obtained by queries to derive new values.
Example: JavaScript
let v = Vars("result1", "result2");
and(evaluate(times(2,3), v.result1),
evaluate(times(v.result1,3), v.result2))Which results in:
Example: JSON
[ {"result1": {"@type":"xsd:decimal", "@value":6},
"result2": {"@type":"xsd:decimal", "@value":18}} ]