how to use a join in cosmos db to get result - azure-cosmosdb

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:

Related

Best way to retrieve document with nested JSON and limit

Suppose we have a structure:
{
"nested_items": [
{
"nested_sample0": "1",
"nested_sample1": "test",
"nested_sample2": "test",
"nested_sample3": {
"type": "type"
},
"nested_sample": null
},
{
"nested_sample0": "1",
"nested_sample1": "test",
"nested_sample2": "test",
"nested_sample3": {
"type": "type"
},
"nested_sample1": null
},
...
],
"sample1": 1233,
"id": "ed68ca34-6b59-4687-a557-bdefc9ec2f4b",
"sample2": "",
"sample3": "test",
"sample4": "test",
"_ts": 1656503348
}
I want to retrieve documents by id by with limit of "nested_items" field .As I know limit and offset not supported in sub queries. Any way to do this except of divide into two queries? Maybe some udf or else?
You can use the function ARRAY_SLICE assuming the array is ordered.
Example data:
{
"name": "John",
"details": [
{
"id": 1
},
{
"id": 2
},
{
"id": 3
}
]
}
Example queries
-- First 2 items from nested array
SELECT c.name, ARRAY_SLICE(c.details, 0, 2) as details
FROM c
-- Last 2 items from nested array
SELECT c.name, ARRAY_SLICE(c.details, ARRAY_LENGTH(c.details) - 2, 2) as details
FROM c

given the same sub key of a nested dictionary, subtract corresponding keys

I have a dictionary dct with two sets set1 and set2 of different lengths.
dct ={
"id": "1234",
"set1": [
{
"sub_id": "1234a",
"details": [
{
"sum": "10",
"label": "pattern1"
}
],
},
{
"sub_id": "1234b",
"details": [
{
"sum": "10",
"label": "pattern3"
}
],
}
],
"set2": [
{
"sub_id": "3463a",
"details": [
{
"sum": "20",
"label": "pattern1"
}
],
},
{
"sub_id": "3463b",
"details": [
{
"sum": "100",
"label": "pattern2"
}
],
},
{
"sub_id": "3463c",
"details": [
{
"sum": "100",
"label": "pattern3"
}
],
}
]
}
I need to check for each label if the corresponding sum has changed, and if yes, subtract these.
pairs1=[]
pairs2=[]
for d in dct['set1']:
for dd in d['details']:
pairs1.append((dd['label'],dd['sum']))
for d in dct['set2']:
for dd in d['details']:
pairs2.append((dd['label'],dd['sum']))
result={}
for p in pairs1:
for pp in pairs2:
if p[0] == pp[0]:
result[p[0]]= int(pp[1])-int(p[1])
result
Output something like:
{'pattern1': 10, 'pattern3': 90}
Is there a better way to iterate through the nested dictionary?

CosmosDB, help flatten an array

I'm trying to flatten and filter my json data that is in a CosmosDB. The data looks like below and I would like to flatten everything in the array Variables. I need to show the id, amount and the array of orderidentifiers( only the orderId field)
my json:
{
"id": "b71687be180da8116208cbb9a40e7e5e630e6cd595f3e09040a155978a2169f3",
"amount": 5354.39,
"orderIdentifiers": [
{
"orderId": "16520328183646646587",
"itemIds": [
"90420839-2769-3acc-a686-3171386190a7"
]
},
{
"orderId": "45288779686596595261",
"itemIds": [
"fb662e41-1c7e-3f36-8cfd-ef2f3c7f0752",
"0b97371c-4eb9-3ec5-8ab9-bb65a9c9efe1"
]
},
{
"orderId": "859986489484974993023",
"itemIds": [
"5c16bb0d-f1a4-3289-bda5-28d0b09a2a56",
"ffb5dc2a-6e43-321d-97cb-3279ddfd1e39",
"781953ad-83e0-30da-a563-e69ed2a752c6",
"1936e885-c41a-3bd1-a3e0-80e7e3089fe4"
]
},
{
"orderId": "80455639909013091834",
"itemIds": [
"147d4a11-6c05-3fe3-8e4c-bcda3d238845",
"6c586585-6355-393d-a6f4-6fa6c665f3b8"
]
}
]
}
when I run this query :
select c.id, c.amount, oi.orderId
from finalcategorysales c
JOIN oi IN c.orderIdentifiers
where c.id='b71687be180da8116208cbb9a40e7e5e630e6cd595f3e09040a155978a2169f3'
the result is showing me only one element in orderIdentifiers array
{ "id": "b71687be180da8116208cbb9a40e7e5e630e6cd595f3e09040a155978a2169f3",
"amount": 5354.39,
"orderId": "16520328183646646587"
},
You can try something like this SQL:
SELECT
c.id, c.amount, Array(SELECT oi.orderId FROM c JOIN oi IN c.orderIdentifiers) AS orderIds
FROM c
WHERE c.id='b71687be180da8116208cbb9a40e7e5e630e6cd595f3e09040a155978a2169f3'
Result:
[
{
"id": "b71687be180da8116208cbb9a40e7e5e630e6cd595f3e09040a155978a2169f3",
"amount": 5354.39,
"orderIds": [
{
"orderId": "16520328183646646587"
},
{
"orderId": "45288779686596595261"
},
{
"orderId": "859986489484974993023"
},
{
"orderId": "80455639909013091834"
}
]
}
]

How to check more then two id at a time

I have been create a catalog service like blow json pattern.
Json pattern :
{
"id": "b01ee924-78d3-4f3a-9568-5ee80cbad7a7",
"VendorName": "string",
"Industy": [
{
"Id": "0350ac6c-ca15-4a1e-9211-ad078fbf443c",
"IdustryId": 0,
"IdustryName": "string",
"Category": [
{
"id": "a7b71770-9daf-4b67-b471-0a8390843544",
"Name": "string",
"Description": "string",
"Subcategory": [
{
"id": "76a6ead4-9f4d-4d6e-9c30-70938f088ea3",
"Name": "string",
"Description": "string",
"Product": [
{
"Id": "abf95277-ccbc-4f9d-aeda-b6cc9c99953b",
"Name": "string",
"CurrentQuantity": 0,
"Tag": "string",
"Unit": "string",
"Price": 0,
"hasMethodOfPreparation": true,
"MethodOfPreparation": [
{
"id": "a78cb9ea-276f-494b-840a-6eab5e7d8f4b",
"Description": "string",
"Price": 0
}
],
"Addons": [
{
"id": "bdf97be3-5dd1-49e9-bdec-7ac0d3288adb",
"Description": "one",
"Price": 0
},
{
"id": "8f03d2e2-be1f-446d-b943-be9b8fe8ec4c",
"Description": "new add",
"Price": 0
I query the data like blow
query:
SELECT product FROM catalog
join industry in catalog.Industy
join category in industry.Category
join product in category.Subcategory.Product
where catalog.id ='" + itemId + "'
actual result:For a specific vendor ,industry ,category,sub category i need to get and create product.
note: here more than one indutry,category,sub category .
{
"Id": "abf95277-ccbc-4f9d-aeda-b6cc9c99953b",
"Name": "string",
"CurrentQuantity": 0,
"Tag": "string",
"Unit": "string",
"Price": 0,
"hasMethodOfPreparation": true,
"MethodOfPreparation": [
{
"id": "a78cb9ea-276f-494b-840a-6eab5e7d8f4b",
"Description": "string",
"Price": 0
}
but i need to check the industry id ,category id and sub category id.
How to do that,
Please give me suggestion. thanks in advance .
Please use sql:
SELECT product FROM c
join industry in c.Industy
join category in industry.Category
join Subcategory in category.Subcategory
join product in Subcategory.Product
where industry.Id ='<your item id>'
and category.Id = '<your item id>'
and Subcategory.Id = '<your item id>'
Output:

Filtering objects in jq by existence of nested array values

Given a document like this:
[
{
"KVs" : [
{
"Key": "animal",
"Value": "lion"
},
{
"Key": "mascot",
"Value": "lion"
}
],
"name": "roger"
},
{
"KVs" : [
{
"Key": "animal",
"Value": "zebra"
},
{
"Key": "mascot",
"Value": "lion"
}
],
"name": "linda"
}
]
I want to use jq to select only those elements of the top array that contain the KV pair animal == "lion".
The output for the above JSON document should be:
{
"KVs" : [
{
"Key": "animal",
"Value": "lion"
},
{
"Key": "mascot",
"Value": "lion"
}
],
"name": "roger"
}
Can't figure out how to accomplish this with select(). I know how to use it to select based on one specific field. e.g. by key name: .[] | select(.KVs[].Key == "animal"), right? But how do I tell it to match the same KV object on two fields (Key & Value)?
NM, solved it with the help of jqplay and some trial and error.
This is the solution:
.[] | select(.KVs[] | .Key == "animal" and .Value == "lion")
(jqplay permalink)

Resources