![](/img/trans.png)
[英]Split column into multiple rows in ORACLE based on static length of substring
[英]Split rows into multiple rows based on string length of column Postgresql
我有下表
+---------------+---------------+-------------+
| employee_name | role | date_joined |
+---------------+---------------+-------------+
| John | 10013004 | 2018-01-09 |
| Jane | 1004 | 2020-08-09 |
| Sam | 100380003000 | 2022-03-31 |
+---------------+---------------+-------------+
我想將上表轉換為以下格式,其中角色列字符串應分成 4 組,並應添加為新條目。
+---------------+-------+-------------+
| employee_name | role | date_joined |
+---------------+-------+-------------+
| John | 1001 | 2018-01-09 |
| John | 3004 | 2018-01-09 |
| Jane | 1004 | 2020-08-09 |
| Sam | 1003 | 2022-03-31 |
| Sam | 8000 | 2022-03-31 |
| Sam | 3000 | 2022-03-31 |
+---------------+-------+-------------+
知道如何實現以下目標嗎?
您可以使用regexp_matches()
生成包含這 4 個字符子字符串的行:
select t.employee_name,
x.role[1] as role,
x.pos,
t.date_joined
from the_table t
cross join regexp_matches(t.role, '[0-9]{4}', 'g') with ordinality as x(role, pos)
order by t.employee_name, t.date_joined
regexp_matches()
返回一個匹配數組,這就是為什么需要x.role[1]
的原因。
如果該列可以包含其他字符,而不僅僅是數字,請使用'.{4}'
而不是'[0-9]{4}'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.