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

时间:2014-02-11 22:38:05

标签: sql aggregation

我有一个天气温度数据的数据集,每隔5分钟有一个记录。我想把它汇总到15分钟的数据。要做到这一点,我需要按日期,小时和分钟对时间间隔进行分组,以便我在下一分钟间隔{5,10,15},{20,25,30},{35, 40,45},{50,55,60}。对于每组三个读数,我应该取平均值。

我的数据有'DD-MMM-YY HH.MM'形式的时间戳,所以我提取了日期,小时和分钟。我写了一个案例来分组时间戳,这个时间戳工作得不是很正确,我不知道如何将平均函数构建到案例中。

  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

使用Gordon的反馈修改代码。我还重写了案例逻辑,以纠正稍微的时间偏移。

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 

1 个答案:

答案 0 :(得分:0)

您的case只是在您的查询后徘徊。最简单的方法是使用子查询:

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 

我不完全理解你的逻辑,因为你有差距。我只想写:

       (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)