Hi Couchbase Gurus,
Need your help and expertise, is there a way in Analytics - N1Ql query I could GROUP BY and filter result base on the given Array Values?
So, the array values would be:
["Sent Order", "Contact Emailed", "Contact Called"]
My documents:
// user documents:
[
{
"id": "user::0001",
"firstName": "John",
"lastName": "Doe"
},
{
"id": "user::0002",
"firstName": "Lyn",
"lastName": "Max"
},
{
"id": "user::0005",
"firstName": "Garry",
"lastName": "Poe"
}
]
// activity documents:
[
{
"id": "activity::1001",
"user": {
"id": "user::0001",
"name": "John Doe",
"title": "Sales Rep"
},
"activityType": "Sent Order"
},
{
"id": "activity::1002",
"user": {
"id": "user::0002",
"name": "Lyn Max",
"title": "Sales Rep"
},
"activityType": "Sent Order"
},
{
"id": "activity::1003",
"user": {
"id": "user::0001",
"name": "John Doe",
"title": "Sales Rep"
},
"activityType": "Sent Order"
},
{
"id": "activity::1004",
"user": {
"id": "user::0005",
"name": "Garry Poe",
"title": "Sales Rep"
},
"activityType": "Sent Order"
},
{
"id": "activity::1005",
"user": {
"id": "user::0005",
"name": "Garry Poe",
"title": "Sales Rep"
},
"activityType": "Contact Called"
},
{
"id": "activity::1006",
"user": {
"id": "user::0005",
"name": "Garry Poe",
"title": "Sales Rep"
},
"activityType": "Contact Email"
},
{
"id": "activity::1007",
"user": {
"id": "user::0002",
"name": "Lyn Max",
"title": "Sales Rep"
},
"activityType": "Check In"
},
{
"id": "activity::1008",
"user": {
"id": "user::0005",
"name": "Garry Poe",
"title": "Sales Rep"
},
"activityType": "Check In"
}
]
My partial query, but I have no luck with desired result 
SELECT
META(users).id,
users.firstName,
users.lastName,
activitySummary
FROM `company_analytics`.users AS users
LEFT OUTER JOIN (
SELECT
`user`.id AS id,
COUNT(activityType) AS sentOrders
FROM `company_analytics`.activities
WHERE activityType IN ["Sent Order", "Contact Emailed", "Contact Called"]
GROUP BY activityType, `user`.id
) AS activitySummary ON META(users).id = activitySummary.id
WHERE users.`title`.id = 'administrator'
ORDER BY activitySummary.sentOrders DESC
This would be my desired results:
[
{
"id": "user::0001",
"firstName": "John",
"lastName": "Doe",
"activitySummary": {
"sentOrders": 2,
"calls": 0,
"emails": 0
}
},
{
"id": "user::0002",
"firstName": "Lyn",
"lastName": "Max",
"activitySummary": {
"sentOrders": 1,
"calls": 0,
"emails": 0
}
},
{
"id": "user::0005",
"firstName": "Garry",
"lastName": "Poe",
"activitySummary": {
"sentOrders": 1,
"calls": 1,
"emails": 1
}
}
]
So, what ever the the array values given ["Sent Order", "Contact Emailed", "Contact Called"] would be the results in my activitySummary.
really need you help;
Thanks Guys,
