行号 regexp_split_to_table

Row number regexp_split_to_table

我有这个要求:

 select
    narttx,
    LIBETX
    --,row_number() over (partition by narttx)
from
(select
        codearticle_article as NARTTX,
        regexp_split_to_table(valeur_article_libelles, E'\x0b') as LIBETX
    from
        article.article a
    join article.article_libelles bl on
        bl.idarticle = a.idarticle
    and typelibelle_article_libelles = 'descriptifTech'
) vue
where   
narttx = '5627811' 
or 
narttx = '5627819'

它输出这个:

5627819 Finition
5627819 du
5627819 produit
...
5627811 Largeur (en cm) 
5627811 Hauteur (en cm)
5627811 Matière principale
...

我想添加行号并在 narttx 更改时重置它。 所以我取消注释“--,row_number() over (partition by narttx)”部分。

我应该:

5627819 Finition 1
5627819 du       2
5627819 produit  3
...
5627811 Largeur (en cm) 1
5627811 Hauteur (en cm) 2
5627811 Matière principale 3
...

但是不行,顺序不对:

5627811 Largeur (en cm) 1
5627811 Hauteur (en cm) 2
5627811 Matière principale 3
...
5627819 Largeur 1
5627819 du  2
5627819 corps   3
5627819 de  4
5627819 meuble  5
...
5627819 Finition    110
5627819 de  111
5627819 prise   112
...

如果我select只有一篇文章:

where   
narttx = '5627811'

效果很好,但是放两个的时候顺序不对...

为什么?

regexp_split_to_array() 移动到 from 子句(它所属的位置),然后你可以使用 with ordinality 它将自动 return 数组索引:

select
    narttx,
    libetx, 
    rn
from (select
        codearticle_article as narttx,
        v.*
    from
        article.article a
    join article.article_libelles bl 
          on bl.idarticle = a.idarticle
      and typelibelle_article_libelles = 'descriptifTech'
      cross join regexp_split_to_table(valeur_article_libelles, E'\x0b') with ordinality as v(libetx, rn)
) vue
where   
narttx = '5627811' 
or 
narttx = '5627819'