BigQuery SQL 运行 算术余额视图
BigQuery SQL running arithmetic balance view
我有 2 个 table 想加入,以便获得下面所需的视图:
Table 1:合同
Contracts*
Quantity
Contract 1
10
Contract 2
20
Contract 3
30
Table 2:交货
Date
Vehicle*
Contracts
Delivered
01/01/2022
Vehicle A
Contract 1
5
02/01/2022
Vehicle B
Contract 1
5
02/01/2022
Vehicle B
Contract 2
10
03/01/2022
Vehicle C
Contract 2
10
03/01/2022
Vehicle C
Contract 3
15
所需视图(显示当前未结数量,以抵消该特定合同的交付)
*
- 表示 table
的主键
Date
Vehicle
Contracts
Quantity
Outstanding
Delivered
Balance
01/01/2022
Vehicle A
Contract 1
10
10
5
5
02/01/2022
Vehicle B
Contract 1
10
5
5
0
02/01/2022
Vehicle B
Contract 2
20
20
10
10
03/01/2022
Vehicle C
Contract 2
20
10
10
0
03/01/2022
Vehicle C
Contract 3
30
30
15
15
select
deliveries.date,
deliveries.vehicle,
contracts.contracts,
contracts.quantity,
outstanding (?),
contracts.quantity - contracts.delivered as delivered
balance (?)
from contracts
left join deliveries on deliveries.contracts = contracts.contracts
我不知道outstanding和balance的表达式怎么写。
是否涉及sum(quantity - delivered) over (partition by vehicle order by contract)
?
考虑以下方法
select date, vehicle, contracts, quantity,
quantity - ifnull(sum(delivered) over prev, 0) outstanding,
delivered,
quantity - delivered - ifnull(sum(delivered) over prev, 0) balance
from deliveries d
left join contracts c
using (contracts)
window prev as (partition by contracts order by date rows between unbounded preceding and 1 preceding)
如果应用于您问题中的示例数据 - 输出为
我有 2 个 table 想加入,以便获得下面所需的视图:
Table 1:合同
Contracts* | Quantity |
---|---|
Contract 1 | 10 |
Contract 2 | 20 |
Contract 3 | 30 |
Table 2:交货
Date | Vehicle* | Contracts | Delivered |
---|---|---|---|
01/01/2022 | Vehicle A | Contract 1 | 5 |
02/01/2022 | Vehicle B | Contract 1 | 5 |
02/01/2022 | Vehicle B | Contract 2 | 10 |
03/01/2022 | Vehicle C | Contract 2 | 10 |
03/01/2022 | Vehicle C | Contract 3 | 15 |
所需视图(显示当前未结数量,以抵消该特定合同的交付)
*
- 表示 table
Date | Vehicle | Contracts | Quantity | Outstanding | Delivered | Balance |
---|---|---|---|---|---|---|
01/01/2022 | Vehicle A | Contract 1 | 10 | 10 | 5 | 5 |
02/01/2022 | Vehicle B | Contract 1 | 10 | 5 | 5 | 0 |
02/01/2022 | Vehicle B | Contract 2 | 20 | 20 | 10 | 10 |
03/01/2022 | Vehicle C | Contract 2 | 20 | 10 | 10 | 0 |
03/01/2022 | Vehicle C | Contract 3 | 30 | 30 | 15 | 15 |
select
deliveries.date,
deliveries.vehicle,
contracts.contracts,
contracts.quantity,
outstanding (?),
contracts.quantity - contracts.delivered as delivered
balance (?)
from contracts
left join deliveries on deliveries.contracts = contracts.contracts
我不知道outstanding和balance的表达式怎么写。
是否涉及sum(quantity - delivered) over (partition by vehicle order by contract)
?
考虑以下方法
select date, vehicle, contracts, quantity,
quantity - ifnull(sum(delivered) over prev, 0) outstanding,
delivered,
quantity - delivered - ifnull(sum(delivered) over prev, 0) balance
from deliveries d
left join contracts c
using (contracts)
window prev as (partition by contracts order by date rows between unbounded preceding and 1 preceding)
如果应用于您问题中的示例数据 - 输出为