Postgres - Select rownumber 没有 table 但数据来自函数

Postgres - Select rownumber without a table but data from a function

我正在尝试获得这样的结果:

rownumber | value
    1     |   a
    2     |   b
    3     |   c

没有任何表格,我正在做这样的事情:

WITH RECURSIVE t(rownumber, value) AS (
select 1, regexp_split_to_table('a, b, c', ',')
UNION ALL
SELECT rownumber+1, regexp_split_to_table('a, b, c',',') FROM t 
)
SELECT * FROM t limit (select count(*) from regexp_split_to_table('a, b, c', ','));

但结果并不如预期

我这样做的原因是因为值 'a, b, c' 应该是某个变量。

在 Oracle 中,SQL 看起来像这样:

SELECT value  FROM (
        SELECT ROWNUM AS rownumber, trim(REGEXP_SUBSTR('a, b, c','[^,]+', 1, LEVEL)) AS value
        FROM DUAL CONNECT BY trim(REGEXP_SUBSTR('a, b, c', '[^,]+', 1, LEVEL)) IS NOT NULL 
      ) 

并且有效。

我在 Postgresql 中做错了什么?

这是因为 regexp_split_to_table returns 一个 table 而不是每个行号的单个值。

也许换一种方法行得通?例如,这给了我你想要的输出:

SELECT  regexp_split_to_table('a,b,c', ',')   as value, 
        rank() over(order by regexp_split_to_table('a,b,c', ','))   as rownumber

编辑:以上将重新排序可能不是您想要的结果。以下将保留顺序:

WITH T as
(
SELECT  regexp_split_to_table('d,a,b,c', ',')   as value
)

SELECT  row_number() over() as rownumber,
        value

FROM    t