简体   繁体   English

如何在大查询中根据时间戳最小最大值查询按某些类别分组的两个值的减法

[英]How can I query subtraction of two values grouped by certain categories based on timestamp min max on big query

Hi I have a table like this ( just an example)嗨,我有一张这样的桌子(只是一个例子)

Dashboarding Category仪表板类别 timestamp时间戳 value价值
PV ENERGY光伏能源 11/03/2022 11/03/2022 113.957348 113.957348
BATTERY ENERGY电池能量 11/03/2022 11/03/2022 140.5875153 140.5875153
HEAT ENERGY热能 11/03/2022 11/03/2022 276.9997795 276.9997795
TOTAL ENERGY总能量 11/03/2022 11/03/2022 487.9871685 487.9871685
PV ENERGY光伏能源 14/03/2022 14/03/2022 534.6937951 534.6937951
BATTERY ENERGY电池能量 14/03/2022 14/03/2022 625.9614076 625.9614076
HEAT ENERGY热能 14/03/2022 14/03/2022 669.2673149 669.2673149
TOTAL ENERGY总能量 14/03/2022 14/03/2022 1175.157762 1175.157762
PV ENERGY光伏能源 19/03/2022 19/03/2022 1352.12033 1352.12033
BATTERY ENERGY电池能量 19/03/2022 19/03/2022 1747.298151 1747.298151
HEAT ENERGY热能 19/03/2022 19/03/2022 1891.235057 1891.235057
TOTAL ENERGY总能量 19/03/2022 19/03/2022 1909.890893 1909.890893
PV ENERGY光伏能源 20/03/2022 20/03/2022 2118.666904 2118.666904
BATTERY ENERGY电池能量 20/03/2022 20/03/2022 2335.954084 2335.954084
HEAT ENERGY热能 20/03/2022 20/03/2022 2542.706342 2542.706342
TOTAL ENERGY总能量 20/03/2022 20/03/2022 2675.744966 2675.744966
PV ENERGY光伏能源 21/03/2022 21/03/2022 3513.539046 3513.539046
BATTERY ENERGY电池能量 21/03/2022 21/03/2022 4464.32658 4464.32658
HEAT ENERGY热能 21/03/2022 21/03/2022 4469.372355 4469.372355
TOTAL ENERGY总能量 21/03/2022 21/03/2022 4650.514689 4650.514689

And this is the result I want based on the min and maximum timestamp in the entire table and extracting the corresponding values by dashboarding category.The query result expected is below这是我想要的结果,基于整个表中的最小和最大时间戳,并通过仪表板类别提取相应的值。预期的查询结果如下

Dashboarding Category仪表板类别 value(t_min)价值(t_min) value(t_max)值(t_max) max-min_value最大最小值
PV ENERGY光伏能源 113.957348 113.957348 3513.539046 3513.539046 3399.581698 3399.581698
BATTERY ENERGY电池能量 140.5875153 140.5875153 4464.32658 4464.32658 4323.739064 4323.739064
HEAT ENERGY热能 276.9997795 276.9997795 4469.372355 4469.372355 4192.372575 4192.372575
TOTAL ENERGY总能量 487.9871685 487.9871685 4650.514689 4650.514689 4162.52752 4162.52752

How can I achieve this in big query?我怎样才能在大查询中实现这一点?

Thanks!谢谢!

I haven't used Bigquery, but I think it supports common table expressions like most other database types.我没有使用过 Bigquery,但我认为它像大多数其他数据库类型一样支持通用表表达式。 The CTEs calculate and use a value referenced later in the query. CTE 计算并使用稍后在查询中引用的值。 This works, where you are using those CTEs to get the min and max values, and the bottom brings it altogether.这行得通,您使用这些 CTE 来获取最小值和最大值,而底部则将其全部带入。

    with min_max_time as (
     select category, min(dt) as min_date, max(dt) as max_date
     from energy
     group by category
    ),
    min_value as (
     select e.category, e.evalue
     from energy e
     join min_max_time mmt
       on e.category = mmt.category
      and e.dt = mmt.min_date
    ), 
    max_value as (
     select e.category, e.evalue
     from energy e
     join min_max_time mmt
       on e.category = mmt.category
      and e.dt = mmt.max_date
    )
    select distinct e.category,
     minv.evalue as value_t_min, 
     maxv.evalue as value_t_max, 
     maxv.evalue - minv.evalue as max_minus_min_value
    from energy e
    join min_value minv
      on e.category = minv.category
    join max_value maxv
      on e.category = maxv.category

Db-fiddle found here .这里找到了 Db-fiddle。

EDIT Based on your comment, here's another solution that determines the min and max by Category and Building:编辑根据您的评论,这是另一种按类别和建筑物确定最小值和最大值的解决方案:

    with min_max_time as (
     select category, building, min(dt) as min_date, max(dt) as max_date
     from energy
     group by category, building
    ) ,
    min_value as (
    select e.category, e.building, e.evalue
    from energy e
    join min_max_time mmt
      on e.category = mmt.category
     and e.dt = mmt.min_date
     and e.building = mmt.building
    ), 
    max_value as (
    select e.category, e.building, e.evalue
    from energy e
    join min_max_time mmt
      on e.category = mmt.category
     and e.dt = mmt.max_date
     and e.building = mmt.building
    )
    select distinct e.category, 
    e.building, 
    minv.evalue as value_t_min, 
    maxv.evalue as value_t_max, 
    maxv.evalue - minv.evalue as max_minus_min_value
    from energy e
    join min_value minv
      on e.category = minv.category
     and e.building = minv.building
    join max_value maxv
      on e.category = maxv.category
     and e.building = maxv.building

The db-fiddle is found here , and the output looks like this: db-fiddle 可在此处找到,output 如下所示:

    category        building    value_t_min     value_t_max     max_minus_min_value
    BATTERY ENERGY  house       140.5875153     4464.32658      4323.7390647
    BATTERY ENERGY  shed        40.5875153      464.32658       423.7390647
    HEAT ENERGY     house       276.9997795     4469.372355     4192.3725755
    HEAT ENERGY     shed        76.9997795      469.372355      392.3725755
    PV ENERGY       house       113.957348      3513.539046     3399.581698
    PV ENERGY       shed        13.957348       513.539046      499.581698
    TOTAL ENERGY    house       487.9871685     4650.514689     4162.5275205
    TOTAL ENERGY    shed        87.9871685      650.514689      562.5275205

Consider below approach考虑以下方法

select *, value_t_max - value_t_min as max_minus_min_value
from (
  select DashboardingCategory, 
    array_agg(value order by timestamp limit 1)[offset(0)] as value_t_min,
    array_agg(value order by timestamp desc limit 1)[offset(0)] as value_t_max,
  from  your_table
  group by DashboardingCategory
)                

if applied to sample data in your question - output is如果应用于您问题中的示例数据 - output 是

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Big Query SQL 我可以实现以下减法(减去两行值并将其替换为该值)? - Using Big Query SQL I can I achieve the following subtraction ( subtract two row values and replace it with that value )? 如何将值中包含换行符的 CSV 数据导入 GCloud Big Query 表? - How can I import CSV data with newline characters in its values into a GCloud Big Query table? 如何在 Big Query 中只获取没有时区的时间戳作为字符串? - How to get only the timestamp without the time zone as a string in Big Query? 如何查询按“时间戳”排序的 dynamodb 表? - How can I query dynamodb table sorted by `timestamp`? 如何将 Performance MAX 广告系列 Google Ads 检索到 Google Big Query - How to retrieve Performance MAX campaigns Google Ads into Google Big Query 大查询:查询不允许我们获取结构的最小值 - Big Query: Query doesn't allow us to get the min of a struct firebase 查询如何从子项中检索两个特定值 - firebase query how can i retrieve two specific values from child 大查询中的上一季度最大月份值 - Previous quarter max month value in big Query 如何根据字段日期获取大查询的最新记录 - How to get the latest record on big query based on field date 如何将大查询 event_timestamps 转换为 PST? - How can I convert big query event_timestamps into PST?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM