Hi,
I’m new to N1QL queries and I’m trying to achieve something specific. Getting some documents sorted by groups.
What I would like is something like this :
"result": {
"Age_minus_5": {
"origin_1": 15,
"origin_2": 6,
"origin_3": 4
},
"Age_plus_5": {
"origin_1": 34,
"origin_2": 10,
"origin_3": 1
}
}
The result of this request groups persons that has an event with a value = “new” (see request below) and sort them first by their ages (<= 5, > 5) and then by their origin and finally count them.
I achieved to do a request like this :
SELECT COUNT(*) as count, age_minus_5, age_plus_5, person.origin
FROM MY_BUCKET event
UNNEST event.values value
JOIN MY_BUCKET person ON KEYS event.personId
WHERE
event.`type` = "EVENT"
AND value.`value` = "new"
GROUP BY
DATE_DIFF_MILLIS(NOW_MILLIS(), person.birthDate, "year") <= 5,
person.origin
LETTING age_minus_5 = DATE_DIFF_MILLIS(NOW_MILLIS(), person.birthDate, "year") <= 5,
age_plus_5 = DATE_DIFF_MILLIS(NOW_MILLIS(), person.birthDate, "year") > 5
But this is how the result of my request looks like :
"results": [
{
"age_minus_5": true,
"age_plus_5": false,
"count": 15,
"origin": "origin_1"
},
{
"age_minus_5": true,
"age_plus_5": false,
"count": 6,
"origin": "origin_2"
},
{
"age_minus_5": true,
"age_plus_5": false,
"count": 4,
"origin": "origin_3"
},
{
"age_minus_5": false,
"age_plus_5": true,
"count": 34,
"origin": "origin_1"
},
{
"age_minus_5": false,
"age_plus_5": true,
"count": 10,
"origin": "origin_2"
} ,
{
"age_minus_5": false,
"age_plus_5": true,
"count": 1,
"origin": "origin_3"
}
Is their a way to achieve what am I trying to do? I think that a hint could be the alias of the GROUP BY clause set by the LETTING clause but I’m not sure what to do with it.