简体   繁体   English

使用AQL在arangodb中进行聚合

[英]Aggregation in arangodb using AQL

I'm attempting a fairly basic task in arangodb, using the SUM() aggregate function. 我正在使用SUM()聚合函数在arangodb中尝试一个相当基本的任务。

Here is a working query which returns the right data (though not yet aggregated): 这是一个工作查询,它​​返回正确的数据(虽然尚未汇总):

FOR m IN pkg_spp_RegMem
FILTER m.memberId == "40289"
COLLECT member = m.memberId INTO g
RETURN { "memberId" : member, "amount" : g[*].m[*].items }

This returns the following results: 这将返回以下结果:

[
  {
    "memberId": "40289",
    "amount": [
      [
        {
          "amount": 50,
          "description": "some description"
        }
      ],
      [
        {
          "amount": 50,
          "description": "some description"
        },
        {
          "amount": 500,
          "description": "some description"
        },
        {
          "amount": 0,
          "description": "some description"
        }
      ],
      [
        {
          "amount": 0,
          "description": "some description"
        },
      ]
    ]
  }
]

I am using Collect to group the results because a given memberId may have multiple'RegMem' objects. 我使用Collect对结果进行分组,因为给定的memberId可能有多个'RegMem'对象。 As you can see from the query/results, each object has a list of smaller objects called 'items', with each item having an amount and a description. 从查询/结果中可以看出,每个对象都有一个名为“items”的较小对象列表,每个项目都有一个数量和一个描述。

I want to SUM() the amounts by member. 我希望SUM()按成员金额。 However, adjusting the query like this does not work: 但是,像这样调整查询不起作用:

FOR m IN pkg_spp_RegMem
FILTER m.memberId == "40289"
COLLECT member = m.memberId INTO g
RETURN { "memberId" : member, "amount" : SUM(g[*].m[*].items[*].amount) }

It returns 0 because it apparently can't find a field in the expanded items list called amount. 它返回0,因为它显然无法在名为amount的展开项列表中找到一个字段。

Looking at the results I can sort of understand why: the results are being returned such that items is actually a list, of lists of objects with amount/description. 查看结果我可以理解为什么:返回结果,使得项目实际上是具有金额/描述的对象列表的列表。 But I don't understand how to reference or expand the un-named list correctly to return the amount field values for the SUM() function. 但我不明白如何正确引用或扩展未命名列表以返回SUM()函数的金额字段值。

Ideally the query should return the memberId and total amount, one row per member such that I can remove the filter and execute for all members. 理想情况下,查询应返回memberId和总金额,每个成员一行,以便我可以删除过滤器并为所有成员执行。

Many thanks in advance if you can help! 非常感谢您提供帮助! Martin 马丁

PS I've worked through the AQL tutorial on the arangodb website and checked out the manual but what would really help me is loads more example queries to look through. PS我已经在arangodb网站上完成了AQL教程,并查看了手册,但真正帮助我的是加载更多示例查询来查看。 If anyone knows of a resource like that or wants to share some of their own, 'much obliged. 如果有人知道这样的资源或者想要分享他们自己的资源,那就非常有帮助。 Cheers! 干杯!

Edited: Misread the question the first time. 编辑:第一次误读了这个问题。 The first one can be seen in the edit history, as it also contains some hints: 第一个可以在edit历史中看到,因为它还包含一些提示:

I replicated your data by creating some documents in this format (and some with only one item): 我通过以这种格式创建一些文档来复制您的数据(有些只有一个项目):

{
  "memberId": "40289",
  "items": [
    {
      "amount": 50,
      "description": "some description"
    },
    {
      "amount": 500,
      "description": "some description"
    }
  ]
}

Based on some of those types of documents, your non-summarized query should indeed be looking like this: 基于某些类型的文档,您的非汇总查询应该看起来像这样:

FOR m IN pkg_spp_RegMem
FILTER m.memberId == "40289"
COLLECT member = m.memberId INTO g

RETURN { "memberId" : member, "amount" :  g[*].m[*].items }

The data returned: 返回的数据:

[
  {
    "memberId": "40289",
    "amount": [
      [
        {
          "amount": 50,
          "description": "some description"
        },
        {
          "amount": 0,
          "description": "some description"
        }
      ],
      [
        {
          "amount": 50,
          "description": "some description"
        },
        {
          "amount": 0,
          "description": "some description"
        }
      ],
      [
        {
          "amount": 50,
          "description": "some description"
        }
      ],
      [
        {
          "amount": 50,
          "description": "some description"
        },
        {
          "amount": 500,
          "description": "some description"
        }
      ],
      [
        {
          "amount": 0,
          "description": "some description"
        }
      ],
      [
        {
          "amount": 50,
          "description": "some description"
        },
        {
          "amount": 500,
          "description": "some description"
        }
      ]
    ]
  }
]

Based on the non summarized version, you need to loop through the items of the groups that have been generated by the collect function and do your SUM() there. 基于非汇总版本,您需要遍历由collect函数生成的组的项目并在那里执行SUM() In order to be able to SUM the items you must FLATTEN() them into a single list, before summarizing them. 为了能够对项目进行FLATTEN() ,在汇总它们之前,必须将它们FLATTEN()放入单个列表中。

FOR m IN pkg_spp_RegMem
FILTER m.memberId == "40289"
COLLECT member = m.memberId INTO g

RETURN { "memberId" : member, "amount" :  SUM(
                                              FLATTEN(
                                                       (
                                                         FOR r in g[*].m[*].items
                                                         RETURN r[*].amount
                                                       )
                                                     )
                                             )
       }

This results in: 这导致:

[
  {
    "memberId": "40289",
    "amount": 1250
  }
]

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

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