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

发布于 2025-01-16 18:55:02 字数 4367 浏览 3 评论 0原文

您好,我有一个像这样的表(只是一个例子)

仪表板类别时间戳
PV ENERGY11/03/2022113.957348
BATTERY ENERGY11/03/2022140.5875153
HEAT ENERGY11/03/2022276.9997795
TOTAL ENERGY11/03/2022487.9871685
光伏能源14/03/2022534.6937951
电池能源14/03/2022625.9614076
热能14/03/2022669.2673149
总能源14/03/20221175.157762
光伏能源19/03/20221352.12033
电池能源19/03/20221747.298151
热能19/03/20221891.235057
总能源19/03/20221909.890893
光伏能源20/03/20222118.666904
电池能源20/03/20222335.954084
热能20/03/20222542.706342
总能源20/03/20222675.744966
光伏能源21/03/20223513.539046
电池能源21/03/20224464.32658
热能21/03/20224469.372355
总能源21/03/20224650.514689

这是我想要的结果,基于整个表中的最小和最大时间戳,并通过仪表板类别提取相应的值。预期的查询结果低于

仪表板类别value(t_min)value(t_max)max -min_value
光伏能源113.9573483513.5390463399.581698
电池能量140.58751534464.326584323.739064
热能276.99977954469.3723554192.372575
总能量487.98716854650.5146894162.52752

如何在大查询中实现此目的?

谢谢!

Hi I have a table like this ( just an example)

Dashboarding Categorytimestampvalue
PV ENERGY11/03/2022113.957348
BATTERY ENERGY11/03/2022140.5875153
HEAT ENERGY11/03/2022276.9997795
TOTAL ENERGY11/03/2022487.9871685
PV ENERGY14/03/2022534.6937951
BATTERY ENERGY14/03/2022625.9614076
HEAT ENERGY14/03/2022669.2673149
TOTAL ENERGY14/03/20221175.157762
PV ENERGY19/03/20221352.12033
BATTERY ENERGY19/03/20221747.298151
HEAT ENERGY19/03/20221891.235057
TOTAL ENERGY19/03/20221909.890893
PV ENERGY20/03/20222118.666904
BATTERY ENERGY20/03/20222335.954084
HEAT ENERGY20/03/20222542.706342
TOTAL ENERGY20/03/20222675.744966
PV ENERGY21/03/20223513.539046
BATTERY ENERGY21/03/20224464.32658
HEAT ENERGY21/03/20224469.372355
TOTAL ENERGY21/03/20224650.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 Categoryvalue(t_min)value(t_max)max-min_value
PV ENERGY113.9573483513.5390463399.581698
BATTERY ENERGY140.58751534464.326584323.739064
HEAT ENERGY276.99977954469.3723554192.372575
TOTAL ENERGY487.98716854650.5146894162.52752

How can I achieve this in big query?

Thanks!

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

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

发布评论

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

评论(2

心意如水 2025-01-23 18:55:02

请考虑以下方法

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
)                

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

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

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

enter image description here

童话 2025-01-23 18:55:02

我没有使用过 Bigquery,但我认为它像大多数其他数据库类型一样支持通用表表达式。 CTE 计算并使用稍后在查询中引用的值。这是有效的,您可以使用这些 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 在此处找到。

编辑
根据您的评论,这是另一个按类别和建筑物确定最小值和最大值的解决方案:

    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

找到 db-fiddle 此处,输出如下所示:

    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

I haven't used Bigquery, but I think it supports common table expressions like most other database types. The CTEs calculate and use a value referenced later in the query. This works, where you are using those CTEs to get the min and max values, and the bottom brings it altogether.

    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.

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:

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