sql - 将逗号分隔的列数据拆分为多列(可变大小)
问题描述
需要一些帮助将列数据(字符串)拆分Postgres
为多个虚拟列,例如:
---------column-------
data1;data2;data3 -
data1;data3 -
data1 -
进入
- Col1 ---- Col2 ---- Col3
1 | 1 | 1
1 | 0 | 1
1 | 0 | 0
我知道最大列数,所以我可以预设虚拟列。我需要做某种for循环吗?
J.P
解决方案
您可以使用 split_part():
select split_part(col, ';', 1) as col1,
split_part(col, ';', 2) as col2,
split_part(col, ';', 3) as col1
from the_table;
或者效率更高一些,因为每行只进行一次拆分:
select c[1] as col1,
c[2] as col2,
c[3] as col3
from (
select string_to_array(col, ';') as c
from the_table
) t;
推荐阅读
- ios - 表格视图单元格的案例。迅速
- python-3.x - 使用python从嵌套字典创建数据框
- ios - 如何在 Storyboard 中嵌套视图控制器
- java - 在 ComboBox 对象中检索数组对象
- docker - Docker swarm 健康检查失败杀死容器 - 退出 0
- macos - 我无法安装 pod
- javascript - Chrome 中 fetch() 和 setTimeout() 的执行优先级
- mysql - 我应该在 for 循环中将带有数组的对象作为属性插入到我的数据库中吗?
- html - 在相同的行高css上打印div
- c# - WinUI ProgressRing 不显示在 WASM (Uno 2.4) 上