首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Oracle JSON_ARRAYAGG基于ORDER /Rownum

Oracle JSON_ARRAYAGG基于ORDER /Rownum
EN

Stack Overflow用户
提问于 2020-07-26 22:40:22
回答 1查看 2.9K关注 0票数 3

我使用Oracle19c和JSON_ARRAYAGG函数(带有JSON_OBJECT)返回一个连接的JSON对象数组字符串。我需要根据SENT_DATE DESC的顺序将结果限制在前10位对象上。

请注意,JSON_ARRAYAGG有自己的ORDER BY,所以我把它放在这里。然而,是否有限制设施?

以下内容在语法上是正确的,但结果是不正确的。我的JSON对象在连接字符串中没有按SENT_DATE DESC顺序排列。

代码语言:javascript
复制
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)  
                                 ORDER BY mh.sent_date DESC) /*ORDER BY inside json_arrayagg)*/
                                                             /*Normally this works, but not with ROWNUM*/
    from mail_history_t mh 
    where mh.plan_id = 763 and mh.is_current_status = 'Y' and rownum <= 10; /*ROWNUM outside*/

我发现如果在通常的行查询中检查最上面的结果是不正确的,

代码语言:javascript
复制
select * from mail_history_t where plan_id = 763 and is_current_status ='Y' order by sent_date desc;       
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-07-26 22:44:28

可以首先使用fetch first行限制子句在子查询中选择前10行,然后在外部查询中进行聚合:

代码语言:javascript
复制
select json_arrayagg(
    json_object(
        'sentDate'    value sent_date, 
        'sentByEmail' value send_by_email,  
        'sentBy'      value sent_by, 
        'sentByName'  value sent_by_name,  
        'sentToEmail' value sendee_email
    )  
    order by sent_date desc
) js_array
from (
    select *
    from mail_history_t
    where plan_id = 763 and  is_current_status = 'Y'
    order by sent_date desc
    fetch first 10 rows only
) t
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63106488

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档