如何在presto中取消多列,输出到相应的行

问题描述 投票:1回答:1

我正在尝试删除一些代码

我有几列有数组,两列都使用|作为分隔符

The data would be stored looking like this, with extra values to the side which show the current currency

I want to output it like this我尝试做另一个不需要的专栏,就像这样

SELECT c.campaign, c.country, a.product_name, u.price--, u.price -- add price to this split. handy for QBR

FROM c, UNNEST(split(price, '|')) u(price), UNNEST(split(product_name, '|')) a(product_name)

group by 1,2, 3, 4

但是这个重复了几行,所以我不确定是否需要排除这两列并不是很有效

谢谢

presto
1个回答
5
投票

你的查询的问题是,FROM c, UNNEST(...), UNNEST(...)子句有效地计算了c的每一行与UNNEST调用产生的每个派生表产生的行之间的交叉连接。

你可以通过在一次调用UNNEST中取消所有数组来解决它,从而产生一个派生表。当以这种方式使用时,UNNEST生成一个表,每个数组有一列,数组中每个元素有一行。如果数组具有不同的长度,则它将生成最大数组中元素数量的行,并使用NULL填充较小数组的列。

为了说明,对于您的情况,这是您想要的:

WITH data(a, b, c) AS (
    VALUES
        ('a|b|c', '1|2|3', 'CAD'),
        ('d|e|f', '4|5|6', 'USD')
)
SELECT t.a, t.b, data.c
FROM data, UNNEST(split(a, '|'), split(b, '|')) t(a, b)

产生:

 a | b |  c
---+---+-----
 a | 1 | CAD
 b | 2 | CAD
 c | 3 | CAD
 d | 4 | USD
 e | 5 | USD
 f | 6 | USD
(6 rows)
© www.soinside.com 2019 - 2024. All rights reserved.