Finding intersecting number ranges

Hello, Bubblers.
I am working on a new application and suddenly got stuck on an easy at first sight task.
I have a datatype called “Commission” (2 fields “from” and “to” - all type “number”). Also there is a button for saving inputs mentioned above to the DB (create a new Commission).

Now I wan’t to add a check BEFORE saving a new Commission to the DB. I need to check whether there are any existing Commissions in the DB that intersect with a new one I’m trying to create. Intersection should be determined using a number range, where FROM is the minimal value of the range and TO is the maximum value of the range.

Example, I have 3 commissions in the DB:
FROM - TO
1 - 10
11 - 20
21 - 30

The check should work in the following way:

  • 31 - 40 should be saved successfully (no intersections with existing commissions)
  • 30 - 40 should not be saved (intersection with 1 commission from the DB)
  • 9 - 32 should not be saved (intersection with 3 commissions from the DB)

Any help will be appreciated!

1 Like

Bubble has a data type just for this: Numeric Ranges. They have the same operators as Date Ranges, pretty much. Consult the Reference on number ranges and their operators.

2 Likes

Hi there, @artemzheg… I would approach this one by having one field (as opposed to separate FROM and TO fields) on the Commission datatype, and that single field has a field type of numeric range. Then, the workflow to create a commission would have an Only when condition that is configured like this…

At that point, you can decide what you want to do when that count is greater than 0, but I think that should get you what you need. Hope this helps.

Best…
Mike

2 Likes

Hey, @mikeloc and @keith. Thanks for pointing the direction!
I looked at Bubble Reference before, but was confused by locating numeric ranges description in “Slider Input” section.
Just replaced my two separate “number” fields with one “numeric range” - and it works, now it’s possible to perform any check with intersecting ranges!
Thanks!

2 Likes

Yeah, it’s a really interesting feature that I haven’t seen many people use (but it’s the same as date ranges, so anything anyone’s written about date ranges pretty much applies to numeric ranges as well.)