PostgreSQL 9.4 introduced WITH ORDINALITY for generating a sequence number for each element which are produced by UNNEST().
When we are talking about the string array, internal element numbering is also important for further operation.
I have prepared a small demonstration of this, you can access here.
Create a table with sample data:
CREATE TABLE tbl_TextArray(key varchar(100), tags text);
INSERT INTO tbl_TextArray(key, tags)
UNNEST() WITH ORDINALITY:
SELECT key, ArrayOrder, tag
FROM tbl_TextArray, unnest(tags)
WITH ORDINALITY As T (tag, ArrayOrder);
dbrnd=# SELECT key, ArrayOrder, tag
dbrnd-# FROM tbl_TextArray, unnest(tags)
dbrnd-# WITH ORDINALITY As T (tag, ArrayOrder);
key | arrayorder | tag
first | 1 | aa
first | 2 | fe
first | 3 | gr
first | 4 | fh
sec | 1 | tr
sec | 2 | we
sec | 3 | et
sec | 4 | ui
sec | 5 | er
third | 1 | we
third | 2 | gh
third | 3 | fv
third | 4 | dc
third | 5 | sx
Please share your ideas and opinions about this topic with me, your contribution will add true value to this topic.
If anyone has doubts on this topic then please do let me know by leaving comments or send me an email.
If you like this post, then please share it with others.
Please follow dbrnd.com, I will share my experience towards the success of Database Research and Development Activity.
I put up a post every day, please keep reading and learning.
Discover Yourself, Happy Blogging !
Anvesh M. Patel.