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;