使用 Big Query SQL 我可以实现以下减法(减去两行值并将其替换为该值)吗?

发布于 2025-01-19 03:02:17 字数 3048 浏览 1 评论 0原文

应用程序BuildingIDDashboarding_tag
AbacusBLD01Heating_System_Energy_Conspiration3000.00
AbacusBLD02Heating_System_Energy_Conspiration1000.00
AbacusBLD04Heating_System_Energy_Conspiration1000.00
AbacusBLD05Heating_System_Energy_Conspiration500.00
AbacusBLD02MVHR_Energy_Conspiration300.00
AbacusBLD03MVHR_Energy_Conspiration900.00
AbacusBLD04MVHR_Energy_Conspiration50.00
AbacusBLD05MVHR_Energy_Conspiration80.00
AbacusBLD06MVHR_Energy_Conspiration200.00
ForellBLD07Heating_System_Energy_Conspiration0.00
ForellBLD08Heating_System_Energy_Conspiration50.00
ForellBLD09Heating_System_Energy_Conspiration-5.00
ForellBLD10Heating_System_Energy_Conspiration-30.00

应用程序buildingid仪表板_标签
AbacusBLD01Heating_System_Energy_Conspiration3000.00
AbacusBLD02加热_系统_能源_消耗 700.00
算盘BLD04加热_系统_能源_消耗950.00
算盘BLD05加热_系统_能源_消耗420.00
算盘BLD02MVHR_能源_消耗300.00
算盘BLD03MVHR_能源_消耗900.00
算盘BLD04MVHR_Energy_Conspiration50.00
AbacusBLD05MVHR_Energy_Conspiration80.00
AbacusBLD06MVHR_Energy_Conspiration200.00
ForellBLD07Heating_System_Energy_Conspiration0.00
ForellBLD08Heating_System_Energy_Conspiration50.00
ForellBLD09Heating_System_Energy_Conspiration-5.00
ForellBLD10Heating_System_Energy_Conspiration-30.00

请注意表 2 中第 2、3 和 4 行中的值如何根据第 5、7、8 行中的值进行更改。

逻辑是,如果应用程序是 Abacus ,则始终替换 Heating_System_Energy_Conspiration 的值,如下所示:

Heating_System_Energy_Conspiration=Heating_System_Energy_Conspiration-MVHR_Energy_Conspiration 每栋建筑

applicationbuildingiddashboarding_tagvalue
AbacusBLD01Heating_System_Energy_Consumption3000.00
AbacusBLD02Heating_System_Energy_Consumption1000.00
AbacusBLD04Heating_System_Energy_Consumption1000.00
AbacusBLD05Heating_System_Energy_Consumption500.00
AbacusBLD02MVHR_Energy_Consumption300.00
AbacusBLD03MVHR_Energy_Consumption900.00
AbacusBLD04MVHR_Energy_Consumption50.00
AbacusBLD05MVHR_Energy_Consumption80.00
AbacusBLD06MVHR_Energy_Consumption200.00
ForellBLD07Heating_System_Energy_Consumption0.00
ForellBLD08Heating_System_Energy_Consumption50.00
ForellBLD09Heating_System_Energy_Consumption-5.00
ForellBLD10Heating_System_Energy_Consumption-30.00

to

applicationbuildingiddashboarding_tagvalue
AbacusBLD01Heating_System_Energy_Consumption3000.00
AbacusBLD02Heating_System_Energy_Consumption700.00
AbacusBLD04Heating_System_Energy_Consumption950.00
AbacusBLD05Heating_System_Energy_Consumption420.00
AbacusBLD02MVHR_Energy_Consumption300.00
AbacusBLD03MVHR_Energy_Consumption900.00
AbacusBLD04MVHR_Energy_Consumption50.00
AbacusBLD05MVHR_Energy_Consumption80.00
AbacusBLD06MVHR_Energy_Consumption200.00
ForellBLD07Heating_System_Energy_Consumption0.00
ForellBLD08Heating_System_Energy_Consumption50.00
ForellBLD09Heating_System_Energy_Consumption-5.00
ForellBLD10Heating_System_Energy_Consumption-30.00

Notice how the values in row 2 ,3 and 4 changed on table 2 based on values in row 5,7,8.

The logic is , if the application is Abacus , then always replace the value of the Heating_System_Energy_Consumption such that:

Heating_System_Energy_Consumption=Heating_System_Energy_Consumption-MVHR_Energy_Consumption
for each building

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

愛上了 2025-01-26 03:02:17

请考虑以下方法

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

如果应用于问题中的示例数据,

- 输出为在此处输入图像描述

Consider below approach

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

if applied to sample data in your question - output is

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文