如何通过CubeJS的JSON查询中的度量返回COUNT条记录

时间:2019-12-03 09:57:30

标签: node.js cube.js

我想获取下面的CubeJS JSON查询返回的记录计数。

{
  "measures": [
    "Employee.salaryTotal"
  ],
  "timeDimensions": [
    {
      "dimension": "Employee.createdat"
    }
  ],
  "dimensions": [
    "Employee.isactive"
  ],
  "filters": []
}

此JSON查询在CubeJS中的SQL下面生成:

SELECT
  `employee`.`isActive` `employee__isactive`,
   SUM(salary) `employee__salary_total`
FROM
  DEMO.Employee AS `employee`
GROUP BY
  1
ORDER BY
  2 DESC
LIMIT
  10000

SQL的输出是:

+---------------------+------------------------+
|  employee__isactive | employee__salary_total |
+---------------------+------------------------+
| Y                   |                  17451 |
| N                   |                   1249 |
+---------------------+------------------------+

但是如果我想通过上面的SQL返回记录数怎么办。
例如:

SELECT COUNT(*) FROM 
    (SELECT
      `employee`.`isActive` `employee__isactive`,
      SUM(salary) `employee__salary_total`
    FROM
      DEMO.Employee AS `employee`
    GROUP BY
      1
    ORDER BY
      2 DESC
    LIMIT
      10000) AS EMPSAL

预期结果应如下:

+------------+
| # COUNT(*) |
+------------+
|          2 |
+------------+

1 个答案:

答案 0 :(得分:0)

当前,无法在最终的Cube.js查询之上执行COUNT操作。如果您要实现分页功能,则可以使用offsetlimit查询参数通过limit和行数比较来检查是否存在下一页:{{3} }。