Flatten nested arrays in cosmosdb sql - azure-cosmosdb

I have the following cosmosdb document:
{
id: "id",
outer: [
{
"inner": [ "a", "b", "c" ]
},
{
"inner": [ "d", "e", "f" ]
}
]
}
And I need to create a SQL request which would return all of the combined values of the "inner" arrays, like this:
{
"allInners": [ "a", "b", "c", "d", "e", "f" ]
}
I was able to unwind the first array level using the "IN" operator, but I am not sure how unwind it one more level and to handle double or even triple nested arrays.
The following is my subquery to aggregate those items
SELECT
... other stuff.
ARRAY(SELECT VALUE innerObj.inner FROM innerObj IN c.outer) AS allInners,
...
FROM c

I found the following solution to my problem (using the nested "IN" and a subquery):
ARRAY(
SELECT VALUE inner
FROM inner IN (
SELECT VALUE outers.inner
FROM outers IN c.outer
)
)

Please try something like this sql:
select ARRAY(SELECT VALUE e FROM c join d in c["outer"] join e in d["inner"]) AS allInners from c
Here is result of my test:
Hope this can help you.:)

Related

Cosmos group by across partitions

I'm trying to query a collection to get the count of items and group by two columns; let's call them X and Y. I'm running the query in Microsoft Azure Storage Explorer v1.18.1.
The issue I have is that the query is across partitions in the collection. This is the query and the error below it.
Query:
SELECT COUNT(1) AS ItemCount, c.X, c.Y
FROM (
SELECT c.X, c.Y, c.A
FROM c
WHERE c.Deleted = false AND c.State = 'Open'
) C
GROUP BY c.X, c.Y
Error:
Cross partition query only supports 'VALUE <aggregateFunc>' for aggregates.
Where do I place the VALUE keyword in the query?
Additional information:
The partition key is /ItemId
An example document is
{
"ItemId": "BSD-02930",
"A": "GB2825",
"X": "SAM",
"Y": "WAN",
}

Kusto create an in-memory table for testing

Just looking to create a quick in-memory/temp table for testing out queries. I've seen this done before but I'm having trouble finding any examples from a web search or StackOverflow search. I'm looking for something like this:
let TempTable = table("TestTable",
Column column1 = [1,2,3],
Column comumn2 = ["A","B","C"]
);
Result:
I don't need to save the table in any database, just want to use for testing & demonstration purposes.
you could use the datatable operator: https://learn.microsoft.com/en-us/azure/data-explorer/kusto/query/datatableoperator
for example:
let T = datatable(column1:int, column2:string)
[
1, "A",
2, "B",
3, "C",
];
... do something with T ...

Cosmos Db - search on value from array

Using a query for Cosmos, how do I select out all Roles that have an ID of '11'?
{
"Roles":[
{
"Name":"Admin",
"ID":[
"11",
"22",
"33"
]
},
{
"Name":"User",
"ID":[
"11"
]
}
]
}
I think it sort of looks like this but not sure about the where clause...
select value r from c join r in c.Roles join i in r.ID // where i = ??
Please try something like this:
select value r from c join r in c.Roles where ARRAY_CONTAINS(r.ID,'11',false)
Hope this can help you:).

SQLITE: pull items in the same order as IN array

I have the following query:
SELECT * from tWords where WordAton IN ("bbb", "aaa2", "ccc", "aaa1")
the query returns first the results for "aaa1" then for "aaa2" then for "bbb" and then for "ccc". Is there a way to return the results in the order of the input array, which means first the results for "bbb" then for "aaa2"... etc.
Thank you in advance.
You can apply conditional ordering like this:
SELECT *
from tWords
where WordAton IN ('bbb', 'aaa2', 'ccc', 'aaa1')
order by case WordAton
when 'bbb' then 1
when 'aaa2' then 2
when 'ccc' then 3
when 'aaa1' then 4
end
In SQL (not just SQLite), the only way to always return rows in a given order is with a SQL ORDER BY ... clause. So the short answer is, "No", there's no simple way to return rows in the order given by the contents of an IN (...) clause.
You could use a common table expression (CTE) to define a sort order, but that's usually not worth the trouble. This isn't the same thing as ordering by the contents of a IN (...) clause, but it looks the same. (You're ordering by the sort order specified in the CTE.)
with ordered_words as (
select 1 as sort_order, 'bbb' as WordAton union
select 2, 'aaa2' union
select 3, 'ccc' union
select 4, 'aaa1'
)
select t.WordAton
from tWords t
join ordered_words o on t.WordAton = o.WordAton
where t.WordAton in ('bbb', 'aaa2', 'ccc', 'aaa1')
order by o.sort_order;

How can I re-order the values of a dictionary in python?

I'm attempting to reorder the values of a particular key in a python dictionary.
For instance, if a I have dictionary {'a':"hello","hey","hi"}
When i print the values of 'a', how could I change the order of "hello", "hey", or "hi"?
To rephrase this: the value of your key 'a' in your dictionary is a list containing 3 string-objects.
dict = { 'a': [ "hello", "hey", "hi" ] }
this being the case, you can sort the list like this

Resources