我有一个 Postgres 表,其中文本列的内容用“|”分隔。
ID | ... | my_column
-----------------------
1 | ... | text|concatenated|as|such
2 | ... | NULL
3 | ... | NULL
我试图 unnest(string_to_array()) 该列以分隔行,这些行工作正常,除了我的 NULL 值(> 90% 的所有条目)被排除在外。我尝试了几种方法:
SELECT * from "my_table", lateral unnest(CASE WHEN "this_column" is NULL
THEN NULL else string_to_array("this_column", '|') END);
或者
如此处建议:PostgreSQL unnest with empty array
我得到什么:
ID | ... | my_column
-----------------------
1 | ... | text
1 | ... | concatenated
1 | ... | as
1 | ... | such
但这就是我需要的:
ID | ... | my_column
-----------------------
1 | ... | text
1 | ... | concatenated
1 | ... | as
1 | ... | such
2 | ... | NULL
3 | ... | NULL