Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.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_Aggregate Functions_Unnest - Fatal编程技术网

Sql 使用其中一个列名从查询中Postgres

Sql 使用其中一个列名从查询中Postgres,sql,arrays,postgresql,aggregate-functions,unnest,Sql,Arrays,Postgresql,Aggregate Functions,Unnest,作为对上一个问题的后续行动: 我有以下疑问: SELECT row_number() OVER (ORDER BY t.id) AS id , t.id AS "RID" , count(DISTINCT a.ord) AS "Matches" FROM tbl t LEFT JOIN ( unnest(array_content) WITH ORDINALITY x(elem, ord) CROSS JOIN LATERAL unnest(s

作为对上一个问题的后续行动:

我有以下疑问:

SELECT row_number() OVER (ORDER BY t.id) AS id
     , t.id AS "RID"
     , count(DISTINCT a.ord) AS "Matches"
FROM   tbl t
LEFT   JOIN (
   unnest(array_content) WITH ORDINALITY x(elem, ord)
   CROSS JOIN LATERAL
   unnest(string_to_array(elem, ',')) txt
   ) a ON t.description ~ a.txt
       OR t.additional_info ~ a.txt
GROUP  BY t.id;
这使我正确匹配,但是现在
array\u content
的值需要是动态的,并且这也是列值之一

假设我使用聚合器函数获取查询中的数组内容,如下所示:

SELECT row_number() OVER (ORDER BY t.id) AS id
     , t.id AS "RID"
     , array_agg(DISTINCT demo_a.element_demo) as array_values
     , count(DISTINCT a.ord) AS "Matches"
     , count(DISTINCT demo_a.ord) AS "Demo_Matches"
FROM   tbl t
LEFT   JOIN (
   unnest(array_values) WITH ORDINALITY x(elem, ord)
   CROSS JOIN LATERAL
   unnest(string_to_array(elem, ',')) txt
   ) a ON t.description ~ a.txt
       OR t.additional_info ~ a.txt
LEFT JOIN (
   unnest("test1","test2"::varchar[]) WITH ORDINALITY x(element_demo, ord)
   CROSS JOIN LATERAL
   unnest(string_to_array(element_demo, ',')) text
   ) demo_a ON i.name ~ demo_a.text
GROUP  BY t.id;
现在,我需要的是获取
array\u值
列来代替unnest部分中定义的array\u内容。可能吗? 目前,它给出了一个未定义列名的例外

目前,它给出了一个未定义列名的例外

这是因为您使用的是不同的列名
a.obj_元素
。在子查询中,我们将列命名为
elem
。(或者你真的想用
txt
?)那么:

目前,它给出了一个未定义列名的例外

这是因为您使用的是不同的列名
a.obj_元素
。在子查询中,我们将列命名为
elem
。(或者你真的想用
txt
?)那么:


@SyedAsadAbbasZaidi:哦,你还重命名了输入列
array\u content
——我猜是错误吧?@SyedAsadAbbasZaidi:哦,你还重命名了输入列
array\u content
——我猜是错误吧?为简单数组
{test”,“test1”}
添加的双unest毫无意义。一个
unnest()
已经取消了所有要取消的测试…而且,两个左连接子查询之间的代理交叉连接几乎肯定是错误的。想想鱼市的例子:但是,欧文,这2个左边的连接有不同的用途。当然,你仍然在搅乱计数。(我现在要睡觉了…)是否可以像上面的问题一样将select查询中定义的列名引用到左连接中为简单数组添加的双unnest
{“test”,“test1”}
没有意义。一个
unnest()
已经取消了所有要取消的测试…而且,两个左连接子查询之间的代理交叉连接几乎肯定是错误的。想想鱼市的例子:但是,欧文,这2个左边的连接有不同的用途。当然,你仍然在搅乱计数。(我现在要睡觉了…)是否可以像上面的问题一样将select查询中定义的列名引用到左连接中
SELECT row_number() OVER (ORDER BY t.id) AS id
     , t.id AS "RID"
     , array_agg(DISTINCT a.elem) AS array_values  -- or a.txt?
     , count(DISTINCT a.ord) AS "Matches"
FROM   tbl t
LEFT   JOIN (
   unnest(array_content) WITH ORDINALITY x(elem, ord)
   CROSS JOIN LATERAL
   unnest(string_to_array(elem, ',')) txt
   ) a ON t.description ~ a.txt
       OR t.additional_info ~ a.txt
GROUP  BY t.id;