want to sum inner element with JSON in using N1QLCouchbase

我是研究僧i 提交于 2019-12-31 05:36:54

问题


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:


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;


来源:https://stackoverflow.com/questions/40838443/want-to-sum-inner-element-with-json-in-using-n1qlcouchbase

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!