Fetch Nested Json Value under single Column name using Azure Cosmos Query - azure-cosmosdb

Based on my requirement my project can have multiple tasks, and each tasks can have sub tasks under it.
I need to fetch all the task name for that particular project under one column based on the assignee. Trying to write a cosmos db query for the same. But how to fetch all the sub tasks name as well under one column based on Assignee.
I am not able to do union as well in cosmos db
Below is the json format for the same :
{
"ProjectName": "Abc",
"Tasks": [
{
"TaskName": "Create Customer Profile",
"StartDate": "2022-05-09T00:00:00",
"CompletionDate": "2022-05-10T00:00:00",
"Assignee": "Abdul Saleem",
"Approver": "Akshat Soni",
"SubTasks": [
{
"SubTaskName": "t5",
"StartDate": "2022-05-11T00:00:00",
"CompletionDate": "2022-05-13T00:00:00",
"Assignee": "Abdul Saleem",
"Approver": "Akshat Soni"
}
]
},
{
"TaskName": "Create Customer Profile",
"StartDate": "2022-05-09T00:00:00",
"CompletionDate": "2022-05-10T00:00:00",
"Assignee": "Abdul Saleem",
"Approver": "Akshat Soni",
"SubTasks": [
{
"SubTaskName": "t5",
"StartDate": "2022-05-11T00:00:00",
"CompletionDate": "2022-05-13T00:00:00",
"Assignee": "John",
"Approver": "Akshat Soni"
},
{
"SubTaskName": "t5",
"StartDate": "2022-05-11T00:00:00",
"CompletionDate": "2022-05-13T00:00:00",
"Assignee": "Abdul Saleem",
"Approver": "Akshat Soni"
}
]
}
]
}

The below query worked for me. I am not sure if it is the best way possible, but it worked. I will update it if I optimize the current solution.
SELECT ARRAY_CONCAT(c2.TaskName,c2.SubTaskName,c2.SubTask1Name) TaskName FROM
(SELECT ARRAY(SELECT c.ProjectName,i.PhaseName,tb.TaskName FROM c join i in c.Phases join t in i.Tasks where t.Approver = 'GTS Test') as TaskName
,ARRAY(SELECT c.ProjectName,i.PhaseName,s.SubTaskName as TaskName FROM c join i in c.Phases join t in i.Tasks join s in t.SubTasks where s.Approver = 'GTS Test') as SubTaskName
,ARRAY( SELECT c.ProjectName,s1.SubTask1Name as TaskName FROM c join i in c.Phases join t in i.Tasks join s in t.SubTasks join s1 in s.SubTasks1
where s1.Approver = 'GTS Test') as SubTask1Name FROM c) as c2

Related

Getting a specific item in a sub array and selecting one value from it

I want to get the boardgame rank (value) from this nested array in Cosmos DB.
{
"name": "Alpha",
"statistics": {
"numberOfUserRatingVotes": 4155,
"averageRating": 7.26201,
"baysianAverageRating": 6.71377,
"ratingStandardDeviation": 1.18993,
"ratingMedian": 0,
"rankings": [
{
"id": 1,
"name": "boardgame",
"friendlyName": "Board Game Rank",
"type": "subtype",
"value": 746
},
{
"id": 4664,
"name": "wargames",
"friendlyName": "War Game Rank",
"type": "family",
"value": 140
},
{
"id": 5497,
"name": "strategygames",
"friendlyName": "Strategy Game Rank",
"type": "family",
"value": 434
}
],
"numberOfComments": 1067,
"weight": 2.3386,
"numberOfWeightVotes": 127
},
}
So I want:
{
"name": "Alpha",
"rank": 746
}
Using this query:
SELECT g.name, r
FROM Games g
JOIN r IN g.statistics.rankings
WHERE r.name = 'boardgame'
I get this (so close!):
{
"name": "Alpha",
"r": {
"id": 1,
"name": "boardgame",
"friendlyName": "Board Game Rank",
"type": "subtype",
"value": 746
}
},
But extending the query to this:
SELECT g.name, r.value as rank
FROM Games g
JOIN r IN g.statistics.rankings
WHERE r.name = 'boardgame'
I get this error:
Failed to query item for container Games:
Message: {"errors":[{"severity":"Error","location":{"start":21,"end":26},"code":"SC1001","message":"Syntax error, incorrect syntax near 'value'."}]}
ActivityId: 0a0cb394-2fc3-4a67-b54c-4d02085b6878, Microsoft.Azure.Documents.Common/2.14.0
I don't understand why this doesn't work? I don't understand what the syntax error is. I tried adding square braces but that didn't help. Can some help me understand why I get this error and also how to achieve the output I'm looking for?
This should work,
SELECT g.name, r["value"] as rank
FROM Games g
JOIN r IN g.statistics.rankings
WHERE r.name = 'boardgame'

How to iterate through the array and find sum of all products for all customers using SQL in Azure Cosmos DB -Core SQL API?

{
"Name": "Naveen",
"Age": 28,
"IS_MEMBER": false,
"Products":[
{
"id": 1,
"NAME": "XXXX"
"COST": 5.99
},
{
"id": 2,
"NAME": "XXXX"
"COST": 4.99
}
]
}
What you need is,
SELECT SUM(a.COST) AS SumTotal
FROM a in c.Products
This can be achieved using "Join".
SELECT sum(t.Cost) as SumTotal FROM c join t in c.Products

Filtering on a aggregate function

I am using Azure Cosmos DB and trying to write a query to filter document by Name and version. I am new to Cosmos and it seems the way I'm doing applies the filter per record versus the results themselves. Can anyone tell me the proper way to accomplish this:
select C.*
from c
JOIN (select MAX(c.version) from c where c.name = "test") maxVersion
where maxVersion = c.version
Sample data:
[{"name":"test","verson":1}{"name":"test","verson":2}{"name":"test","verson":3}]
Results:
I get a record back for each version vs the max version. IE I only should get one record back and it's version number should be 3
When you run this SQL:
select c,maxVersion
from c
JOIN (select MAX(c.version) from c where c.name = "test") maxVersion
you will get this document:
{
"c": {
"id": "1",
"name": "test",
"version": 1
},
"maxVersion": {
"$1": 1
}
}
{
"c": {
"id": "2",
"name": "test",
"version": 2
},
"maxVersion": {
"$1": 2
}
},
{
"c": {
"id": "3",
"name": "test",
"version": 3
},
"maxVersion": {
"$1": 3
}
}
Your maxVerson equals to c.version in each document, so you will get multiple documents not one.
According to your requirement, you can try something like this SQL:
SELECT TOP 1 *
FROM c
WHERE c.name = "test"
ORDER BY c.version DESC

as query not supporting in cosmos db

I am trying following code to get details but it is not return what i want .
my query :
var query = $"SELECT catalog.id as vendorguid,catalog.VendorName,industry.Id as industryguid,industry.IdustryId,industry.IdustryName,category.id as categoryguid,category.Name as categoryname,category.Description as categorydescription,Subcategory.Id as subcategoryguid,Subcategory.Name as subcategoryname,Subcategory.Description as subcategorydescription,product.Id as productguid,product.Name as productname,product.CurrentQuantity as productCurrentQuantity,product.Tag as productTag,product.ImageUrl as productImageUrl,product.Unit as productUnit,product.Price as productPrice,product.hasMethodOfPreparation as producthasMethodOfPreparation,product.MethodOfPreparation as productMethodOfPreparation,product.Addons as productAddons FROM catalogjoin industry in catalog.Industy join category in industry.Category join Subcategory in category.Subcategoryjoin product in Subcategory.Product where product.Id = '"+productId+"'";
my service class :
output:
{
"id": "d91af3e6-6aae-4d10-9ba6-6b97ca4cd881",
"vendorName": "string",
"industy": []
}
expected output:
{
"vendorguid": "97392a23-c8c4-4a7f-bffb-7c3807cc40de",
"VendorName": "string",
"industryguid": "f8265ee6-a351-4036-a0ee-10ec4f51ecf4",
"IdustryId": 0,
"IdustryName": "string",
"categoryguid": "ce7edfdf-1608-4a8c-ae38-7ca60c43bc30",
"categoryname": "string",
"categorydescription": "string",
"subcategoryguid": "709f4e6f-1bba-421f-8dc0-7cf067cbc032",
"subcategoryname": "string",
"subcategorydescription": "string",
"productguid": "d24dd340-d7a2-42e5-b84a-1ee72d9840c6",
"productname": "string",
"productCurrentQuantity": 0,
"productTag": "string",
"productImageUrl": [
"string"
],
"productUnit": 0,
"productPrice": 0,
"producthasMethodOfPreparation": true,
"productMethodOfPreparation": [
],
"productAddons": [
]
}
how can i get it anyone help me?
I think the issue is in your joins (Category, SubCategory). Are your sure its always finding a match? If it doesn't find a match or don't have that properties, CosmosDb will not include it in the results set.
Its not like the left join of MSSQL where nulls are included. CosmosDb is more of a full outer join (cross join).

how to use a join in cosmos db to get result

I have below structure
{
"SubscriberId": "a4db02c1-f41b-4ab1-9f3e-83f9a7ccde83",
"Subscription": {
"Type": "Member",
"MaxUsers": "200",
"StartDate": "2018-07-01T00:00:00.0000000Z",
"EndDate": "2019-07-31T00:00:00.0000000Z",
"Families": [
{
"Id": "4042e5dc-ff0e-5cca-d5ee-d96c4522f1db",
"Products": [
{
"Id": "e9313211-ca18-4776-8fea-1d552b6f40d6",
"Price": null
},
{
"Id": "bf3cdaa4-8cbe-42e6-8f6f-20c1210dc4ac",
"Price": null
}
]
}
],
"IsActive": "true"
}
}
so I want to get data in the below structure, how can I use sql to do this?
SubscriberID , Type,Families.Id,Families.Products.Id, Families.Products.Price
Please use sql:
select distinct c.SubscriberId,c.Subscription.Type as Type,
f.Id as FamiliesId,p.Id as ProductsId,p.Price as ProductsPrice
from c
join f in c.Subscription.Families
join p in f.Products
Result:

Resources