我猜你有这样的东西(BigQuery 语法):
view: invoice {
derived_table: {
sql: SELECT *
FROM UNNEST(ARRAY<STRUCT<ID STRING, COST FLOAT64, PAYMENTID STRING>>[
('X', 10, '1'),
('Y', 30, '1'),
('Z', 60, '1')])
;;
}
dimension: id {
type: string
primary_key: yes
}
dimension: cost {
type: number
}
dimension: paymentid {
type: string
}
measure: total_cost {
type: sum
sql: ${cost} ;;
}
measure: total_amount {
type: sum
sql: ${payment.amount} ;;
}
}
view: payment {
derived_table: {
sql: SELECT *
FROM UNNEST(ARRAY<STRUCT<PAYMENTID STRING, AMOUNT FLOAT64>>[
('1', 100),
('2', 200)])
;;
}
dimension: paymentid {
primary_key: yes
type: string
}
dimension: amount {
type: number
}
}
explore: invoice {
join: payment {
sql_on: ${invoice.paymentid} = ${payment.paymentid} ;;
}
}
这确实将总发票显示为发票的总和,因为您通过首先加入来对表格进行非规范化。
如果您在它所属的视图中创建总度量,Looker 的对称聚合将使用主键处理扇形。
view: invoice {
derived_table: {
sql: SELECT *
FROM UNNEST(ARRAY<STRUCT<ID STRING, COST FLOAT64, PAYMENTID STRING>>[
('X', 10, '1'),
('Y', 30, '1'),
('Z', 60, '1')])
;;
}
dimension: id {
type: string
primary_key: yes
}
dimension: cost {
type: number
}
dimension: paymentid {
type: string
}
measure: total_cost {
type: sum
sql: ${cost} ;;
}
}
view: payment {
derived_table: {
sql: SELECT *
FROM UNNEST(ARRAY<STRUCT<PAYMENTID STRING, AMOUNT FLOAT64>>[
('1', 100),
('2', 200)])
;;
}
dimension: paymentid {
primary_key: yes
type: string
}
dimension: amount {
type: number
}
measure: total_amount {
type: sum
sql: ${amount} ;;
}
}
explore: invoice {
join: payment {
sql_on: ${invoice.paymentid} = ${payment.paymentid} ;;
}
}
那么总数将是正确的。
总成本等于总金额。