Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think the very short answer to your question is to use WITH clauses to generate your own.</p> <p>Unfortunately, the big names in databases don't have built-in queryable number-range pseudo-tables. Or, more generally, easy pure-SQL data generation features. Personally, I think this is a <strong>huge</strong> failing, because if they did it would be possible to move a lot of code that is currently locked up in procedural scripts (T-SQL, PL/SQL, etc.) into pure-SQL, which has a number of benefits to performance and code complexity.</p> <p>So anyway, it sounds like what you need in a general sense is the ability to generate data on the fly.</p> <p>Oracle and T-SQL both support a WITH clause that can be used to do this. They work a little differently in the different DBMS's, and MS calls them "common table expressions", but they are very similar in form. Using these with recursion, you can generate a sequence of numbers or text values fairly easily. Here is what it might look like...</p> <p>In Oracle SQL:</p> <pre><code>WITH digits AS -- Limit recursion by just using it for digits. (SELECT LEVEL - 1 AS num FROM DUAL WHERE LEVEL &lt; 10 CONNECT BY num = (PRIOR num) + 1), numrange AS (SELECT ones.num + (tens.num * 10) + (hundreds.num * 100) AS num FROM digits ones CROSS JOIN digits tens CROSS JOIN digits hundreds WHERE hundreds.num in (1, 2)) -- Use the WHERE clause to restrict each digit as needed. SELECT -- Some columns and operations FROM numrange -- Join to other data if needed </code></pre> <p>This is admittedly quite verbose. Oracle's recursion functionality is limited. The syntax is clunky, it's not performant, and it is limited to 500 (I think) nested levels. This is why I chose to use recursion only for the first 10 digits, and then cross (cartesian) joins to combine them into actual numbers.</p> <p>I haven't used SQL Server's Common Table Expressions myself, but since they allow self-reference, recursion is MUCH simpler than it is in Oracle. Whether performance is comparable, and what the nesting limits are, I don't know.</p> <p>At any rate, recursion and the WITH clause are very useful tools in creating queries that require on-the-fly generated data sets. Then by querying this data set, doing operations on the values, you can get all sorts of different types of generated data. Aggregations, duplications, combinations, permutations, and so on. You can even use such generated data to aid in rolling up or drilling down into other data.</p> <p><strong>UPDATE:</strong> I just want to add that, once you start working with data in this way, it opens your mind to new ways of thinking about SQL. It's not just a scripting language. It's a fairly robust data-driven <a href="http://en.wikipedia.org/wiki/Declarative_programming_language" rel="nofollow noreferrer">declarative language</a>. Sometimes it's a pain to use because for years it has suffered a dearth of enhancements to aid in reducing the redundancy needed for complex operations. But nonetheless it is very powerful, and a fairly intuitive way to work with data sets as both the target and the driver of your algorithms.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload