Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fbLya-0005kL-Fm for pgsql-general@arkaria.postgresql.org; Fri, 06 Jul 2018 08:20:24 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fbLyY-0006Gi-Ao for pgsql-general@arkaria.postgresql.org; Fri, 06 Jul 2018 08:20:22 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fbLyY-0006GR-24 for pgsql-general@lists.postgresql.org; Fri, 06 Jul 2018 08:20:22 +0000 Received: from aragorn.net-wizard.org ([138.201.127.29] helo=gandalf.net-wizard.org) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fbLyU-0002Dt-MS for pgsql-general@postgresql.org; Fri, 06 Jul 2018 08:20:21 +0000 Received: from localhost (localhost [127.0.0.1]) by mail.net-wizard.org (Postfix) with ESMTP id 38FE480AAA; Fri, 6 Jul 2018 10:20:15 +0200 (CEST) X-Virus-Scanned: Gentoo amavisd-new at aragorn Received: from aragorn.net-wizard.org ([127.0.0.1]) by localhost (aragorn.net-wizard.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 6D60mjhT0uPF; Fri, 6 Jul 2018 10:20:08 +0200 (CEST) Received: from MTEC0211 (mtec-lee-dock-2-133.ethz.ch [129.132.6.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-SHA384 (256/256 bits)) (No client certificate requested) (Authenticated sender: clavadetscher@swisspug.org) by gandalf.net-wizard.org (Postfix) with ESMTPSA id 6754480AD0; Fri, 6 Jul 2018 10:20:04 +0200 (CEST) From: "Charles Clavadetscher" To: "'Brahmam Eswar'" Cc: "'pgsql-general'" References: In-Reply-To: Subject: RE: How to remove elements from array . Date: Fri, 6 Jul 2018 10:19:57 +0200 Message-ID: <057501d41502$26aac270$74004750$@swisspug.org> MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_NextPart_000_0576_01D41512.EA35DC60" X-Mailer: Microsoft Outlook 15.0 Thread-Index: AQEjXCTNLm9g7Pk7uZHIvhkF3gtBNqXjS4fg Content-Language: de-ch List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk This is a multipart message in MIME format. ------=_NextPart_000_0576_01D41512.EA35DC60 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Hi =20 From: Brahmam Eswar [mailto:brahmam1234@gmail.com]=20 Sent: Freitag, 6. Juli 2018 09:50 To: pgsql-general ; = pgsql-hackers@postgresql.org Subject: How to remove elements from array . =20 Hi , =20 I tried to use array_remove to remove elements from an array but it's = saying function doesn't exist . I'm able to use other array functions. =20 1) Capture the results with multiple columns into array .=20 2) if ay results exist then loop through an array to find out the = record with col1=3D'Y' 3) If col1=3D'Y' then get the respective value of Col2 (10) and delete = the similar records of col2 if exist. =20 Col1 Col2=20 Y 10 N 20 N 10=20 =20 Need to delete record1 and record3.To delete the array records i'm using = array_remove but it says doesn't exist. =20 Version pgadmin4 . =20 =20 =20 =20 =20 =20 Snippet :- =20 CREATE or REPLACE FUNCTION FUNC1 ( << List of elements >> ) AS $$ =20 DECLARE =20 TEST_CODES record1 ARRAY; TEMP_REF_VALUE VARCHAR(4000); =20 BEGIN IS_VALID :=3D 'S'; =20 SELECT ARRAY=20 (SELECT ROW(Col1,Col2,COl3,Col4) ::record1 FROM table1 INTO TEST_CODES =20 IF array_length(TEST_CODES, 1) > 0 THEN =20 FOR indx IN array_lower(TEST_CODES, = 1)..array_upper(TEST_CODES, 1) LOOP IF TEST_CODES[indx].COL1 =3D 'Y' THEN = TEMP_REF_VALUE:=3DTEST_CODES[indx].Col2; TEST_CODES :=3D = array_remove(TEST_CODES,TEMP_REF_VALUE); END IF; END Loop; END IF;=20 =20 --=20 Thanks & Regards, Brahmeswara Rao J. =20 I am not so in clear why you are using arrays in a function for that. A solution with SQL would be: =20 CREATE TABLE tst ( col1 text, col2 integer ); =20 INSERT INTO tst VALUES ('Y', 10), ('N', 20), ('N', 10); =20 SELECT * FROM tst; =20 col1 | col2 ------+------ Y | 10 N | 20 N | 10 (3 rows) =20 DELETE FROM tst t USING (SELECT * FROM tst WHERE col1 =3D 'Y') AS x WHERE t.col2 =3D x.col2; =20 SELECT * FROM tst; =20 col1 | col2 ------+------ N | 20 (1 row) =20 Regards Charles ------=_NextPart_000_0576_01D41512.EA35DC60 Content-Type: text/html; charset="utf-8" Content-Transfer-Encoding: quoted-printable

Hi

 

From:<= /b> Brahmam = Eswar [mailto:brahmam1234@gmail.com]
Sent: Freitag, 6. Juli = 2018 09:50
To: pgsql-general = <pgsql-general@postgresql.org>; = pgsql-hackers@postgresql.org
Subject: How to remove elements = from array .

 

Hi = ,

 

I = tried to use array_remove to remove elements from an array but it's = saying function doesn't exist . I'm able to use other array = functions.

 

1) Capture the results with multiple columns into = array . 

2)  if ay results exist then loop through an = array to find out the record with col1=3D'Y'

3)  If col1=3D'Y' then get the respective value = of Col2 (10) and delete the similar records of col2 if = exist.

 

Col1    = Col2 

 Y  =        10

 N        =  20

N    =       10 

 

Need to delete record1 and record3.To delete the array = records i'm using array_remove but it says doesn't = exist.

 

Version pgadmin4 .

 

 

 

 

 

 

Snippet :-

 

CREATE or REPLACE FUNCTION = FUNC1

(

=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 = << List of elements >>

) AS $$

 

DECLARE

    

=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 = TEST_CODES record1 ARRAY;

    =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 = TEMP_REF_VALUE VARCHAR(4000);

=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2=A0=C2=A0=C2=A0=C2=A0=C2=A0 END = IF;

=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 =    END Loop;

        END = IF; 


 

-- =

Thanks & = Regards,
Brahmeswara Rao J.

 

I am = not so in clear why you are using arrays in a function for = that.

A = solution with SQL would be:

 

CREATE = TABLE tst (

=C2=A0 = col1 text,

=C2=A0 = col2 integer

);

 

INSERT = INTO tst VALUES ('Y', 10), ('N', 20), ('N', 10);

 

SELECT = * FROM tst;

 

col1 = | col2

------+------

= Y=C2=A0=C2=A0=C2=A0 |=C2=A0=C2=A0 10

= N=C2=A0=C2=A0=C2=A0 |=C2=A0=C2=A0 20

= N=C2=A0=C2=A0=C2=A0 |=C2=A0=C2=A0 10

(3 = rows)

 

DELETE = FROM tst t

USING = (SELECT * FROM tst

=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 WHERE col1 =3D = 'Y') AS x

WHERE = t.col2 =3D x.col2;

 

SELECT = * FROM tst;

 

col1 = | col2

------+------

= N=C2=A0=C2=A0=C2=A0 |=C2=A0=C2=A0 20

(1 = row)

 

Regards

Charles

=
------=_NextPart_000_0576_01D41512.EA35DC60--