Skip to Main Content

ORDS, SODA & JSON in the Database

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

JSON _ARRAYAGG is ignoring DISTINCT

ifernandMar 22 2018 — edited Mar 28 2018

Hi.

I am facing a weird result from SQL/JSON using JSON_ARRAYAGG. I have found this situation on "Oracle Database 12c Enterprise Edition Release 12.2.0.1.0 - 64bit Production" but I have reproduced same results in LiveSQL with "Oracle Database 18c Enterprise Edition Release 18.0.0.0.0 - Production".

I have created a table with 2 columns and populated with 3 rows:

CREATE table items

(

  item_id          NUMBER(5) not null,

  item             VARCHAR2(6) not null

)

;

insert into items (item_id, item)

values (1, '397850')

;

insert into items (item_id, item)

values (2, '397855')

;

insert into items (item_id, item)

values (3, '397855')

;

Item_id has 3 different values, but item only have 2 unique values. If I look for different values of item I only find 2 values:

SELECT DISTINCT item

  FROM items;

ITEM
397855
397850

If I convert items into JSON objects I have 2 rows/values:

SELECT   JSON_OBJECT(

               'item_obj' IS JSON_OBJECT(

                               'item' IS item))

              AS qry_result

      FROM (

             SELECT DISTINCT item

                FROM items

               );

QRY_RESULT
{"item_obj" : {"item" : "397855"}}
{"item_obj" : {"item" : "397850"}}

But when I try to aggregate this objects as an JSON array I find this:

SELECT JSON_ARRAYAGG( 

             JSON_OBJECT( 

               'item_obj' IS JSON_OBJECT( 

                               'item' IS item)) 

             FORMAT JSON 

             ORDER BY item 

             RETURNING CLOB 

             ) 

              AS qry_result 

      FROM ( 

             SELECT DISTINCT item 

                FROM items

               ) ;

QRY_RESULT

[

{"item_obj" : {"item" : "397850"}},

{"item_obj" : {"item" : "397855"}},

{"item_obj" : {"item" : "397855"}}

]

Can someone explain me why this query is returning 3 JSON objects instead of 2? Have I misunderstood something about JSON_ARRAYAGG? Is this a bug?

Regards.

This post has been answered by Beda Hammerschmidt-Oracle on Mar 26 2018
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 23 2018
Added on Mar 22 2018
3 comments
6,059 views