我在PostgreSQL 11.0 中有以下表格
col1 other_name
1 Galantamine
2 Galantamine::::Propane
3 null
4 Galantamine::::Propane::::Methane
我想将other_name中的值拆分为多行
所需输出为:
col1 other_name
1 Galantamine
2 Galantamine
2 Propane
3 null
4 Galantamine
4 Propane
4 Methane
我正在尝试以下查询:
select col1,
coalesce(btrim(regexp_split_to_table(col2,'::::')), col2, null) as col2
我在运行上述查询时遇到以下错误:
[Code: 0, SQL State: 0A000] ERROR: set-returning functions are not allowed in CASE
Hinweis: You might be able to move the set-returning function into a LATERAL FROM item.
我们非常感谢您的帮助。
按照提示的操作,将set返回函数移到FROM
子句中:
select t.col1, u.other_name
from the_table t
left join unnest(string_to_array(t.other_name, '::::')) as u(other_Name) on true
order by t.col1;
left join
是必要的,因为unnest
将不为other_name
中具有null
值的行返回行,而cross join
将完全移除该行。由于我们不需要真正的联接条件,所以我使用on true
来满足left join
运算符的语法要求。
在线示例