# Is there a way to perform arithmetic operations in query

**URL:** <https://forum.scylladb.com/t/is-there-a-way-to-perform-arithmetic-operations-in-query/3229>\
**Category:** ScyllaDB\
**Tags:** data-model, cql\
**Created:** [November 25, 2024, 5:00pm UTC](https://forum.scylladb.com/t/is-there-a-way-to-perform-arithmetic-operations-in-query/3229 "2024-11-25T17:00:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bloodpriest](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.scylladb.com/bloodpriest/32/347_2.png) [@Bloodpriest](https://forum.scylladb.com/u/Bloodpriest)\
**Post date:** [November 25, 2024, 5:00pm UTC](https://forum.scylladb.com/t/is-there-a-way-to-perform-arithmetic-operations-in-query/3229/1 "2024-11-25T17:00:38Z")

</div>

I need to perform queries which check fields based on some arithmetic ops.  
For example-  
select a, b from table\_name  
where a + b == 10

Is there a way for this to be done?

---

<div class="post-metadata">

**Author:** ![Bloodpriest](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.scylladb.com/bloodpriest/32/347_2.png) [@Bloodpriest](https://forum.scylladb.com/u/Bloodpriest)\
**Post date:** [November 25, 2024, 6:15pm UTC](https://forum.scylladb.com/t/is-there-a-way-to-perform-arithmetic-operations-in-query/3229/2 "2024-11-25T18:15:49Z")

</div>

Im trying this-  
SELECT bucket, counter  
FROM used\_bucket\_tracker  
WHERE lounge\_id = ?  
AND bucket - counter \<= ?  
LIMIT 10

& I get-  
:invalid\_syntax, message: "line 4:4 no viable alternative at input ‘bucket’",

Even enclosing (bucket - counter) with the roud parentheses is useless, in that case it gives error-  
:invalid\_syntax, message: "line 4:4 no viable alternative at input ‘’",

---

<div class="post-metadata">

**Author:** ![felipemendes](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.scylladb.com/felipemendes/32/11_2.png) [@felipemendes](https://forum.scylladb.com/u/felipemendes)\
**Post date:** [December 9, 2024, 5:42pm UTC](https://forum.scylladb.com/t/is-there-a-way-to-perform-arithmetic-operations-in-query/3229/3 "2024-12-09T17:42:38Z")

</div>

As you realized, you can’t do this. Best is to pre-aggregate, so that you could then do `WHERE key=? AND c=10` or, as in the latter example, `WHERE key=? AND c <= ? LIMIT 10`

This works, but of course, it assumes that the other fields (which then would’ve been pre-aggregated) don’t change. Otherwise, perhaps denormalizing to a counter table (if you’re ok w/ eventual consistency) and regular PN-Counter should eventually converge to a common state.

---

<div class="post-metadata">

**Author:** ![Bloodpriest](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.scylladb.com/bloodpriest/32/347_2.png) [@Bloodpriest](https://forum.scylladb.com/u/Bloodpriest)\
**Post date:** [December 12, 2024, 6:48am UTC](https://forum.scylladb.com/t/is-there-a-way-to-perform-arithmetic-operations-in-query/3229/4 "2024-12-12T06:48:29Z")

</div>

thanks again. yeah i figured that out, so opted for somewhat of an architecture change, so yeah…thanks agian
