简体   繁体   English

在Azure Cosmos DB中是否有子句? 如何使用它?

[英]Is there having clause in azure cosmos db? How to use it?

I was trying to write a query which is finding MAX value from all documents. 我试图编写一个查询,从所有文档中查找MAX值。 The scenario is something like I have 100 Students Documents, in which student Name, roll number as well as array of Tests inside that array of Subject and its respective marks. 情况类似于我有100个学生文档,其中学生姓名,卷号以及该科目数组及其相应分数内的测试数组。 So, I am getting highest marks among subject physics from all documents. 因此,我从所有文档中都获得了主题物理学的最高分。 But I am not getting it with student roll number. 但是我没有学生卷号。 That I was trying to find out. 我试图找出答案。

TestDoc is:

    Student[
    StudenName:"A",
    StudentRollNo :1,
    id:"1",
    StudentAdd:"---",
    Test1:[
    {
      SubName:"S1",
      Marks:20
    },
    {
      SubName:"S2",
      Marks:30
    },
    ...

    ],
    Test2:
    [
     Same as above
    ],         
    ],

    [
    STUDENT2
    ] ,

and so on

Query I am using is: select MAX(s.Marks) from c join test in c.Test1 join s in test.marks 我正在使用的查询是:从c.Test1中的c join test中选择c(Test.marks)中的MAX(s.Marks)

According to your description, you want to implement function like GROUP BY in azure cosmosdb queries. 根据您的描述,您想在天蓝色的cosmosdb查询中实现类似GROUP BY功能。

Per my experience, azure cosmosdb aggregation capability in SQL limited to COUNT, SUM, MIN, MAX, AVG functions. 根据我的经验,SQL中的azure cosmosdb aggregation功能仅限于COUNT, SUM, MIN, MAX, AVG函数。 GROUP BY or other aggregation functionality are not be supported in azure cosmosdb now. 现在, azure cosmosdb中不支持GROUP BY或其他聚合功能。

However, stored procedures or UDF can be used to implement your aggregation requirement. 但是,可以使用stored proceduresUDF来实现您的聚合要求。

You could refer to a great package documentdb-lumenize based on DocumentDb stored procedure. 您可以基于DocumentDb存储过程引用一个很棒的包documentdb-lumenize

For your first scenario in your post,I created two student documents in my azure cosmosdb account. 对于您帖子中的第一个场景,我在azure cosmosdb帐户中创建了两个学生文档。

[
  {
    "id": "1",
    "StudenName": "A",
    "StudentRollNo": 1,
    "Test": [
      {
        "SubName": "S1",
        "Marks": 20
      },
      {
        "SubName": "S2",
        "Marks": 30
      }
    ],
  },
  {
    "id": "2",
    "StudenName": "B",
    "StudentRollNo": 2,
    "Test": [
      {
        "SubName": "S1",
        "Marks": 10
      },
      {
        "SubName": "S2",
        "Marks": 40
      }
    ],
  }
]

then I put the resultset searched by SQL below to the documentdb-lumenize mentioned above to get the max S2 mark. 然后将下面通过SQL搜索的结果集documentdb-lumenize上面提到的documentdb-lumenize中,以获得max S2标记。

SELECT  c.StudentRollNo,test1.Marks as mark FROM c
join test1 in  c.Test
where test1.SubName='S2'

在此处输入图片说明

For your second scenario in your comment,I removed the where clause of the SQL above. 对于您评论中的第二种情况,我删除了上面的SQL的where clause

SELECT  c.StudentRollNo,test1.Marks as mark FROM c
join test1 in  c.Test

and resultset like: resultset如:

在此处输入图片说明

This applies only to one test.If you want to query multiple tests, you could use stored procedure . 这仅适用于one测试。如果要查询multiple测试,则可以使用stored procedure

You could also refer to SO threads below: 您还可以在下面引用SO线程:

1. Azure DocumentDB - Group By Aggregates 1. Azure DocumentDB-汇总分组

2. Grouping by a field in DocumentDB 2. 按DocumentDB中的字段分组

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM