How Can I perform a query that returns a row if I have the wanted values at same index on different columns ? For example, here is some code:
select id_reg, a1, a2 from lb_reg_teste2;
id_reg | a1 | a2
--------+------------------+-------------
1 | {10,10,20,20,10} | {3,2,4,3,6}
(1 row)
The query would be someting like:
select id_reg from lb_reg_teste2 where idx(a1, '20') = idx(a2, '3');
# Should return id_reg = 1
I Found this script , but it only returns the first occurrence of a value in an array. For this case, I need all occurrences.
CREATE OR REPLACE FUNCTION idx(anyarray, anyelement)
RETURNS int AS
$$
SELECT i FROM (
SELECT generate_series(array_lower($1,1),array_upper($1,1))
) g(i)
WHERE $1[i] = $2
LIMIT 1;
$$ LANGUAGE sql IMMUTABLE;