I'm stuck on this SQL problem.
I have a column that is a list of starting points (prevdoc
), and anther column that lists how many sequential numbers I need after the starting point (exdiff
).
For example, here are the first several rows:
prevdoc | exdiff
----------------
1 | 3
21 | 2
126 | 2
So I need an output to look something like:
2
3
4
22
23
127
128
I'm lost as to where even to start. Can anyone advise me on the SQL code for this solution?
Thanks!
If the numbers are not too large, then you can use the following trick in most databases:
The use of INFORMATION_SCHEMA columns in the subquery is arbitrary. The only purpose is to generate a sequence of numbers at least as long as the maximum exdiff number.
This approach will work in any database that supports the ranking functions. Most databases have a database-specific way of generating a sequence (such as recursie CTEs in SQL Server and CONNECT BY in Oracle).
If your exdiff is going to be a small number, you can make up a virtual table of numbers using
SELECT..UNION ALL
as shown here and join to it:I have provided for 5 but you can expand as required. You haven't specified your DBMS, but in each one there will be a source of sequential numbers, for example in SQL Server, you could use:
The master..spt_values table contains numbers between 0-2047 (when filtered by type='p').