Dynamic table name 动态表名
- Dominant language
- Rust
- Stars
- 20.8k
- Forks
- 2.1k
- Avg merge
- 1d 2h
- Merged PRs (30d)
- 181
Description
中文:根据不同周期来获取不同的sql_table。
例如:dateType:DAY WEEK MONTH,对应三张表 t_ads_store_day_stats t_ads_store_week_stats t_ads_store_month_stats。
它们三个表的字段是一致的,因此我想只创建一个 cube: name='store'。
我应该怎么做?(目前我能想到的只是下面这种方式,还有没有更好的方式。)
Chinese: Obtain different SQL_tables according to different periods.
For example, dateType:DAY WEEK MONTH corresponds to three tables t_ads_store_day_stats t_ads_store_week_stats t_ads_store_month_stats.
The fields in all three tables are the same, so I want to create just one cube: name='store'.
What should I do?(So far I can think of only the following way, there is no better way)
使用动态联合表
https://cube.dev/docs/guides/recipes/data-modeling/dynamic-union-tables
```
// model/utils.js
const tableNames = [
{ dateType: "DAY", table: "t_ads_store_daily_stats" },
{ dateType: "WEEK", table: "t_ads_store_weekly_stats" },
{ dateType: "MONTH", table: "t_ads_store_month_total_stats" }
];
export function unionData() {
return tableNames.map((p) =>
`select
*,
'${p.dateType}' dateType
from ${p.table}
`
)
.join(" UNION ALL ");
}
import { unionData } from "../utils";
cube(`store`, {
sql: unionData(),
measures: {
...
},
dimensions: {
dateType: {
sql: `dateType`,
type: `string`,
},
},
});
```
功能:是否可以增加一个功能,根据前端一个动态的值来映射不同的表(以下是我的构思,如果有更好的方案请告诉我,我将感激不尽!)。
feat: Can you add a function to map different tables based on a dynamic value in the front end(The following is my idea, if there is a better plan, please let me know,I would be grateful.)?
```
# a table
cubes:
- name: store
#sql: select * from {store_table}
sql_table: {store_table}
store_table:
DAY: t_ads_store_daily_stats
WEEK: t_ads_store_weekly_stats
MONTH: t_ads_store_month_total_stats
data_source: default
...
{
"measures": ["store.sales_amt_actual","store.sales_qty","store.deal_rate"],
"order": [["store.sales_amt_actual","desc"]],
"measures": ["store.store_id","scd.store_name"],
"filters": [
{"key":"store.stats_date","op":"inDateRange","values":["2023-05-01","2023-05-07"]}
],
// new
"table_map": {
"store_table": "DAY"
}
}
# more table join
cubes:
- name: store
sql: select * from {store_table} store join {target_table} target on store.store_id = target.store_id
table_mapping:
store_table:
DAY: t_ads_store_daily_stats
WEEK: t_ads_store_weekly_stats
MONTH: t_ads_store_month_total_stats
target_table:
DAY: t_sc_feature_daily_target_info
WEEK: t_sc_feature_week_target_info
MONTH: t_sc_feature_month_target_info
data_source: default
```
Contributor guide
Assessment
This issue has not been assessed yet.