Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 循环中查询的Postgres并集_Sql_Arrays_Postgresql - Fatal编程技术网

Sql 循环中查询的Postgres并集

Sql 循环中查询的Postgres并集,sql,arrays,postgresql,Sql,Arrays,Postgresql,我有一个有两列的表。让我们给他们打电话 array\u列和text\u列 我正在尝试编写一个查询,以找出从1到10的K中,text\u列中的值在array\u列的前K个元素中出现了多少行 我期待的结果是: k | count ________________ 1 | 70 2 | 85 3 | 90 ... 我确实通过简单地重复查询10次并合并结果获得了这些结果,如下所示: SELECT 1 AS k, count(*) FROM table W

我有一个有两列的表。让我们给他们打电话
array\u列
text\u列

我正在尝试编写一个查询,以找出从1到10的K中,
text\u列中的值在
array\u列的前K个元素中出现了多少行

我期待的结果是:

k    |    count
________________
1    |    70
2    |    85
3    |    90
...
我确实通过简单地重复查询10次并合并结果获得了这些结果,如下所示:

SELECT 1 AS k, count(*) FROM table WHERE array_column[1:1] @> ARRAY[text_column]
UNION ALL
SELECT 2 AS k, count(*) FROM table WHERE array_column[1:2] @> ARRAY[text_column]
UNION ALL
SELECT 3 AS k, count(*) FROM table WHERE array_column[1:3] @> ARRAY[text_column]
...
但这看起来不是正确的方法。如果我想要一个非常大的K范围呢

所以我的问题是,是否可以在循环中执行查询,并统一每个查询的结果?或者,如果这不是解决问题的正确方法,你会怎么做


提前谢谢

可以使用
array\u positions()
返回数组中找到参数的所有位置的数组,例如

select t.*, 
       array_positions(array_column, text_column)
from the_table t;
这将返回不同的结果,但效率更高,因为不需要增加结果的总体大小。只考虑前十个数组元素,只需将一个切片传递给函数:

select t.*, 
       array_positions(array_column[1:10], text_column)
from the_table t;
要将结果限制为仅包含您可以使用的值的行,请执行以下操作:

select t.*, 
       array_positions(array_column[1:10], text_column)
from the_table t
where text_column = any(array_column[1:10]);
要获得所需的结果,可以使用unnest()将其转换为行:

select k, count(*)
from the_table t, unnest(array_positions(array_column[1:10], text_column)) as k
where text_column = any(array_column[1:10])
group by k
order by k;

您可以使用
array\u positions()
,它返回在数组中找到参数的所有位置的数组,例如

select t.*, 
       array_positions(array_column, text_column)
from the_table t;
这将返回不同的结果,但效率更高,因为不需要增加结果的总体大小。只考虑前十个数组元素,只需将一个切片传递给函数:

select t.*, 
       array_positions(array_column[1:10], text_column)
from the_table t;
要将结果限制为仅包含您可以使用的值的行,请执行以下操作:

select t.*, 
       array_positions(array_column[1:10], text_column)
from the_table t
where text_column = any(array_column[1:10]);
要获得所需的结果,可以使用unnest()将其转换为行:

select k, count(*)
from the_table t, unnest(array_positions(array_column[1:10], text_column)) as k
where text_column = any(array_column[1:10])
group by k
order by k;

您可以使用
generate_series
函数生成一个表,其中包含预期行数和预期值,然后在查询中连接到该表,如下所示:

SELECT t.k AS k, count(*) 
FROM table 
--right join ensures that you will get a value of 0 if there are no records meeting the criteria
right join (select generate_series(1,10) as k) t 
 on array_column[1:t.k] @> ARRAY[text_column]
group by t.k

这可能是最接近于使用循环遍历结果,而不使用PL/SQL之类的东西在用户定义的函数中执行实际循环的方法。

您可以使用
generate\u series
函数生成一个表,该表包含预期值的预期行数,然后在查询中连接到该表,像这样:

SELECT t.k AS k, count(*) 
FROM table 
--right join ensures that you will get a value of 0 if there are no records meeting the criteria
right join (select generate_series(1,10) as k) t 
 on array_column[1:t.k] @> ARRAY[text_column]
group by t.k
这可能是最接近于使用循环遍历结果,而不使用PL/SQL之类的东西在用户定义函数中执行实际循环的方法