I need below output for the regexp_split_to_table output: -
select regexp_split_to_table ('a,b,c,d,e,f',',') expr
---------------------------------------------
rownum expr
--------------
1 a
2 b
3 c
4 d
5 e
6 f
i tried using the row_number() function as well, but it is returning 1 for all the rows
select row_number() over (), regexp_split_to_table ('a,b,c,d,e,f',',')
rownum expr
--------------
1 a
1 b
1 c
1 d
1 e
1 f
That's what with ordinality
is for:
select rownum, expr
from regexp_split_to_table ('a,b,c,d,e,f',',') with ordinality as t(expr, rownum)
The rownum ("ordinality") is the "index" of that element in the result of regexp_split_to_table
Note that using string_to_array()
with unnest
is typically faster as regexes are quite expensive:
select rownum, expr
from unnest(string_to_array('a,b,c,d,e,f',',')) with ordinality as t(expr, rownum)
Try wrapping your current query in a CTE and then use ROW_NUMBER
:
WITH cte AS (
SELECT REGEXP_SPLIT_TO_TABLE ('a,b,c,d,e,f', ',') expr
)
SELECT
ROW_NUMBER() OVER (ORDER BY expr) rownum,
expr
FROM cte
ORDER BY expr;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.