CosmosDb查询复杂结构:子总计

时间:2018-02-25 16:31:41

标签: azure-cosmosdb

在我的收藏中,我有这样的文件

{
    "purchase_order_number": "PO15428132600",
    "total_due": 4893.3929,
    "account_number": "Account2",
    "freight": 183.1626,
    "ttl": 2592000,
    "order_date": "Mon Jul 11 00:00:00 2005",
    "_attachments": "attachments/",
    "subtotal": 6107.082,
    "id": "SalesOrder",
    "_ts": 1519575296,
    "items": [
      {
        "line_price": 5.7,
        "order_qty": 3,
        "currecny_code": "USD",
        "unit_price": 17.1,
        "currency_symbol": "$",
        "product_code": "A-123",
        "product_name": "Product 1"
      },
      {
        "line_price": 4.7,
        "order_qty": 4,
        "currecny_code": "USD",
        "unit_price": 15,
        "currency_symbol": "$",
        "product_code": "A-123",
        "product_name": "Product 1"
      },
      {
        "line_price": 1.7,
        "order_qty": 2,
        "currecny_code": "USD",
        "unit_price": 5.1,
        "currency_symbol": "$",
        "product_code": "A-123",
        "product_name": "Product 1"
      }
    ]
}

我不知道我是否有可能成为Cosmo的初学者(但是关系数据库的背景)

我希望能够进行一个查询,从我的项目中获得总和(unit_price * order_qty),我可以将其放在UDF中

由于

1 个答案:

答案 0 :(得分:0)

您可以使用:

SELECT SUM(item.unit_price * item.order_qty) FROM item IN o.items 

或者,如果您需要过滤订单并对项目求和,请使用JOIN子句展开数组。

SELECT SUM(item.unit_price * item.order_qty) 
FROM order 
JOIN item IN order.items 
WHERE order.purchase_order_number = "PO15428132600"