[英]Redshift SQL - Reverse Listagg function
Suppose I have a table like below:假设我有一个如下表:
Name Order
AA 1,2
BB 2,3
I want my result to be like:我希望我的结果是这样的:
Name Order
AA 1
AA 2
BB 2
BB 3
How can I achieve this in Redshift?如何在 Redshift 中实现这一点?
Thanks!谢谢!
Assuming that you know in advance the maximum number of elements per delimited list, you can do this with a table of numbers and split_part()
:假设您事先知道每个分隔列表的最大元素数,您可以使用数字表和
split_part()
来做到这一点:
select t.name, split_part(t.order, ',', n.n) val
from (
select 1 n
union all select 2
union all select 3
) n
inner join mytable t
on n.n <= regexp_count(t.order, ',') + 1
you can expand derived table n
with more numbers as needed.您可以根据需要使用更多数字扩展派生表
n
。 It is also possible to use row_number()
against a large table to generate the numbers table.也可以对大表使用
row_number()
来生成数字表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.