在带有order by的sub select中使用jsonèarrayagg会产生错误

hjzp0vay  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(330)

使用oracle 19c:
我有以下查询,其中一个子选择(链接到主选择通过 plans1_.ID )使用 JSON_ARRAYAGG 功能。

select
    ... , /* Other columns... */
    (SELECT
        json_arrayagg(json_object('sentDate' value mh.sent_date,
                                  'sentByEmail' value mh.send_by_email,    
                                  'sentBy' value mh.sent_by,
                                  'sentByName' value mh.sent_by_name,
                                  'sentToEmail' value mh.sendee_email) RETURNING CLOB) 
        from mail_history_t mh 
        where mh.plan_id = plans1_.id and mh.is_current_status = 'Y' 
        /*---This is the problem block: If I remove this ORDER BY the query works---*/
        order by mh.sent_date desc 
    ) as col_33_0_, 
    /* ... */

    from TABLE_T table0_ 
    left outer join PLANS_T plans1_ 
        on table0_.SOME_ID=plans1_.SOME_ID
    where ... /* etc. */

当我有 order by 作为我生活的一部分 select from mail_history_t mh ,我得到错误

00907. 00000 -  "missing right parenthesis"

但当我摆脱 order by 子句,查询工作。另外,如果我要隔离它,子选择本身就可以工作。
我的目标是获得包含满足条件但按排序的列的行的json数组表示 sent_date 描述。

wooyq4lh

wooyq4lh1#

JSON_ARRAYAGG() 接受自己的 ORDER BY 条款:

json_arrayagg(json_object('sentDate' value mh.sent_date,
                          'sentByEmail' value mh.send_by_email,    
                          'sentBy' value mh.sent_by,
                          'sentByName' value mh.sent_by_name,
                          'sentToEmail' value mh.sendee_email
                         ) ORDER BY mh.sent_date desc RETURNING CLOB
              )
``` `ORDER BY` 不建议使用in子查询。

相关问题