want to sum inner element with JSON in using N1QLC

2019-03-04 11:52发布

when I run below query

SELECT * FROM myBucket WHERE ANY x IN transactions SATISFIES x.type in [0,4] END;

Result:

{
  "_type": "Company",
  "created": "2015-12-01T18:30:00.000Z",
  "transactions": [
    {
      "amount": "96.5",
      "date": "2016-01-03T18:30:00.000Z",
      "type": 0
    },
    {
      "amount": "483.7",
      "date": "2016-01-10T18:30:00.000Z",
      "type": 0
    }
  ]
}

I get multiple json like this

SELECT sum(transactions[*].amount) FROM Inheritx WHERE ANY x IN transactions SATISFIES x.type in [0,4] END;

Result: [ { "$1": null } ]

Now I want to sum of all this. How can I do it?

1条回答
戒情不戒烟
2楼-- · 2019-03-04 12:40

transactions[*].amount this is return array so first need to user array function

ARRAY_SUM

than use sum like below.

SELECT sum(ARRAY_SUM(transactions[*].amount)) FROM Inheritx WHERE ANY x IN transactions SATISFIES x.type in [0,4] END;
查看更多
登录 后发表回答