In the first code sample, I suppose that this portion should return each number between 0 and 32, right ?
SELECT
a.i + b.j + c.k
FROM
(SELECT 0 i UNION ALL SELECT 1 UNION ALL SELECT 2) a,
(SELECT 0 j UNION ALL SELECT 3 UNION ALL SELECT 6) b,
(SELECT 0 k UNION ALL SELECT 12 UNION ALL SELECT 24) c
ORDER BY 1
Well... it doesn't. It generate some numbers between 0 and 32, but not all of them. The above query only generates 27 values. If you want to generate each number between 0 and 31, you can use this query:
SELECT a.i + b.j + c.k
FROM
(SELECT 0 i UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3) a,
(SELECT 0 j UNION ALL SELECT 4 UNION ALL SELECT 8 UNION ALL SELECT 12) b,
(SELECT 0 k UNION ALL SELECT 16) c
ORDER BY 1
Razvan