Cosmos group by across partitions - azure-cosmosdb

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",
}

Related

Azure CosmosDB, count with subquery

I'm currently using CosmosDB to store some reviews data. I'm trying to retrieve the average rating from a specific date and also the number of registers that have a rating below 3.
The query that I'm using to retrieve the average rating and count the number of registers with ratings below 3 are:
**Ratings**
SELECT avg(c.x_review) as avg_x_review,
avg(c.y_review) as avg_y_review,
{date} as date
FROM c where c.date = {date}
**Criticals (below 3)**
SELECT count(1) FROM c where c.date = {date}
AND (
c.x_review < 3 OR
c.y_review < 3
)
I wanted it to be inside just one query. This data is going to be retrieved by an Azure Function HTTP Trigger, I will use and save the generated JSON from this query in another container.
I've been searching for tips but nothing seems to work, one example of a query that I'm trying to reach is:
SELECT avg(c.x_review) as avg_x_review,
avg(c.y_review) as avg_y_review,
count(SELECT * FROM c where c.date = {date}
AND (
c.x_review < 3 OR
c.y_review < 3
)
) as criticals
FROM c where c.date = {date}
...or something like that where I could have both queries inside one query alone.
I'm expecting to generate a JSON like this:
{
"avg_x_review": 5,
"avg_y_review": 4,
[...]
[...]
"criticals": 0,
"date": "2022-02-28"
}

Firebase Automatically Collected User Property Gender Data - How to Extract the Gender Data

How to query Firebase Automated User Property Gender from Big Query. I used below query, the query is right, but after executing the query, I am getting the result "The query returned no results"
select app_info.id,app_info.version,
event_date,event_name,
param1.value.string_value as category,
param2.value.string_value as action,
param3.value.string_value as label,
param4.value.string_value as Gender,
count(1) as totalevent,count( distinct user_pseudo_id ) as uniqueusers
From `Tablet_*`,
UNNEST(event_params) as param1,
UNNEST(event_params) as param2,
UNNEST(event_params) as param3,
UNNEST(user_properties) as param4
WHERE _TABLE_SUFFIX BETWEEN '20200203' AND '20200203'
and param1.key='category'
and param2.key='action'
and param3.key='label'
and param4.key='Gender'
group by 1, 2, 3, 4, 5,6,7,8
order by totalevent desc
I have tested your query with the public table firebase-public-project.analytics_153293282.events_*, please validate the following:
1 The table suffix values to confirm the available range of dates:
SELECT DISTINCT(_TABLE_SUFFIX) AS RANGE_DATES
FROM `firebase-public-project.analytics_153293282.events_*`
2 If the previous contains data in date '20200203', check your filters:
param2.key='action'
param3.key='label'
param4.key='Gender'
For example, I tried to filter by 'action':
SELECT event_date, param1.key
FROM `firebase-public-project.analytics_153293282.events_*`,
UNNEST(event_params) as param1
where param1.key ='action'
The query returned no results

Can't get top nth records sorted on value in Cosmos DB

I am trying to get the top 5 records ordered on a specific value in Cosmos DB but I am getting stuck at getting the records ordered.
The query is done on the following document:
{
"id": string,
"Compliant": bool,
"DefinitionId": int,
"DefinitionPeriod": string,
"EventDate": date,
"HerdProfileId": int,
"Period": int,
"Value": int
}
What i have tried:
1st try
SELECT TOP 5 cr.HerdProfileId, cr.Compliant, cr.NonCompliant, cr.NullCompliant FROM (
SELECT
c.HerdProfileId,
SUM(comp) as Compliant,
SUM(noncomp) as NonCompliant,
SUM(nullcomp) as NullCompliant
FROM c
JOIN(SELECT VALUE COUNT(c.id) FROM c WHERE c.Compliant = true) comp
JOIN(SELECT VALUE COUNT(c.id) FROM c WHERE c.Compliant = false) noncomp
JOIN(SELECT VALUE COUNT(c.id) FROM c WHERE c.Compliant = null) nullcomp
WHERE c.Period = 201948
GROUP BY c.HerdProfileId) cr
WHERE cr.NonCompliant > 0
ORDER BY cr.NonCompliant
results in: Unsupported ORDER BY clause. ORDER BY item expression could not be mapped to a document path
2nd try:
SELECT TOP 5 cr.HerdProfileId, cr.Compliant, cr.NonCompliant, cr.NullCompliant FROM (
SELECT
c.HerdProfileId,
SUM(comp) as Compliant,
SUM(noncomp) as NonCompliant,
SUM(nullcomp) as NullCompliant
FROM c
JOIN(SELECT VALUE COUNT(c.id) FROM c WHERE c.Compliant = true) comp
JOIN(SELECT VALUE COUNT(c.id) FROM c WHERE c.Compliant = false) noncomp
JOIN(SELECT VALUE COUNT(c.id) FROM c WHERE c.Compliant = null) nullcomp
WHERE c.Period = 201950
GROUP BY c.HerdProfileId
ORDER BY NonCompliant DESC) cr
WHERE cr.NonCompliant > 0
results in: ORDER BY' is not supported in presence of GROUP BY
Is there any way to get the data needed or is this just not possible in Cosmos DB and do I need to order the results in code later on?
The first sql: Order by item expression could not be mapped to a document path. Please refer to the statements in this blog:
The second sql:
Order by can't work with Group By so far,please refer to official statement:
I suppose that you have to follow the suggestions in my previous case:How to group by and order by in cosmos db? you order the results in code so far. Waiting for the plan of above 2rd statement for group by and order by...

ASP.NET MVC 5 Join on Between, no primary key

I have some interesting code to look at.
I have three tables:
Table A has 4 columns:
TablePK
UserID
TableBFKID
Score
Table B has 3 columns:
TablePK
Name
ShortName
Table c has 4 columns:
TablePK
ScoreMin
ScoreMax
Modifier
So when the full join happens it looks like this:
SELECT B.ShortName
, A.Score
, C.Modifier
FROM TableA A
INNER JOIN TableB B ON a.TablePK= B.TablePK
INNER JOIN TableC C ON A.Score BETWEEN C.ScoreMin AND C.ScoreMax
The results would look like this:
ShortName, Score, Modifier. EX:
CHA, 19, 4
Now I know how to do an Entity Framework join if there is an actual PK or FK, or even if there is only a 0:1 relationship.
But how do you do the join when there is neither a PK nor an FK?
LINQ, including LINQ to Entities, only supports equi-joins.
But you can run SQL directly:
var res = myContext.Database.SqlQuery<TResult>("SQL goes here", parmeters...);
and EF will map the columns of the result set to the properties of TResult (which needs to other connection to the context: it does not need to be an entity type with a DbSet typed property in the context).
In this case I wouldn't try to join them, just use a sub-select in your linq to select from the un-joined table where the scores are between your wanted ranges.
var results = context.TableA
.Select(a => new {
score = a.Score, //add all needed columns
tableCs = context.TableC.Where(c => a.Score >= c.ScoreMin && a.Score <= c.ScoreMax)
});

SQLITE Recursive Query

I've found an interesting recursive query here (http://beyondrelational.com/modules/2/blogs/28/posts/10486/recursive-cte-and-ordering-of-the-hierarchical-result.aspx) and I adapted to my needs, but is not working.
Here's the code:
WITH cte AS (
SELECT 0 AS lvl, collectionID, collectionName, parentCollectionID, CAST(collectionID AS VARCHAR(128)) AS Sort
FROM collections WHERE parentCollectionID IS NULL
UNION ALL
SELECT p.lvl + 1, c.collectionID, c.collectionName, c.parentCollectionID, CAST(p.Sort + '/' + CAST(c.collectionID AS VARCHAR) AS VARCHAR(128))
FROM collections c
INNER JOIN cte p ON p.collectionID = c.parentCollectionID
)
SELECT
collectionID,
SPACE(lvl * 4) + collectionName AS collectionName,
Sort,
parentCollectionID
FROM cte ORDER BY Sort;
I receive this message: "Kernel error: near "WITH": syntax error"
Please, what I have to do to fix it?
I'm using SQLIte 3.8.2

Resources