想要在N1QL中进行数组乘法

时间:2016-11-30 13:33:28

标签: couchbase n1ql

我有JSON,如下所示

{
  "_type": "Usage",
  "bids": [
    {
     "bid": 0.015,
      "clicks": 13,
      "endTime": "2016-06-01T00:31:39.000Z",
     },
    {
      "bid": 0.023,
      "clicks": 1731,
      "endTime": "2016-06-01T03:08:30.000Z",
     },
    {
      "bid": 0.025,
      "clicks": 604,
      "endTime": null,
     }
  ],
  "campaignId": 2229,
}

我想在出价中增加点击次数和出价但是当我在查询下方运行时,其结果为空 ARRAY_SUM((出价[*]。点击次数)*(出价[* ] .bid))

select campaignId,ARRAY_MAX(bids[*].startTime) date,

ARRAY_SUM(bids[*].clicks) clicks,ARRAY_SUM((bids[*].clicks)*(bids[*].bid)) total

from Default where _type="DailyCampaignUsage";

但总计为空

1 个答案:

答案 0 :(得分:2)

您希望将数组中每个元素的单个点击次数/出价值相乘。请注意,出价[]。点击会生成一系列点击值,''是基本/标量数据类型的乘法运算符。所以,试试这个:

SELECT campaignId,ARRAY_MAX(bids[*].endTime) date,
       ARRAY_SUM(bids[*].clicks) clicks, 
       ARRAY_SUM(ARRAY x.clicks * x.bid FOR x IN bids END) total
FROM default
WHERE _type="DailyCampaignUsage";

-prasad