简体   繁体   English

在子查询中排序

[英]Order By In Subquery

HOW TO GET SUBQUERY DATA ASEC ORDER IN ORACLE DB.如何获取 ORACLE 数据库中的子查询数据 ASEC 顺序。 BECAUSE IN SUBQUERY ORDER BY IS NOT WORKING因为在子查询中 ORDER BY 不起作用

SELECT JSON_OBJECT(

'jobId' VALUE a.job_id,

'change_order' VALUE (SELECT JSON_ARRAYAGG( JSON_OBJECT(

'jobId' VALUE JOB_ID,

'changeNbr' VALUE CHANGE_NBR,

'changeDesc' VALUE CHANGE_DESC,

'status' VALUE CHANGE_STATUS

RETURNING CLOB)

RETURNING CLOB)

from builder_change_order_view

where job_id =${jobId}

AND rownum < 4

ORDER BY change_nbr )

),

This is the sample output, actually, I want an array in an object in ascending order {"change_number":1, "qwerty":[{"samp1": "test1"},{"samp2": "test2"},{"samp3": "test3"}]}.这是样本 output,实际上,我想要一个 object 中的数组,按升序排列 {"change_number":1, "qwerty":[{"samp1": "test1"},{"samp2": "test2"}, {“samp3”:“test3”}]}。 ie, array values in ascending order({"":"", array:[1,2,3,5]})即,数组值按升序排列({"":"", array:[1,2,3,5]})

You should use the JSON_ARRAYAGG 's own ORDER BY clause:您应该使用JSON_ARRAYAGG自己的ORDER BY子句:

JSON_ARRAYAGG(JSON_OBJECT(...) ORDER BY change_nbr)

Note that your ROWNUM filtering doesn't work correctly this way.请注意,您的ROWNUM过滤无法以这种方式正常工作。 It filters by arbitrary ROWNUM prior to ordering, so if you just want to aggregate the top 3 values, you'll have to use a derived table for this:它在排序之前按任意ROWNUM进行过滤,因此如果您只想聚合前 3 个值,则必须为此使用派生表:

SELECT JSON_ARRAY_AGG(JSON_OBJECT(...) ORDER BY change_nbr)
FROM (
  SELECT *
  FROM builder_change_order_view
  WHERE job_id = ${jobId}
  ORDER BY change_nbr
  FETCH FIRST 3 ROWS ONLY
) t

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM