I have the following SQL that i have created till now -
That gives the following results .
We want to change the query in such a way that Final result can be achived.
This is an online app so please tell us if we need any indexes
Query we have achieved so far -
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 AS lastModified
FROM api_external AS t4
JOIN api_external AS t5 ON t4.uuid = t5.data.partnerAppId
let product = OBJECT_PAIRS(t5.data.productStatus)[*].name
WHERE t4.type = "partnerApp"
AND t4.data.companyId = '0a5a3c4296a645cabb13a5ac10aff541'
AND t5.type = "integration"
AND t5.data.partnerAppId IS NOT NULL
GROUP BY t4.uuid,t5.data.env,product LETTING m = MIN([-t5.createdTs, {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.dcrFlag
,ct3.lastModified
,(
SELECT api_external.data.displayName AS productName
,uuid AS productId,
ct3.productStatus.[uuid] AS status,
ct3.env
FROM api_external USE KEYS(ARRAY "product::" || v FOR v IN OBJECT_NAMES(ct3.productStatus) END)
) AS products
FROM ct3
where ct3.name ='test project to check multi integration' -- filter is to check for one UUID -this doesnt run in prod . this query will run for all uuids for a company
current results -
[
{
"dcrFlag": false,
"description": "",
"id": "3f14d52d133943e8baa4fc9d56765ff5",
"lastModified": 1688080518990, -- this keeps getting updated to latest time stamp in the database
"name": "test project to check multi integration",
"products": [
{
"env": "SANDBOX",
"productId": "lmx@0e87d374ac4824bc2479fb008abcd",
"productName": "COD",
"status": "REQUESTED"
}
]
}
,{
"dcrFlag": false,
"description": "",
"id": "3f14d52d133943e8baa4fc9d56765ff5",
"lastModified": 1688080516950, -- this keeps getting updated to latest time stamp in the database
"name": "test project to check multi integration",
"products": [
{
"env": "PRODUCTION",
"productId": "75e587d374ac4824bc2479fb008a004c", -- requested status for production env
"productName": "WORK product",
"status": "REQUESTED"
}
]
}
,
{
"dcrFlag": false,
"description": "",
"id": "3f14d52d133943e8baa4fc9d56765ff5",
"lastModified": 1688080516420,
"name": "test project to check multi integration",
"products": [
{
"env": "SANDBOX",
"productId": "75e587d374ac4824bc2479fb008a004c",
"productName": "WORK product", -- approved status for sandbox sandbox
"status": "APPROVED"
},
{
"env": "SANDBOX",
"productId": "aed8468f26424196aa69522b9b52160f",
"productName": "Merchants",
"status": "REQUESTED"
}
]
},
{
"dcrFlag": false,
"description": "",
"id": "3f14d52d133943e8baa4fc9d56765ff5",
"lastModified": 1688080516420,
"name": "test project to check multi integration",
"products": [
{
"env": "SANDBOX",
"productId": "75e587d374ac4824bc2479fb008a004c",
"productName": "WORK product",
"status": "REQUESTED"
},
{
"env": "SANDBOX",
"productId": "aed8468f26424196aa69522b9b52160f",
"productName": "Merchants", -- ADDED ANOTHER PRODUCT
"status": "REQUESTED"
}
]
},
{
"dcrFlag": false,
"description": "",
"id": "3f14d52d133943e8baa4fc9d56765ff5",
"lastModified": 1687925437900,
"name": "test project to check multi integration",
"products": [
{
"env": "SANDBOX",
"productId": "75e587d374ac4824bc2479fb008a004c", -- ADDED 1 PRODUCT
"productName": "WORK product",
"status": "REQUESTED"
}
]
}
]
Final result must be -
[
{
"dcrFlag": false, -- will be same for all records of UUID can choose any
"description": "", -- will be same for all records of UUID can choose any
"id": "3f14d52d133943e8baa4fc9d56765ff5", -- need 1 single record for each id ( uuid )
"lastModified": 1688080518990, -- need to be latest modified time for that UUID
"name": "test project to check multi integration", -- will be same for all records of UUID can choose any
"products": [
{
"env": "PRODUCTION",
"productId": "75e587d374ac4824bc2479fb008a004c", -- for a UUID and product if we have both sandbox and production choose production and its status
"productName": "WORK product",
"status": "REQUESTED" -- for a environment if we have requested status and approved status in the above query result choose APPROVED
},
{
"env": "SANDBOX", -- only sandbox entry is there for this product so keep it
"productId": "aed8468f26424196aa69522b9b52160f",
"productName": "Merchants",
"status": "REQUESTED"
},
{
"env": "SANDBOX", -- only sandbox entry is there for a product so keep it
"productId": "lmx@0e87d374ac4824bc2479fb008abcd",
"productName": "COD",
"status": "REQUESTED"
}
]
}
]