I have a below document in couchbase and I wanted to use group by on values on array(locationClusterId) along with someother fileds like effectiveDateTime(epoch millis) and state. Can someone say best way to write down the N1QL query based on a day range provided for effectiveDateTime.
So you essentially want to UNNEST locationClusterId and group by that, as your result is per locationClusterId element rather than by the array itself.
Not familiar with UNNEST but from the quick reading of it, it might be the one I am looking for, one more think can we group based on the UNNEST elements after flattening?
@dh in the above example the doucment structure is like below, if I UNNEST
locationDSL.parameters.locationClusterId as perCluster, how can I refer locationClusterId in my query.
SELECT p.state, MILLIS_TO_UTC(p.effectiveDateTime, '1111-11-11') as day, perCluster.locationClusterId, count(1) as count
FROM prices p
UNNEST locationDSL.parameters.locationClusterId perCluster
WHERE p._class IN ['com.XXX.YYY']
AND perCluster.locationClusterId IN ["XYZ", "ABC"]
AND p.state IN ["APPROVED", "DRAFT","DELETED","SUBMITTED", "AMENDED"]
AND p.effectiveDateTime BETWEEN STR_TO_MILLIS('2024-02-01T00:00:00Z') AND STR_TO_MILLIS('2025-01-01T00:00:00Z')
GROUP BY MILLIS_TO_UTC(p.effectiveDateTime, '1111-11-11'), p.state, perCluster.locationClusterId
...
UNNEST locationDSL.parameters.locationClusterId AS locationClusterId
...
AND locationClusterId IN ["XYZ", "ABC"]
...
GROUP BY locationClusterId, ...
SELECT p.state, day, locationClusterId, COUNT(1) AS count
FROM prices p
UNNEST locationDSL.parameters.locationClusterId AS locationClusterId
LET day = MILLIS_TO_UTC(p.effectiveDateTime, '1111-11-11')
WHERE p._class IN ["com.XXX.YYY"]
AND locationClusterId IN ["XYZ", "ABC"]
AND p.state IN ["APPROVED", "DRAFT","DELETED","SUBMITTED", "AMENDED"]
AND day BETWEEN "2024-02-01" AND "2024-12-31"
GROUP BY day, p.state, locationClusterId;