如何在大查询上根据时间戳最小值最大值查询按某些类别分组的两个值的减法
您好,我有一个像这样的表(只是一个例子)
仪表板类别 | 时间戳 | 值 |
---|---|---|
PV ENERGY | 11/03/2022 | 113.957348 |
BATTERY ENERGY | 11/03/2022 | 140.5875153 |
HEAT ENERGY | 11/03/2022 | 276.9997795 |
TOTAL ENERGY | 11/03/2022 | 487.9871685 |
光伏能源 | 14/03/2022 | 534.6937951 |
电池能源 | 14/03/2022 | 625.9614076 |
热能 | 14/03/2022 | 669.2673149 |
总能源 | 14/03/2022 | 1175.157762 |
光伏能源 | 19/03/2022 | 1352.12033 |
电池能源 | 19/03/2022 | 1747.298151 |
热能 | 19/03/2022 | 1891.235057 |
总能源 | 19/03/2022 | 1909.890893 |
光伏能源 | 20/03/2022 | 2118.666904 |
电池能源 | 20/03/2022 | 2335.954084 |
热能 | 20/03/2022 | 2542.706342 |
总能源 | 20/03/2022 | 2675.744966 |
光伏能源 | 21/03/2022 | 3513.539046 |
电池能源 | 21/03/2022 | 4464.32658 |
热能 | 21/03/2022 | 4469.372355 |
总能源 | 21/03/2022 | 4650.514689 |
这是我想要的结果,基于整个表中的最小和最大时间戳,并通过仪表板类别提取相应的值。预期的查询结果低于
仪表板类别 | value(t_min) | value(t_max) | max -min_value |
---|---|---|---|
光伏能源 | 113.957348 | 3513.539046 | 3399.581698 |
电池能量 | 140.5875153 | 4464.32658 | 4323.739064 |
热能 | 276.9997795 | 4469.372355 | 4192.372575 |
总能量 | 487.9871685 | 4650.514689 | 4162.52752 |
如何在大查询中实现此目的?
谢谢!
Hi I have a table like this ( just an example)
Dashboarding Category | timestamp | value |
---|---|---|
PV ENERGY | 11/03/2022 | 113.957348 |
BATTERY ENERGY | 11/03/2022 | 140.5875153 |
HEAT ENERGY | 11/03/2022 | 276.9997795 |
TOTAL ENERGY | 11/03/2022 | 487.9871685 |
PV ENERGY | 14/03/2022 | 534.6937951 |
BATTERY ENERGY | 14/03/2022 | 625.9614076 |
HEAT ENERGY | 14/03/2022 | 669.2673149 |
TOTAL ENERGY | 14/03/2022 | 1175.157762 |
PV ENERGY | 19/03/2022 | 1352.12033 |
BATTERY ENERGY | 19/03/2022 | 1747.298151 |
HEAT ENERGY | 19/03/2022 | 1891.235057 |
TOTAL ENERGY | 19/03/2022 | 1909.890893 |
PV ENERGY | 20/03/2022 | 2118.666904 |
BATTERY ENERGY | 20/03/2022 | 2335.954084 |
HEAT ENERGY | 20/03/2022 | 2542.706342 |
TOTAL ENERGY | 20/03/2022 | 2675.744966 |
PV ENERGY | 21/03/2022 | 3513.539046 |
BATTERY ENERGY | 21/03/2022 | 4464.32658 |
HEAT ENERGY | 21/03/2022 | 4469.372355 |
TOTAL ENERGY | 21/03/2022 | 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) | value(t_max) | max-min_value |
---|---|---|---|
PV ENERGY | 113.957348 | 3513.539046 | 3399.581698 |
BATTERY ENERGY | 140.5875153 | 4464.32658 | 4323.739064 |
HEAT ENERGY | 276.9997795 | 4469.372355 | 4192.372575 |
TOTAL ENERGY | 487.9871685 | 4650.514689 | 4162.52752 |
How can I achieve this in big query?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请考虑以下方法
如果应用于问题中的示例数据,
- 输出为
Consider below approach
if applied to sample data in your question - output is
我没有使用过 Bigquery,但我认为它像大多数其他数据库类型一样支持通用表表达式。 CTE 计算并使用稍后在查询中引用的值。这是有效的,您可以使用这些 CTE 来获取最小值和最大值,并且底部将其全部呈现出来。
Db-fiddle 在此处找到。
编辑
根据您的评论,这是另一个按类别和建筑物确定最小值和最大值的解决方案:
找到 db-fiddle 此处,输出如下所示:
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.
Db-fiddle found here.
EDIT
Based on your comment, here's another solution that determines the min and max by Category and Building:
The db-fiddle is found here, and the output looks like this: