How do we write the following N1QL

@vsr1
I arrived at below N1QL from all the inputs
please observe the letting clause , for the lowest value of the env we are picking other details. with a change in the application we are getting multiple records for this criteria and we need to pick the one with max(t5.createdTs) . That is we need min(env) record with max time stamp. How do i change this N1QL
I tried using order by before letting it gave me syntax error

WITH ct3 AS ( SELECT m[1].appName as name , m[1].uuid as id ,m[1].description ,m[1].env , m[1].productStatus ,m[1].fourthParty as dcrFlag, 
m[1].createdTs
FROM api_external AS t4
JOIN api_external AS t5 ON t4.uuid = t5.data.partnerAppId
WHERE t4.type = "partnerApp"
       AND t4.data.companyId = '70a149da27cc425da86cba890bf5b143'
       AND t5.type = "integration"
       AND t5.data.partnerAppId IS NOT NULL
GROUP BY t4.uuid
LETTING m = MIN([t5.data.env, {t4.uuid, t4.data.appName, t4.data.description,
                    t5.data.env, t5.data.productStatus , t4.data.fourthParty , t4.uuid,t5.createdTs }]) )
select ct3.name ,
ct3.id , 
ct3.description  ,
ct3.env ,
ct3.dcrFlag,
ct3.createdTs,
(select  api_external.data.displayName as productName ,  uuid as productId
                    from api_external USE KEYS (ARRAY "product::" || v FOR v IN OBJECT_NAMES(ct3.productStatus)  END) ) as ProductDetails
from ct3