Hi Couchbase Gurus,
I need some help with my N1QL Queries. I had a hard time solving this one.
So, my question are:
I. How can I JOIN documents with multiple reference in other documents ?
II. How can I JOIN or MERGED array of objects in the location and territories properties
calculate “totalSales” with the same “entityId” ?
III. And, to be unique object in each property?
I have 2 types of document product and product-sales-history:
First, the product documents with id as unique identifier.
{
"id": "company::product::0717",
"productCode": "98721",
"name": "MAGNESIUM PLUS VITAMIN D3 180 CAPLETS",
"price": 12.45,
"type": "product"
}
Second, the product-sales-history with id as unique identifier.
{
"id": "company::product-sales-history::00001",
"product": "company::product::0717",
"period": "2020-04-08",
"type": "product-sales-history",
"location": [
{
"entityId": "company::location::0002",
"averageSales": 113.72,
"totalSales": 1300
},
{
"entityId": "company::location::0004",
"averageSales": 233.72,
"totalSales": 300
}
],
"territories": [
{
"entityId": "company::territories::0004",
"averageSales": 4113.72,
"totalSales": 7300
},
{
"entityId": "company::territories::0001",
"averageSales": 2233.72,
"totalSales": 200
}
]
}
another product-sales-history document
{
"id": "company::product-sales-history::00002",
"product": "company::product::0717",
"period": "2020-04-13",
"type": "product-sales-history",
"location": [
{
"entityId": "company::location::0001",
"averageSales": 113.72,
"totalSales": 1300
},
{
"entityId": "company::location::0002",
"averageSales": 233.72,
"totalSales": 300
}
],
"territories": [
{
"entityId": "company::territories::0001",
"averageSales": 4113.72,
"totalSales": 7300
},
{
"entityId": "company::territories::0002",
"averageSales": 2233.72,
"totalSales": 200
}
]
}
My expected result would be:
{
"id": "company::product::0717",
"productCode": "98721",
"name": "MAGNESIUM PLUS VITAMIN D3 180 CAPLETS",
"price": 12.45,
"type": "product",
"location": [
{
"entityId": "company::location::0002",
"averageSales": 347.44,
"totalSales": 1600
},
{
"entityId": "company::location::0004",
"averageSales": 233.72,
"totalSales": 300
},
{
"entityId": "company::location::0001",
"averageSales": 113.72,
"totalSales": 1300
}
],
"territories": [
{
"entityId": "company::territories::0004",
"averageSales": 4113.72,
"totalSales": 7300
},
{
"entityId": "company::territories::0001",
"averageSales": 6347.44,
"totalSales": 7500
},
{
"entityId": "company::territories::0002",
"averageSales": 2233.72,
"totalSales": 200
}
]
}
The values for “location” and “territories” property should be unique and totalSales are already sum-up.
Thank you,
thank you