前の質問のフォローアップとして:
次のクエリがあります。
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_content動的である必要があり、それも列値の1つです。
アグリゲーター関数を使用して、クエリ内の配列コンテンツを次のように取得しているとしましょう。
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_values、unnest 部分で定義されている array_content の代わりに列を取得することです。出来ますか?今のところ、列名が定義されていないという例外を与えています。