如何在Cube.js过滤器查询中添加OR条件

时间:2019-10-25 11:36:16

标签: node.js cube.js

我想在Cube.js的JSON查询中添加 OR 条件。但是,一旦我在过滤器中添加了一个条件,它总是会在SQL查询中添加 AND 条件。

下面是我正在尝试的JSON查询。

{
  "dimensions": [
    "Employee.name",
    "Employee.company"
  ],
  "timeDimensions": [],
  "measures": [],
  "filters": [
    {
      "dimension": "Employee.company",
      "operator": "contains",
      "values": [
        "soft"
      ]
    },
    {
      "dimension": "Employee.name",
      "operator": "contains",
      "values": [
        "soft"
      ]
    }
  ]
}

它在下面的SQL查询中生成。

SELECT
  `employee`.name `employee__name`,
  `employee`.company `employee__company`
FROM
  DEMO.Employee AS `employee`
WHERE
  `employee`.company LIKE CONCAT('%', 'soft', '%') 
             AND 
  `employee`.name LIKE CONCAT('%', 'soft', '%')
GROUP BY
  1,
  2;

如果要在SQL下生成,对Cube.js的JSON查询是什么

SELECT
  `employee`.name `employee__name`,
  `employee`.company `employee__company`
FROM
  DEMO.Employee AS `employee`
WHERE
  `employee`.company LIKE CONCAT('%', 'soft', '%') 
             OR
  `employee`.name LIKE CONCAT('%', 'soft', '%')
GROUP BY
  1,
  2;

1 个答案:

答案 0 :(得分:0)

尚未提供对逻辑运算符的API支持。同时,有几种解决方法:

  1. 定义模仿 OR 行为的维度。您的情况是
cube(`Employee`, {
 // ...

 dimensions: {
   companyAndName: {
     sql: `CONCAT(${company}, ' ', ${name})`,
     type: `string`
   }
 }
});
  1. 定义细分。这些也可以生成:https://cube.dev/docs/schema-generation
cube(`Employee`, {
  // ...

  segments: {
    soft: {
      sql: `${company} LIKE CONCAT('%', 'soft', '%') OR ${name} LIKE CONCAT('%', 'soft', '%')` 
    }
  }
});