[英]Struct and array - Error: Scalar subquery produced more than one element
I'm trying to put two tables together using struct and array.我正在尝试使用结构和数组将两个表放在一起。 My idea is for each row in table A apply Levenshtein Distance to table B.我的想法是对表 A 中的每一行应用 Levenshtein Distance 到表 B。
Table A:表一:
col1
whisky
delta
Tango
Table B表B
col1
Whiskey
delta force
Tango is great
Desired output:期望的输出:
col1 col2 col3
whisky Whiskey <lv_distance_score>
delta force <lv_distance_score>
Tango is great <lv_distance_score>
delta Whiskey <lv_distance_score>
delta force <lv_distance_score>
Tango is great <lv_distance_score>
Tango Whiskey <lv_distance_score>
delta force <lv_distance_score>
Tango is great <lv_distance_score>
For this, first I'm trying to just get de desired output of col1 and col2, but I keep getting an error
that says Scalar subquery produced more than one element
.为此,首先我试图获得 col1 和 col2 的期望输出,但我不断收到error
,指出Scalar subquery produced more than one element
。
The query I wrote is:我写的查询是:
WITH a AS (
SELECT col1, [STRUCT((SELECT col1 FROM table_B))] AS col2 FROM table_A
)
SELECT col1,c2 FROM a,UNNEST(a.col2) AS c2;
What I'm doing wrong here?我在这里做错了什么? How can I achieve what I'm looking for?我怎样才能实现我正在寻找的东西?
I'm a bit lost.我有点失落。 Why not just use a cross join
?为什么不直接使用cross join
?
select a.col1, b.col1
from a cross join
b
If you want one row per row in a
with an array for b, then:如果您希望a
每行一行与 b 的数组,则:
select a.col1, array_agg(b)
from a cross join
b
group by a.col1;
What I'm doing wrong here?我在这里做错了什么?
Below is simple fix for your original query以下是对原始查询的简单修复
WITH a AS (
SELECT col1,
[STRUCT(ARRAY(SELECT col1 FROM table_B) as col2)] AS col2
FROM table_A
)
SELECT col1, c2.col2
FROM a, UNNEST(a.col2) AS c2;
While above hopefully shows you what was wrong with your query - I am not sure it is right direction to go.虽然上面希望向您展示您的查询出了什么问题 - 我不确定这是正确的方向。
How can I achieve what I'm looking for?我怎样才能实现我正在寻找的东西?
you just go with simple cross join like in below example你只需像下面的例子一样简单的交叉连接
SELECT a.col1, ARRAY_AGG(b.col1 ORDER BY lv_distance_score(a.col1, b.col1) LIMIT 1)
FROM table_A a
CROSS JOIN table_B b
GROUP BY a.col1
Note: you can find plenty of examples for Levenshtein Distance UDF here on SO注意:您可以在 SO 上找到大量关于 Levenshtein Distance UDF 的示例
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.