在postgresql中创建动态自定义系列(如果可能,避免循环)

在postgresql中创建动态自定义系列(如果可能,避免循环),postgresql,amazon-redshift,Postgresql,Amazon Redshift,我是postgresql的新手,我正在尝试做一些需要T-SQL循环的事情 对如何在postgresql中循环做了一些研究,发现我应该先做一些函数,我正试图避免这种情况 我有一张主桌 SELECT 17 as employeecount, 'Aug-2020' as month UNION SELECT 22, 'Sep-2020' UNION SELECT 27, 'Oct-2020' 我需要一个每月递增1到x(employeecount)的输出,如下所示: SELECT 1 as emplo

我是postgresql的新手,我正在尝试做一些需要T-SQL循环的事情 对如何在postgresql中循环做了一些研究,发现我应该先做一些函数,我正试图避免这种情况

我有一张主桌

SELECT 17 as employeecount, 'Aug-2020' as month
UNION
SELECT 22, 'Sep-2020'
UNION
SELECT 27, 'Oct-2020'
我需要一个每月递增1到x(employeecount)的输出,如下所示:

SELECT 1 as employeecount, 'Aug-2020' as month
UNION
SELECT 2, 'Aug-2020'
UNION
SELECT 3, 'Aug-2020'
........... up to 17, 'Aug-2020'
UNION
SELECT 1, 'Sep-2020'
UNION 
... up to 22, 'Sep-2020' and so on

我试图避免循环,但如果没有其他方法,那就好了


提前谢谢

使用
横向连接
生成\u系列()


使用
横向连接
生成_系列()

---------------------
increment | Month   |
1         | Aug-2020|
2         | Aug-2020|
3         | Aug-2020|
4         | Aug-2020|
.         | Aug-2020|
.         | Aug-2020|
17        | Aug-2020|
1         | Sep-2020|
2         | Sep-2020|
.         | Sep-2020|
.         | Sep-2020|
22        | Sep-2020|
with main (employeecount, month) as (
  values (17, 'Aug-2020'), (22, 'Sep-2020'), (27, 'Oct-2020')
)
select increment, month 
  from main
 cross join lateral generate_series(1, employeecount) as gs(increment);