使用N1QL为日期范围中的每一天选择多个计数

时间:2017-05-09 18:21:58

标签: couchbase n1ql

我试图弄清楚如何使用Couchbase N1QL查询返回特定日期范围内给定日期的文档总数,并将每个日期作为一行返回,第二列包含总计数。

示例文档

{
  "userPk": "43da6438-5a17-4b95-b9cb-993788677675",
  "value": "6f916dba-6fa1-42b0-8816-c1e78bdabce5",
  "type": 3,
  "date": "2017-03-14T12:15:05.4407826-05:00",
  "userAgentString": "Mozilla/5.0 (Windows NT 6.1; Trident/7.0; rv:11.0) like Gecko",
}

所以,基本上我希望运行一个与此相同的查询:

SELECT COUNT(*) FROM bucket1 WHERE `value` = "6f916dba-6fa1-42b0-8816-c1e78bdabce5" AND date = "2017-03-14";

但是为期七天,结果看起来像这样:

Date       | Total
------------------
2017-03-14 | 1300
2017-03-13 | 1000
2017-03-12 | 1200
2017-03-11 | 1100
2017-03-10 | 1300
2017-03-09 | 1100
2017-03-08 | 1300

这是N1QL目前能做的吗?

1 个答案:

答案 0 :(得分:0)

比我想象的要容易......

SELECT SUBSTR(date,0,10) As Date, COUNT(1) As Total FROM bucket1 WHERE value= "6f916dba-6fa1-42b0-8816-c1e78bdabce5" AND date BETWEEN "2017-03-01T12:15:05.4407826-05:00" AND "2017-03-31T12:15:05.4407826-05:00" GROUP BY SUBSTR(date,0,10) ;