Search code examples
node.jscube.js

How to remove CubeJS default Group By clause from query


CubeJS default applies the Group By clause when we run any query in it.

For example below is my JSON query for employee Cube:

{
  "dimensions": [
    "Employee.date",
    "Employee.state",
    "Employee.id"
  ],
  "timeDimensions": [],
  "filters": []
}

When I run above JSON query in CubeJS it will run below SQL:

SELECT
  `employee`.updatedAt `employee__date`,
  `employee`.state `employee__state`,
  `employee`.EmpId `employee__id`
FROM
  DEMO.Employee AS `employee`
GROUP BY
  1,
  2,
  3
ORDER BY
  1 ASC
LIMIT
  10000

For reference below is Employee Cube that I have :

cube(`Employee`, {
  sql: `SELECT * FROM DEMO.Employee `,
  title: `Employee`,

  measures: {
    count: {
      type: `count`
    }
  },
  dimensions: {
    id: {
      sql: `EmpId`,
      type: `string`
    },
    date: {
      sql: `updatedAt`,
      type: `time`
    },
    state: {
      sql: `state`,
      type: `string`
    }
  }
});

What will be the JSON query that I need to create if I expect below SQL :

SELECT
  `employee`.updatedAt `employee__date`,
  `employee`.state `employee__state`,
  `employee`.EmpId `employee__id`
FROM
  DEMO.Employee AS `employee`
WHERE
  `employee`.updatedAt ` BETWEEN '2019-01-01' AND '2019-05-01'
LIMIT
  10000

OR

Is there any way to remove Group By clause from the SQL query generated by CubeJS itself.


Solution

  • There's ungrouped query property since 0.10.62 for that: https://cube.dev/docs/query-format#query-properties. In your case following query can be used:

    {
      "dimensions": [
        "Employee.date",
        "Employee.state",
        "Employee.id"
      ],
      "timeDimensions": [],
      "filters": [],
      "ungrouped": true
    }