There have been quite a number of question regarding ranges in sql however i cant find anything that resembles my use case (most refer to a Tally or numbers table or procedures, something im trying to avoid seeing the elegance in the link below which uses neither)
Im trying to generate numbers between 1 and x, x coming for another table whilst excluding certain numbers from yet another table. (although there is a link between the 2, see the join)
Based on the answer for generating a range of numbers located at https://stackoverflow.com/a/64151448/1161646, im trying to do the following (approximation of the problem not the actual query):
select number
from excluding_numbers as exclusions join ranges range on range.id = exclusions.ranges_id,
(Select 0 + ROW_NUMBER() over (order by (Select null)) as number
from string_split(replicate(' ', range.limit - 1), ' ')) as numbers
where exclusions.number != number
and range.id = 1
The problem is the range.limit in the inner query 'numbers'
Example data:
ranges
id | limit |
---|---|
1 | 120000 |
2 | 10 |
3 | 10000000000 |
excluding_numbers
id | number | ranges_id |
---|---|---|
1 | 1 | 2 |
2 | 2 | 2 |
3 | 50000 | 3 |
This is saying from the range of 1 to 10 (ranges id 2) exclude number 1 and 2, from ranges 1 to 10000000000 (ranges id 3) only exclude number 50000.