SQLite Forum

Repeat values n times
Login
Assuming the sku/qty table is named `input`, here is a possible solution:

```
WITH RECURSIVE counter(value) AS (
   SELECT 1
     UNION ALL
   SELECT value + 1 FROM counter LIMIT (SELECT MAX(qty) FROM input)
)
SELECT * FROM input JOIN counter ON value <= qty ORDER BY sku, qty, value
```

The `counter` CTE will generate a sequence of integers `value` from 1 to the maximum `qty`. It is then joined with table `input` on `value <= qty`.

Using SQLite's specific table-valued function `generate_series()`, the recursive CTE could be removed as in:

```
SELECT * FROM input JOIN generate_series(1, (SELECT MAX(qty) FROM input)) ON value <= qty ORDER BY sku, qty, value
```

By the way, the `ORDER BY` clause is not required. It is only meant to help you inspect the result and check how each query works.