Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>SQL queries have no looping mechanism. Procedure languages have loops, but queries themselves can only "loop" over data that they find in a table (or a derived table).</p> <p>What I do to generate a list of numbers on the fly is to do a cross-join on a small table of digits 0 through 9:</p> <pre><code>CREATE TABLE n (d NUMERIC); INSERT INTO n VALUES (0), (1), (2), (3), (4), (5), (6), (7), (8), (9); </code></pre> <p>Then to generate 00..99:</p> <pre><code>SELECT n1.d + n2.d*10 AS d FROM n AS n1 CROSS JOIN n AS n10; </code></pre> <p>If you want only 00..57:</p> <pre><code>SELECT n1.d + n2.d*10 AS d FROM n AS n1 CROSS JOIN n AS n2 WHERE n1.d + n2.d*10 &lt;= 57; </code></pre> <p>You can of course join the table for the 100's place, 1000's place, etc. Note that you can't use column aliases in the WHERE clause, so you have to repeat the full expression.</p> <p>Now you can use this as a derived table in a <code>FROM</code> clause and join it to your data table.</p> <pre><code>SELECT n0.d, mytable.value FROM (SELECT n1.d + n2.d*10 + n2.d*100 + n3.d*1000 + n4.d*10000 + n5.d*100000 AS d FROM n AS n1 CROSS JOIN n AS n2 CROSS JOIN n AS n3 CROSS JOIN n AS n4 CROSS JOIN n AS n5) AS n0 LEFT OUTER JOIN mytable ON (n0.d = mytable.key) WHERE n0.d &lt;= (SELECT MAX(key) FROM mytable); </code></pre> <p>You do need to add another <code>CROSS JOIN</code> each time your table exceeds an order of magnitude in size. E.g. when it grows past 1 million, add a join for <code>n6</code>.</p> <p>Note also we can now use the column alias in the WHERE clause of the outer query.</p> <p>Admittedly, it can be a pretty expensive query to do this solely in SQL. You might find that it's both simpler and speedier to "fill in the gaps" by writing some application code.</p>
    singulars
    1. This table or related slice is empty.
    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. 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.
    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