将字符串字符按出现顺序拆分为每个标识符的行?

huangapple go评论137阅读模式
英文:

how to split string characters into rows by order of appearance for each id?

问题

假设我有一个由id列和由一组字符表示的字符串组成的表格:

  1. with fake_data(id, ex_character) as (
  2. select * from values
  3. ('A', 'T70891'),
  4. ('B', 'RT9811111')
  5. )

我想将字符串字符拆分为每个不同id值的行,所期望的输出应该如下所示:

  1. # id value
  2. # A T
  3. # A 7
  4. # A 0
  5. # A 8
  6. # A 9
  7. # A 1
  8. # B R
  9. # B T
  10. # B 9
  11. # B 8
  12. # B 1
  13. # B 1
  14. # B 1
  15. # B 1
  16. # B 1

到目前为止,我尝试了类似于以下的方法:

  1. select fd.id, f.value
  2. from fake_data fd,
  3. lateral split_to_table(regexp_replace(trim(fd.ex_character), '.', './0', 2), ',') f

但这种方法没有按顺序检索旋转列。

您知道如何以有序方式检索期望的数据吗?

英文:

Suppose, that I have a table composed by an id column and by a string represented by a set of characters:

  1. with fake_data(id, ex_character) as (
  2. select * from values
  3. ('A', 'T70891'),
  4. ('B', 'RT9811111')
  5. )

I would like to split string characters into rows for each different id value, so expected output should look like this:

  1. # id value
  2. # A T
  3. # A 7
  4. # A 0
  5. # A 8
  6. # A 9
  7. # A 1
  8. # B R
  9. # B T
  10. # B 9
  11. # B 8
  12. # B 1
  13. # B 1
  14. # B 1
  15. # B 1
  16. # B 1

So far I've tried something similar to:

  1. select fd.id, f.value
  2. from fake_data fd,
  3. lateral split_to_table(regexp_replace(trim(fd.ex_character), '.', './/0', 2), ',') f

But this approach hasn't worked as it retrieves pivoted column unordered.

Do you know how may I retrieve expected data in an ordered way?

答案1

得分: 2

如果你将拆分更改为:

  1. lateral split_to_table(regexp_replace(trim(fd.ex_character), ''.', ''.\
    lateral split_to_table(regexp_replace(trim(fd.ex_character), ''.', ''.\\0'', 2), ''.') f
  2. '', 2), ''.') f

它将正常工作。要按顺序获取你想要的内容,你可以使用 f.index

因此:

  1. with fake_data(id, ex_character) as (
  2. select * from values
  3. ('A', 'T70891'),
  4. ('B', 'RT9811111')
  5. )
  6. select
  7. fd.id
  8. ,f.value
  9. from fake_data as fd,
  10. lateral split_to_table(regexp_replace(trim(fd.ex_character), ''.', ''.\
    with fake_data(id, ex_character) as (
  11.     select * from values
  12.         ('A', 'T70891'),
  13.         ('B', 'RT9811111')
  14. )
  15. select 
  16.     fd.id
  17.     ,f.value
  18. from fake_data as fd,
  19. lateral split_to_table(regexp_replace(trim(fd.ex_character), ''.', ''.\\0'', 2), ''.') f
  20. order by fd.id
  21. '', 2), ''.') f
  22. order by fd.id

得到结果如下:

ID VALUE
A T
A 7
A 0
A 8
A 9
A 1
B R
B T
B 9
B 8
B 1
B 1
B 1
B 1
B 1

所以不需要选择 f.index,我只是包含它,以便你可以看到可用的内容。

英文:

if you swap the split to be:

  1. lateral split_to_table(regexp_replace(trim(fd.ex_character), '.', '.\
    lateral split_to_table(regexp_replace(trim(fd.ex_character), '.', '.\\0', 2), '.') f
  2. ', 2), '.') f

it will happen correctly. to order thing you want to use f.index

thus:

  1. with fake_data(id, ex_character) as (
  2. select * from values
  3. ('A', 'T70891'),
  4. ('B', 'RT9811111')
  5. )
  6. select
  7. fd.id
  8. ,f.value
  9. ,f.index
  10. from fake_data as fd,
  11. lateral split_to_table(regexp_replace(trim(fd.ex_character), '.', '.\
    with fake_data(id, ex_character) as (
  12. select * from values
  13. ('A', 'T70891'),
  14. ('B', 'RT9811111')
  15. )
  16. select 
  17. fd.id
  18. ,f.value
  19. ,f.index
  20. from fake_data as fd,
  21. lateral split_to_table(regexp_replace(trim(fd.ex_character), '.', '.\\0', 2), '.') f
  22. order by fd.id, f.index
  23. ', 2), '.') f
  24. order by fd.id, f.index

gives:

ID VALUE INDEX
A T 1
A 7 2
A 0 3
A 8 4
A 9 5
A 1 6
B R 1
B T 2
B 9 3
B 8 4
B 1 5
B 1 6
B 1 7
B 1 8
B 1 9

so the selecting of f.index is not needed, I just included it, so you could see what was available.

huangapple
  • 本文由 发表于 2023年8月5日 08:36:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76839718.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定