在这个问题中,我想到了使用array_position
CREATE Temp TABLE foo (
id integer PRIMARY KEY,
name text[] NOT NULL);
INSERT INTO foo (id, name) VALUES
(1, array[NULL,'c7b74ea7a0ee2924ca6f2fb4f90ac378',NULL]);
INSERT INTO foo (id, name) VALUES
(2, array[NULL,NULL,NULL,NULL,NULL,'4bf4b5c141a361e4eac28b405b31baeb',NULL,'4bf4b5c141a361e4eac28b405b31baeb',NULL]);
select array_position(d.name,(array_agg( x.v) FILTER (WHERE x.v IS NOT NULL))[1])
from foo d
cross join lateral unnest(d.name) as x(v)
group by d.name;
返回正确答案:
| "array_position"|
|:---------------:|
| 2 |
| 6 |
有没有更好的功能可以用于此?
https://dbfiddle.uk/WBUAD_vU