想要在 N1QL couchbase 的情况下使用内部元素总和
Want to use inner element sum in case condition in N1QL couchbase
我想运行喜欢下面的查询
SELECT round(sum (ARRAY_SUM(case when ANY x IN transactions SATISFIES
x.type` `in [0,4] then transactions[*].amount else 0 end))),2)
total_income,_type` `FROM mybucket WHERE _type='Company'
我有多个 json 如下
{
"_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
}
]
}
我想要交易总和-> 类型为 [0,1] 的金额我想要它以防万一。我该怎么做??
SELECT CASE WHEN array_count(a) > 0 THEN ARRAY_SUM(a) ELSE 0 END
FROM default
LET a = ARRAY TONUMBER(x.amount) FOR x in transactions WHEN x.type IN [0,4] END
WHERE _type = "Company";
顺便说一句,您可以在 N1QL 中使用不同的语言结构来编写此查询。检查 UNNEST, and array-indexing。特别是,当您对数组元素(例如 transactions[*].type)进行筛选时,您可以利用数组索引来获得更好的性能并将任何筛选下推到 where 子句(和索引)。
我想运行喜欢下面的查询
SELECT round(sum (ARRAY_SUM(case when ANY x IN transactions SATISFIES
x.type` `in [0,4] then transactions[*].amount else 0 end))),2)
total_income,_type` `FROM mybucket WHERE _type='Company'
我有多个 json 如下
{
"_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
}
]
}
我想要交易总和-> 类型为 [0,1] 的金额我想要它以防万一。我该怎么做??
SELECT CASE WHEN array_count(a) > 0 THEN ARRAY_SUM(a) ELSE 0 END
FROM default
LET a = ARRAY TONUMBER(x.amount) FOR x in transactions WHEN x.type IN [0,4] END
WHERE _type = "Company";
顺便说一句,您可以在 N1QL 中使用不同的语言结构来编写此查询。检查 UNNEST, and array-indexing。特别是,当您对数组元素(例如 transactions[*].type)进行筛选时,您可以利用数组索引来获得更好的性能并将任何筛选下推到 where 子句(和索引)。