I've been trying to use the generate_series() function of Postgres to obtain a a table like this:

enter image description here

I didn't arrived to a solution yet, so any ideas would be appreciated.

1 Answer 1


I guess something like this would do:

select x.n, y.n 
from generate_series(1,3) as x(n) 
cross join generate_series(1,4) as y(n)
order by y.n, x.n

The idea is to take the Cartesian product between a set of 3 elements and a set with 4 elements. In this case Z_3 and Z_4

Since 3 and 4 is co-prime ( gcd(3,4) = 1 ), we can conclude that the Cartesian product between Z_3 and Z_4 is isomorphic with Z_12 (if only we had formatting options ...). See zm-cross-zn-isomorphic-to-zmn for a proof. I.e we can use Z_12 and use modulo arithmetic to achieve the desired set:

select mod(x.n,3)+1, mod(x.n,4)+1 
from generate_series(1,12) as x(n) 
order by mod(x.n,4)+1, mod(x.n,3)+1
  • That's such an elegant solution, thanks! Apr 13, 2020 at 18:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.