gpt4 book ai didi

sql - SQL对同一数据集的多个时间段求平均

转载 作者:行者123 更新时间:2023-12-03 17:14:19 25 4
gpt4 key购买 nike

我有以下查询:

SELECT AVG(val) from floatTable
WHERE tagindex IN(1,2,3,4)
AND DateAndTime > '$first_of_year'


它返回迄今为止为这四个标记测量的所有值的平均值。由于我已经在检索这些数据,因此如何从一个月的第一天,从一周的第一天以及从午夜开始获取数据?我已经将这些日期计算为$ first_of_month,$ first_of_week和$ midnight。我正在尝试最小化查询,并希望有人可以帮助我提高SQL魔术性,以便在单个查询或一组优化的查询中返回此数据。该查询平均需要300秒,因此我想做得尽可能少。

先感谢您。

最佳答案

SELECT AVG(case when DateAndTime > '$first_of_year' then val end) as FirstOfYear,
AVG(case when DateAndTime > '$first_of_month' then val end) as FirstOfMonth,
AVG(case when DateAndTime > '$first_of_week' then val end) as FirstOfWeek,
AVG(case when DateAndTime > '$midnight' then val end) as Midnight
from floatTable
WHERE tagindex IN(1,2,3,4)
and DateAndTime > '$first_of_year'


为了提高性能,请确保在 DateAndTimetagIndex列上都有索引。

关于sql - SQL对同一数据集的多个时间段求平均,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3703176/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com