我使用Oracle19c和JSON_ARRAYAGG函数(带有JSON_OBJECT)返回一个连接的JSON对象数组字符串。我需要根据SENT_DATE DESC的顺序将结果限制在前10位对象上。
请注意,JSON_ARRAYAGG有自己的ORDER BY,所以我把它放在这里。然而,是否有限制设施?
以下内容在语法上是正确的,但结果是不正确的。我的JSON对象在连接字符串中没有按SENT_DATE DESC顺序排列。
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*/我发现如果在通常的行查询中检查最上面的结果是不正确的,
select * from mail_history_t where plan_id = 763 and is_current_status ='Y' order by sent_date desc; 发布于 2020-07-26 22:44:28
可以首先使用fetch first行限制子句在子查询中选择前10行,然后在外部查询中进行聚合:
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
) thttps://stackoverflow.com/questions/63106488
复制相似问题