I’m trying to figure out a way to list all numbers between two columns (INT) and join them in a third column where all numbers are seperated.
Eg.
Column A | Column B | Column C |
---|---|---|
100 | 105 | 100;101;102;103;104;105 |
10 | 14 | 10;11;12;13;14 |
Does anyone have a solotion for this?
Have googled around but didnt find any exact match
2
Answers
Use the
generate_series
which is a function to generate a series of numbers between two values. with the help ofstring_agg
function to concatenate these numbers into a single string, separated by semicolons:dbfiddle DEMO
Set-returning
generate_series()
does just that: demoString_agg()
does what you want butarray_agg()
orjsonb_agg()
are preferable because they maintain the adequate type and allow you to subscript the resulting array.In AWS Redshift flavour of PostgreSQL,
generate_series()
isn’t supported, which is why you’d have to use a recursive cte mentioned by @jarlh:Which works in regular PostgreSQL as well.