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 1fLTjd-0001QZ-5m for pgsql-general@arkaria.postgresql.org; Wed, 23 May 2018 13:23:21 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fLTjb-00084V-Vs for pgsql-general@arkaria.postgresql.org; Wed, 23 May 2018 13:23:19 +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 1fLTjb-00084L-Mk for pgsql-general@lists.postgresql.org; Wed, 23 May 2018 13:23:19 +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 1fLTjO-0007O3-LK for pgsql-general@postgresql.org; Wed, 23 May 2018 13:23:17 +0000 Received: from localhost (localhost [127.0.0.1]) by mail.net-wizard.org (Postfix) with ESMTP id A0C69808A5; Wed, 23 May 2018 15:23:04 +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 IjuU5rVvAw5x; Wed, 23 May 2018 15:23:02 +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 8EABF80882; Wed, 23 May 2018 15:22:58 +0200 (CEST) From: "Charles Clavadetscher" To: "'a'" <372660931@qq.com>, "'pgsql-general'" References: <069101d3f289$4c5c38f0$e514aad0$@swisspug.org> In-Reply-To: Subject: RE: RE: How do I select composite array element that satisfy specific conditions. Date: Wed, 23 May 2018 15:22:52 +0200 Message-ID: <06e301d3f299$2d2b5910$87820b30$@swisspug.org> MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_NextPart_000_06E4_01D3F2A9.F0B69A10" X-Mailer: Microsoft Outlook 15.0 Thread-Index: AQJ1QK3OvwXexOVyXz86EOLOXtkYiAIFhHR1AktTUKKi2CajQA== 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_06E4_01D3F2A9.F0B69A10 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Hello From: a [mailto:372660931@qq.com] Sent: Mittwoch, 23. Mai 2018 14:23 To: Charles Clavadetscher ; pgsql-general Subject: Re: RE: How do I select composite array element that satisfy specific conditions. Thanks for your reply....... Honestly I do not use java and don't really know json. All I understand is that it is a text format that allow some customization. Java and JSON are not really related. For a simple description of JSON: http://www.json.org However, as long as it can solve my problem, I'm happy to learn it. now I do have a complex structure of data to store. what I'm aiming at is to: 1, orgnize the data so that it has hierarchy and structrues for people to operate. 2, all updates, insertion, will be recorded (including who, when, for what reason and which element changed from what to what). currently I wrote a C trigger to dynamically disassemble the complex structure and compare them one by one and generate a string that printing out every change along with the update user info. since my amount of data are not that big and the trigger is written in C, the final efficient is considerablly accepted. Now my question would be if json would be helpful on creating a relative efficient mechanism on that...... I assume that it is possible and much easier, but this would require more knowledge on the data that you want to pack in the json structure. Besides that, a basic question would be if it even necessary at all to have such a complex structure. In many cases a simpler design is more efficient. The best thing would be to have a look at how JSON works and decide for yourself, if it helps in your case. Creating new types and aggregating them in array sounds like an overkill, but I may be mistaken. For example your original example in JSONB could look like this: [ { "x": 1, "y": 2 }, { "x": 3, "y": 4 } ] In the database: CREATE t (a JSONB); INSERT INTO t VALUES ('[{"x": 1,"y": 2},{"x": 3,"y": 4}]'); SELECT * FROM (SELECT jsonb_array_elements(a) e FROM t) x WHERE x.e->>'x' = '3'; e ------------------ {"x": 3, "y": 4} (1 row) For completeness. The answer to your original question is: SELECT * FROM (SELECT unnest(ay) AS ay FROM b) u WHERE (u.ay).x = 3; ay ------- (3,4) (1 row) Regards Charles ---Original--- From: "Charles Clavadetscher" > Date: Wed, May 23, 2018 19:29 PM To: "'pgsql-general'" >;"'a'"<372660931@qq.com >; Subject: RE: How do I select composite array element that satisfy specific conditions. Hi > -----Original Message----- > From: a [mailto:372660931@qq.com] > Sent: Mittwoch, 23. Mai 2018 11:43 > To: pgsql-general > > Subject: How do I select composite array element that satisfy specific conditions. > > Hi, say if I have composite type and table > > create type A as( > x float8, > y float8 > ); > > create table B( > Ay A[] > ); > > insert into B > values(array[ > (1,2)::A, > (3,4)::A] > ); > > How could I select the element of Ay that satisfy x=3?? > > Thank you so much!! > > Shore I did not really follow this thread, so I am not in clear, why you want to complicate your life that much. You create a custom data type and then use it in an array in a column. A complex hierarchical structure. Why don't you simply use JSON or JSONB? Your example sounds terribly academic very much like a school assignment. Bye Charles ------=_NextPart_000_06E4_01D3F2A9.F0B69A10 Content-Type: text/html; charset="us-ascii" Content-Transfer-Encoding: quoted-printable

Hello

 

From:<= /b> a = [mailto:372660931@qq.com]
Sent: Mittwoch, 23. Mai 2018 = 14:23
To: Charles Clavadetscher = <clavadetscher@swisspug.org>; pgsql-general = <pgsql-general@postgresql.org>
Subject: Re: RE: How do I = select composite array element that satisfy specific = conditions.

 

Thanks = for your reply.......

 

Honestly I do not use java and don't really know json. = All I understand is that it is a text format that allow some = customization.

 

Java = and JSON are not really related. For a simple description of JSON: http://www.json.org

 

However, as long as it can solve my = problem, I'm happy to learn it.

 

now I do have a complex structure of data to store. = what I'm aiming at is to:

 

1, orgnize the data so that it has hierarchy and = structrues for people to operate.

 

2, all updates, insertion, will be recorded (including = who, when, for what reason and which element changed from what to = what).

currently I wrote a = C trigger to dynamically disassemble the complex structure and compare = them one by one and generate a string that printing out every change = along with the update user info.

 

since my amount of data are not that big and the = trigger is written in C, the final efficient is considerablly accepted. = Now my question would be if json would be helpful on creating a relative = efficient mechanism on that......

 

I = assume that it is possible and much easier, but this would require more = knowledge on the data that you want to pack in the json structure. = Besides that, a basic question would be if it even necessary at all to = have such a complex structure. In many cases a simpler design is more = efficient.

 

The = best thing would be to have a look at how JSON works and decide for = yourself, if it helps in your case. Creating new types and aggregating = them in array sounds like an overkill, but I may be = mistaken.

 

For = example your original example in JSONB could look like = this:

 

[

  = {

    "x": = 1,

    "y": = 2

  = },

  = {

    "x": = 3,

    "y": = 4

  = }

]

 

In the = database:

 

CREATE = t (a JSONB);

INSERT = INTO t VALUES ('[{"x": 1,"y": 2},{"x": = 3,"y": 4}]');

SELECT = * FROM (SELECT jsonb_array_elements(a) e FROM t) x WHERE x.e->>'x' = =3D '3';

 

        = e

------------------

= {"x": 3, "y": 4}

(1 = row)

 

For = completeness. The answer to your original question = is:

 

SELECT = * FROM (SELECT unnest(ay) AS ay FROM b) u WHERE (u.ay).x =3D = 3;

 

  = ay

-------

= (3,4)

(1 = row)

 

Regards

Charles

 

---Original---

=

From: "Charles = Clavadetscher"<clavadetscher@swisspug.org= >

Date: Wed, May 23, 2018 19:29 = PM

To: = "'pgsql-general'"<pgsql-general@postgresql.org= >;"'a'"<372660931@qq.com>;

Subject: RE: How do I select composite = array element that satisfy specific = conditions.


Hi

> -----Original = Message-----
> From: a [mailto:372660931@qq.com]
> = Sent: Mittwoch, 23. Mai 2018 11:43
> To: pgsql-general <pgsql-general@postgresql.org= >
> Subject: How do I select composite array element that = satisfy specific conditions.
>
> Hi, say if I have = composite type and table
>
> create type A = as(
>      x = float8,
>      y float8
> = );
>
> create table = B(
>      Ay A[]
> );
> =
> insert into B
> = values(array[
>      = (1,2)::A,
>      (3,4)::A]
> = );
>
> How could I select the element of Ay that satisfy = x=3D3??
>
> Thank you so much!!
>
> = Shore

I did not really follow this thread, so I am not in clear, = why you want to complicate your life that much.
You create a custom = data type and then use it in an array in a column. A complex = hierarchical structure.
Why don't you simply use JSON or JSONB? Your = example sounds terribly academic very much like a school = assignment.

Bye
Charles

= ------=_NextPart_000_06E4_01D3F2A9.F0B69A10--