Re: How to remove an item from integer array type

Поиск
Список
Период
Сортировка
От Ian Lawrence Barwick
Тема Re: How to remove an item from integer array type
Дата
Msg-id CAB8KJ=iJ88DFM95E6pEPkqWaH6Suq1j2eXhN9MEe32t4BuCgPg@mail.gmail.com
обсуждение исходный текст
Ответ на Re: How to remove an item from integer array type  (Russell Keane <Russell.Keane@inps.co.uk>)
Ответы Re: How to remove an item from integer array type  (Russell Keane <Russell.Keane@inps.co.uk>)
Список pgsql-general
2013/2/21 Russell Keane <Russell.Keane@inps.co.uk>
>
>
>
> >>Sorry,
>
> >>
>
> >>It's not ordered by value. It's not sorted list unfortunately. It can be '{100, 120, 102, 130, 104}'.
>
> >>
>
> >>Do you have other suggestion?
>
> >>
>
> >>Thank you,
>
> >>Choon Park
>
> >
>
> >This should work:
>
> >update a set f2 = (select array_agg(X.id) from (select f1, unnest(f2) id from a where f1 = 1 except select 1 f1, 101
id)X) where f1 = 1; 
>
>
>
> And with the correct table name:
>
> update tableA set f2 = (select array_agg(X.id) from (select f1, unnest(f2) id from tableA where f1 = 1 except select
1f1, 101 id) X) where f1 = 1; 

I don't think that will work, except accidentally;

testdb=# CREATE TABLE tablea(f1 int, f2 int[]);
CREATE TABLE
testdb=# INSERT INTO tablea values(1, '{100,101,102,103,99,104,22}');
INSERT 0 1
testdb=# BEGIN ;
BEGIN
testdb=*# update tableA set f2 = (select array_agg(X.id) from (select
f1, unnest(f2) id from tablea where f1 = 1 except select 1 f1, 101 id)
x) where f1=1;
UPDATE 1
testdb=*# SELECT * from tablea ;
 f1 |           f2
----+-------------------------
  1 | {100,22,103,99,104,102}
(1 row)

testdb=*# ROLLBACK ;
ROLLBACK

Moving the exclusion operation up a level seems to do the trick:

testdb=# BEGIN ;
BEGIN
testdb=*# update tableA set f2 = (select array_agg(X.id) from (select
f1, unnest(f2) id from tablea where f1 = 1) x WHERE x.id != 101) where
f1=1;
UPDATE 1
testdb=*# SELECT * from tablea ;
 f1 |           f2
----+-------------------------
  1 | {100,102,103,99,104,22}
(1 row)

(It's a bit late where  I am so I might be overlooking something)


Ian Barwick

В списке pgsql-general по дате отправления:

Предыдущее
От: ChoonSoo Park
Дата:
Сообщение: Re: How to remove an item from integer array type
Следующее
От: Christian Schröder
Дата:
Сообщение: Re: Perl function leading to out of memory error