使用 Big Query SQL 我可以实现以下减法(减去两行值并将其替换为该值)?
Using Big Query SQL I can I achieve the following subtraction ( subtract two row values and replace it with that value )?
application
buildingid
dashboarding_tag
value
Abacus
BLD01
Heating_System_Energy_Consumption
3000.00
Abacus
BLD02
Heating_System_Energy_Consumption
1000.00
Abacus
BLD04
Heating_System_Energy_Consumption
1000.00
Abacus
BLD05
Heating_System_Energy_Consumption
500.00
Abacus
BLD02
MVHR_Energy_Consumption
300.00
Abacus
BLD03
MVHR_Energy_Consumption
900.00
Abacus
BLD04
MVHR_Energy_Consumption
50.00
Abacus
BLD05
MVHR_Energy_Consumption
80.00
Abacus
BLD06
MVHR_Energy_Consumption
200.00
Forell
BLD07
Heating_System_Energy_Consumption
0.00
Forell
BLD08
Heating_System_Energy_Consumption
50.00
Forell
BLD09
Heating_System_Energy_Consumption
-5.00
Forell
BLD10
Heating_System_Energy_Consumption
-30.00
到
application
buildingid
dashboarding_tag
value
Abacus
BLD01
Heating_System_Energy_Consumption
3000.00
Abacus
BLD02
Heating_System_Energy_Consumption
700.00
Abacus
BLD04
Heating_System_Energy_Consumption
950.00
Abacus
BLD05
Heating_System_Energy_Consumption
420.00
Abacus
BLD02
MVHR_Energy_Consumption
300.00
Abacus
BLD03
MVHR_Energy_Consumption
900.00
Abacus
BLD04
MVHR_Energy_Consumption
50.00
Abacus
BLD05
MVHR_Energy_Consumption
80.00
Abacus
BLD06
MVHR_Energy_Consumption
200.00
Forell
BLD07
Heating_System_Energy_Consumption
0.00
Forell
BLD08
Heating_System_Energy_Consumption
50.00
Forell
BLD09
Heating_System_Energy_Consumption
-5.00
Forell
BLD10
Heating_System_Energy_Consumption
-30.00
请注意第 2、3 和 4 行中的值如何根据第 5、7、8 行中的值在 table 2 上发生变化。
逻辑是,如果应用程序是 Abacus,则始终替换 Heating_System_Energy_Consumption 的值,这样:
Heating_System_Energy_Consumption=Heating_System_Energy_Consumption-MVHR_Energy_Consumption
每栋建筑
考虑以下方法
select * except(value),
if(dashboarding_tag = 'Heating_System_Energy_Consumption',
value - sum(if(dashboarding_tag = 'MVHR_Energy_Consumption', value, 0)) over(partition by application, buildingid),
value) as value
from your_table
如果应用于您问题中的示例数据 - 输出为
application | buildingid | dashboarding_tag | value |
---|---|---|---|
Abacus | BLD01 | Heating_System_Energy_Consumption | 3000.00 |
Abacus | BLD02 | Heating_System_Energy_Consumption | 1000.00 |
Abacus | BLD04 | Heating_System_Energy_Consumption | 1000.00 |
Abacus | BLD05 | Heating_System_Energy_Consumption | 500.00 |
Abacus | BLD02 | MVHR_Energy_Consumption | 300.00 |
Abacus | BLD03 | MVHR_Energy_Consumption | 900.00 |
Abacus | BLD04 | MVHR_Energy_Consumption | 50.00 |
Abacus | BLD05 | MVHR_Energy_Consumption | 80.00 |
Abacus | BLD06 | MVHR_Energy_Consumption | 200.00 |
Forell | BLD07 | Heating_System_Energy_Consumption | 0.00 |
Forell | BLD08 | Heating_System_Energy_Consumption | 50.00 |
Forell | BLD09 | Heating_System_Energy_Consumption | -5.00 |
Forell | BLD10 | Heating_System_Energy_Consumption | -30.00 |
到
application | buildingid | dashboarding_tag | value |
---|---|---|---|
Abacus | BLD01 | Heating_System_Energy_Consumption | 3000.00 |
Abacus | BLD02 | Heating_System_Energy_Consumption | 700.00 |
Abacus | BLD04 | Heating_System_Energy_Consumption | 950.00 |
Abacus | BLD05 | Heating_System_Energy_Consumption | 420.00 |
Abacus | BLD02 | MVHR_Energy_Consumption | 300.00 |
Abacus | BLD03 | MVHR_Energy_Consumption | 900.00 |
Abacus | BLD04 | MVHR_Energy_Consumption | 50.00 |
Abacus | BLD05 | MVHR_Energy_Consumption | 80.00 |
Abacus | BLD06 | MVHR_Energy_Consumption | 200.00 |
Forell | BLD07 | Heating_System_Energy_Consumption | 0.00 |
Forell | BLD08 | Heating_System_Energy_Consumption | 50.00 |
Forell | BLD09 | Heating_System_Energy_Consumption | -5.00 |
Forell | BLD10 | Heating_System_Energy_Consumption | -30.00 |
请注意第 2、3 和 4 行中的值如何根据第 5、7、8 行中的值在 table 2 上发生变化。
逻辑是,如果应用程序是 Abacus,则始终替换 Heating_System_Energy_Consumption 的值,这样:
Heating_System_Energy_Consumption=Heating_System_Energy_Consumption-MVHR_Energy_Consumption 每栋建筑
考虑以下方法
select * except(value),
if(dashboarding_tag = 'Heating_System_Energy_Consumption',
value - sum(if(dashboarding_tag = 'MVHR_Energy_Consumption', value, 0)) over(partition by application, buildingid),
value) as value
from your_table
如果应用于您问题中的示例数据 - 输出为