cube-js / cube-js/cube

Using time dimension date filter in SQL

Open
#7,867 3 comments 0 reactions 0 assignees View on GitHub
question
Dominant language
Rust
Stars
20.8k
Forks
2.1k
Avg merge
1d 2h
Merged PRs (30d)
181

Description

**Problem**

How to use date filters in SQL queries that are generated by pre_aggregations.

**Background**
I have a very complex SQL query, that runs daily, i.e. to run that SQL query we need a date filter to be passed.

```sql
SELECT '2024-01-01' as date, username, SUM(sales) as total_sales, more fields
FROM sales
-- COMPLEX JOINS BASED ON PASSED DATE.
WHERE sales.date = '2024-01-01'
```

As of now, I can do it using dynamic filters, but that has not worked with pre_aggregations, It replaces it with (1=1).

Expected Behaviour:
I want to run pre_aggregations daily, which generate today's date pass to SQL query and store the result. Currently, If I remove the date filter from the SQL query, the query becomes too complex and it takes a very long time to execute.

**Related Cube.js schema**
```javascript
cube(`Cube`, {
sql: `SELECT '2024-01-01' as date, username, SUM(sales) as total_sales, more fields FROM sales WHERE {FILTER_PARAMS.user_sales.date.filter('sales.date')}`
pre_aggregations: {
type: original_sql,
time_dimension: CUBE.date
partition_granularity: day
}
})
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.