Amazon redshift 字符串值是否存在于字符串列表|红移查询中
我有一些有趣的数据,我试图查询,但我不能得到正确的语法。我有一个临时表temp_id,其中填充了我关心的id值。在本例中,只有两个IDAmazon redshift 字符串值是否存在于字符串列表|红移查询中,amazon-redshift,Amazon Redshift,我有一些有趣的数据,我试图查询,但我不能得到正确的语法。我有一个临时表temp_id,其中填充了我关心的id值。在本例中,只有两个ID CREATE TEMPORARY TABLE temp_id (id bigint PRIMARY KEY); INSERT INTO temp_id (id) VALUES ( 1 ), ( 2 ); 我在生产中有另一个表,我们称之为foo,它在一个单元格中保存多个ID。ids列如下所示,ids是一个字符串,由| ids ----------- 1|9|3
CREATE TEMPORARY TABLE temp_id (id bigint PRIMARY KEY);
INSERT INTO temp_id (id) VALUES ( 1 ), ( 2 );
我在生产中有另一个表,我们称之为foo,它在一个单元格中保存多个ID。ids列如下所示,ids是一个字符串,由|
ids
-----------
1|9|3|4|5
6|5|6|9|7
NULL
2|5|6|9|7
9|11|12|99
我想计算foo.id中的每个单元格,并查看其中是否有id与temp_id表中的id匹配
预期产量
ids |does_match
-----------------------
1|9|3|4|5 |true
6|5|6|9|7 |false
NULL |false
2|5|6|9|7 |true
9|11|12|99 |false
到目前为止,我已经想出了这个,但我似乎不能返回任何东西。我没有尝试创建一个新列dos_match,而是尝试在WHERE语句中进行筛选。然而,问题是我无法弄清楚如何将temp表中的所有id值计算为foo中充满id的字符串blob
任何建议都会有帮助
干杯,下面的SQL我知道它有点像黑客,它返回的输出正好是您期望的,使用示例数据进行测试,不知道它在实际数据上的表现如何,请尝试告诉我
with seq AS ( # create a sequence CTE to implement postgres' unnest
select 1 as i union all # assuming you have max 10 ids in ids field,
# feel free to modify this part
select 2 union all
select 3 union all
select 4 union all
select 5 union all
select 6 union all
select 7 union all
select 8 union all
select 9 union all
select 10)
select distinct ids,
case # since I can't do a max on a boolean field, used two cases
# for 1s and 0s and converted them to boolean
when max(case
when t.id in (
select split_part(ids,'|',seq.i) as tt
from seq
join foo f on seq.i <= REGEXP_COUNT(ids, '|') + 1
where tt != '' and k.ids = f.ids)
then 1
else 0
end) = 1
then true
else false
end as does_match
from temp_id t, foo
group by 1
请让我知道这是否适合你 下面的SQL我知道它有点像黑客,它返回的输出与您期望的完全一样,使用您的示例数据进行测试,不知道它在您的真实数据上的表现如何,请尝试告诉我
with seq AS ( # create a sequence CTE to implement postgres' unnest
select 1 as i union all # assuming you have max 10 ids in ids field,
# feel free to modify this part
select 2 union all
select 3 union all
select 4 union all
select 5 union all
select 6 union all
select 7 union all
select 8 union all
select 9 union all
select 10)
select distinct ids,
case # since I can't do a max on a boolean field, used two cases
# for 1s and 0s and converted them to boolean
when max(case
when t.id in (
select split_part(ids,'|',seq.i) as tt
from seq
join foo f on seq.i <= REGEXP_COUNT(ids, '|') + 1
where tt != '' and k.ids = f.ids)
then 1
else 0
end) = 1
then true
else false
end as does_match
from temp_id t, foo
group by 1
请让我知道这是否适合你 这会起作用,但对性能没有把握
SELECT
ids
FROM foo
JOIN temp_ids
ON '|'||foo.ids||'|' LIKE '%|'||temp_ids.id::varchar||'|%'
您可以将id列表包装成一对额外的分隔符,以便始终可以搜索| id |,包括第一个和最后一个数字,这将起作用,但对性能不确定
SELECT
ids
FROM foo
JOIN temp_ids
ON '|'||foo.ids||'|' LIKE '%|'||temp_ids.id::varchar||'|%'
您将id列表包装成一对附加的分隔符,以便始终可以搜索| id |,包括第一个和最后一个数字,最终效果良好。性能很慢,但它允许我更改临时表中的ID数量。谢谢在这个查询中,性能会很慢,因为每个ids字段2-like的1-varchar转换总是很慢。这最终会很好地工作。性能很慢,但它允许我更改临时表中的ID数量。谢谢在这个查询中,性能会很慢,因为每个ids字段2-like的1-varchar转换总是很慢。这也很有效,但我最终选择了一个不同的答案,因为它更容易实现。感谢您花时间回答这个问题,效果也很好,但我最终选择了一个不同的答案,因为它更容易实现。感谢您抽出时间回答