Join Cube with condition?
- Dominant language
- Rust
- Stars
- 20.8k
- Forks
- 2.1k
- Avg merge
- 1d 2h
- Merged PRs (30d)
- 181
Description
**Problem**
Joining Data from Multiple Data Sources— Clickhouse and Postgresql — join with condition- time filter. How can I do join data with condition with Cube.js?
Alarms — from Postgresql, Place_alarms_count — from Clickhouse
**Related Cube.js schema**
Alarms
```javascript
cube(`Alarms`, {
sql: `select alarm.id,
alarm.employee_id,
alarm.started,
alarm.ended,
from alarm
where alarm.started between now() - interval '1' month and now()`,
preAggregations: {
alarmsRollup: {
type: `rollup`,
external: true,
measures: [CUBE.count_active, CUBE.count_total],
dimensions: [CUBE.id, CUBE.employee_id, CUBE.started, CUBE.ended],
indexes: {
employeeIndex: {
columns: [CUBE.employee_id]
}
},
timeDimension: CUBE.started,
granularity: `minute`,
},
},
measures: {
count_total: {
type: `count`,
drillMembers: [id],
title: `cubeAlarmsCountTotal`
},
count_active: {
type: `count`,
drillMembers: [id],
filters: [{
sql: `${CUBE}.ended is null`
}],
title: `cubeAlarmsCountActive`
}
},
dimensions: {
id: {
sql: `id`,
type: `number`,
primaryKey: true,
title: `cubeAlarmsId`
},
employee_id: {
sql: `employee_id`,
type: `number`,
title: `cubeAlarmsEmployeeId`
},
started: {
sql: `started::timestamp`,
type: `time`,
title: `cubeAlarmsStarted`,
meta: {
shownfront: true
},
},
ended: {
sql: `ended::timestamp`,
type: 'time',
title: `cubeAlarmsEnded`
},
},
dataSource: `test`
});
```
Place_alarms_count.js
```javascript
cube(`Place_alarms_count`, {
sql: `SELECT employee_id as place_employee_id,
code,
toInt64(data.place_id) AS place_id,
data.place_usage AS place_usage,
data.date_from AS date_from,
data.date_to AS date_to,
data.status AS status,
data.date_to-data.date_from AS duration,
processing_dt,
toDate(processing_dt) AS check_date,
ts
FROM events
WHERE code='place-leaving'
AND length(status) > 0`,
preAggregations: {
psRollup: {
type: `rollup`,
external: true,
dimensions: [CUBE.place_id, CUBE.id, CUBE.place_employee_id, CUBE.date_from, CUBE.date_to],
indexes: {
categoryIndex: {
columns: [CUBE.place_employee_id, CUBE.date_from],
}
},
},
combinedRollup: {
type: `rollupJoin`,
external: true,
measures: [Alarms.count_active, Alarms.count_total],
dimensions: [
CUBE.place_id, CUBE.id,CUBE.place_employee_id,CUBE.date_from, CUBE.date_to,
Alarms.alarm_name, Alarms.employee_id, Alarms.alarm_criticality,
Alarms.started, Alarms.ended
],
rollups: [CUBE.psRollup, Alarms.alarmsRollup, ],
}
},
joins: {
Alarms:{
relationship: `belongsTo`,
sql: `${CUBE.place_employee_id} = ${Alarms.employee_id}`,
},
},
dimensions: {
id: {
sql: `ts`,
type: `number`,
primaryKey: true,
},
place_id: {
sql: `place_id`,
type: `number`,
},
place_employee_id: {
sql: `place_employee_id`,
type: `number`,
},
date_from: {
sql: `date_from`,
type: `number`,
title: `cubePlaceVisitDateFrom`
},
date_to: {
sql: `date_to`,
type: `number`,
title: `cubePlaceVisitDateTo`
}
},
dataSource: `clickhouse-telemetry`
});
```
**Data examples**
Employee_id | Started | Ended
-- | -- | --
136 | 2023-01-24T16:30:00.000 | 2023-01-24T16:32:02.000
136 | 2023-01-24T12:00:00.000 | 2023-01-24T12:09:14.046
136 | 2023-01-24T11:58:00.000 | 2023-01-24T11:59:55.086
136 | 2023-01-24T11:56:00.000 | 2023-01-24T11:57:39.000
136 | 2023-01-24T10:40:00.000 | 2023-01-24T10:44:52.994
PlaceAlarms data
Place Alarms Count Place Employee Id | Place Alarms Count cubePlaceVisitDateFrom | Place Alarms Count cubePlaceVisitDateTo
-- | -- | --
136 | 2023-01-24T10:28:32 | 2023-01-24T10:42:32.000
136 | 2023-01-24T11:02:32 | 2023-01-24T11:42:32.000
136 | 2023-01-24T12:00:32 | 2023-01-24T12:10:32.000
Desired Result
Place Alarms Count Place Employee Id | Place Alarms Count cubePlaceVisitDateFrom | Place Alarms Count cubePlaceVisitDateTo | Alarms Count_total
-- | -- | -- | --
136 | 2023-01-24T10:28:32 | 2023-01-24T10:42:32.000 | 1
136 | 2023-01-24T11:02:32 | 2023-01-24T11:42:32.000 | 0
136 | 2023-01-24T12:00:32 | 2023-01-24T12:10:32.000 | 1
Count only intersections by time?
Contributor guide
Assessment
This issue has not been assessed yet.