简体   繁体   English

在Oracle SQL中按时间间隔聚合数据

[英]aggregating Data by Time Interval in Oracle SQL

I have a data set of weather temperature data that has a record for every 5 minute interval. 我有一个天气温度数据集,该数据集每隔5分钟记录一次。 I'd like to aggregate this to 15 minute data. 我想将其汇总为15分钟的数据。 To do this I need to group the time intervals by date, hour, and minutes so for the minutes I am group readings taken on the following minute interval {5, 10, 15}, {20, 25, 30}, {35, 40, 45}, {50, 55, 60}. 为此,我需要按日期,小时和分钟对时间间隔进行分组,因此对于分钟,我将在以下分钟间隔{5,10,15},{20,25,30},{35, 40,45},{50,55,60}。 For each group of three readings I should take the average value. 对于每组三个读数,我应该取平均值。

My data has a time stamp in the form of 'DD-MMM-YY HH.MM,' so I have extracted the date, hours, and minutes. 我的数据带有“ DD-MMM-YY HH.MM”形式的时间戳,因此我提取了日期,小时和分钟。 I wrote a Case to group the time stamps, which hasn't worked quite right and I am not sure how to build in the average function into the case. 我写了一个Case来对时间戳进行分组,但效果不太理想,而且我不确定如何将平均函数构建到Case中。

  Select
  Temperature,
  Extract(Minute From Reading_Time) As Minutes,
  Extract(Hour From Reading_Time) As Hours,
  Cast(Reading_Time As Date) As Calendar_Date
  From Weather_Data Where Weather_Station='BDX'    
  Case
    When (Minutes>5 And Minutes<=15)  Then
    To_Timestamp(Calendar_Date &' '&'0'&Hours&':15', 'dd-mon-yy hh24:mi')
    When (Minutes>20 And Minutes<=30)  Then
    To_Timestamp(Calendar_Date &' '&'0'&Hours&':30', 'dd-mon-yy hh24:mi')
    When (Minutes>35 And Minutes<=45)  Then
    To_Timestamp(Calendar_Date &' '&'0'&Hours&':45', 'dd-mon-yy hh24:mi')
    When (Minutes>50 And Minutes<=60)  Then
    To_Timestamp(Calendar_Date &' '&'0'&Hours&':60', 'dd-mon-yy hh24:mi')
End

Modified code with Gordon's feedback. 带有戈登反馈的修改后的代码。 I also rewrote the case logic to correct for a slight offset in time. 我还重写了案例逻辑,以在时间上稍作调整。

select t.*,
(Case When Minutes <= 15 AND Minutes > 0
         Then To_Timestamp(Calendar_Date  &' '&Hours&':15', 'dd-mon-yy hh24:mi')
         When Minutes <= 30 AND Minutes > 0
         Then To_Timestamp(Calendar_Date &' '&Hours&':30', 'dd-mon-yy hh24:mi')
         When Minutes <= 45 AND Minutes > 0
         Then To_Timestamp(Calendar_Date &' '&Hours&':45', 'dd-mon-yy hh24:mi')
         When Minutes = 0 
         Then To_Timestamp(Calendar_Date &' '&Hours&':00', 'dd-mon-yy hh24:mi')
         Else To_Timestamp(Calendar_Date &' '&(Hours+1)&':00', 'dd-mon-yy hh24:mi')
   End) As Aggregated_Timestamp
 from (Select average(Temperature), Extract(Minute From Reading_Time) As Minutes,
         Extract(Hour From Reading_Time) As Hours, Cast(Reading_Time As Date) As    Calendar_Date
  From Weather_Data
  Where Weather_Station = 'BDX'
  Group by Aggregated_Time
 ) t 

Your case is just lingering after your query. 查询之后,您的case就一直持续着。 The easiest way to write this is with a subquery: 最简单的方法是使用子查询:

select t.*,
       (Case When (Minutes>5 And Minutes<=15) 
             Then To_Timestamp(Calendar_Date &' '&'0'&Hours&':15', 'dd-mon-yy hh24:mi')
             When (Minutes>20 And Minutes<=30)
             Then To_Timestamp(Calendar_Date &' '&'0'&Hours&':30', 'dd-mon-yy hh24:mi')
             When (Minutes>35 And Minutes<=45)
             Then To_Timestamp(Calendar_Date &' '&'0'&Hours&':45', 'dd-mon-yy hh24:mi')
             When (Minutes>50 And Minutes<=60)
             Then To_Timestamp(Calendar_Date &' '&'0'&Hours&':60', 'dd-mon-yy hh24:mi')
       End)
from (Select Temperature, Extract(Minute From Reading_Time) As Minutes,
             Extract(Hour From Reading_Time) As Hours, Cast(Reading_Time As Date) As Calendar_Date
      From Weather_Data
      Where Weather_Station = 'BDX'
     ) t 

I don't fully understand your logic, because you have gaps. 我没有完全理解您的逻辑,因为您之间存在差距。 I would simply write: 我会简单地写:

       (Case When Minutes <= 15
             Then To_Timestamp(Calendar_Date &' '&'0'&Hours&':15', 'dd-mon-yy hh24:mi')
             When Minutes <= 30
             Then To_Timestamp(Calendar_Date &' '&'0'&Hours&':30', 'dd-mon-yy hh24:mi')
             When Minutes <= 45
             Then To_Timestamp(Calendar_Date &' '&'0'&Hours&':45', 'dd-mon-yy hh24:mi')
             else To_Timestamp(Calendar_Date &' '&'0'&Hours&':60', 'dd-mon-yy hh24:mi')
       End)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM