Hi Dear Experts, I am having almost zero experience with Couchbase. I am trying to make a query on the following sample document in Couchbase:
[
{
"bucket": {
"customerid": 123456,
"ticket": [
{
"creationDate": "2023-11-08T07:31:47.077Z",
"caseAction": {
"id": "11",
"type": "Complaint",
"reason": "Any"
},
"status": {
"state": "Active"
},
},
{
"creationDate": "2023-11-07T07:31:47.077Z",
"caseAction": {
"id": "11",
"type": "Complaint",
"reason": "Any"
},
"status": {
"state": "In Progress"
},
},
{
"creationDate": "2023-11-06T07:31:47.077Z",
"caseAction": {
"id": "11",
"type": "Request",
"reason": "Any"
},
"status": {
"state": "Active"
},
},
{
"creationDate": "2023-11-01T09:40:27.215Z",
"caseAction": {
"id": "19",
"name": "Request",
"reason": "no reason"
},
"status": {
"state": "Closed"
},
"statusDate": "2023-11-30T09:57:03.704Z",
"validFor": {
"endDateTime": "2023-11-30T09:57:03.244Z",
"startDateTime": "2023-11-15T09:40:27.212Z"
}
}
]
````Preformatted text`
The equivalent of the query in Oracle or PostgreSQL is like this:
select customer_id,
sum(case
when name = 'Request' and reason = 'Any' then power(2, 0)
when name = 'Complaint' and reason = 'Any' then power(2, 1)
else 0
end) long_ActiveCases
from cases
where state in ('Active','In Progress')
group by customer_id
The output is for example 123456,3
For some reason unnest doesn't work, using ARRAY is working, but here I need to use under 'ticket' two subarrays for caseStatus to check name and reason, then status for state Active or In Progress Then use POWER and CASE, which I checked already do exist in N1QL . But don't have a clue how to combine all of those. In RDBMS it is clear, but here :slightly_smiling_face: