Selecting an average of records grouped by 5 minute periods

后端 未结 3 471
离开以前
离开以前 2021-01-16 04:06

I\'m having a slight issue. I have a PostgreSQL table with such format

time (datetime)     | players (int) | servers (int)
----------------------------------         


        
3条回答
  •  执念已碎
    2021-01-16 04:27

    How about this?

    select datepart('year', time) as StartYear, datepart('month', time) as StartMonth,
        datepart('day', time) as StartDay, datepart('hour', time) as StartHour,
        floor(datepart('minute', time)/5)*5 as StartMinute,
        avg(case when datepart('minute', time) = floor(datepart('minute', time)/5)*5 then players else null end) as Zero,
        avg(case when datepart('minute', time) = floor(datepart('minute', time)/5)*5+1 then players else null end) as One,
        avg(case when datepart('minute', time) = floor(datepart('minute', time)/5)*5+2 then players else null end) as Two,
        avg(case when datepart('minute', time) = floor(datepart('minute', time)/5)*5+3 then players else null end) as Three,
        avg(case when datepart('minute', time) = floor(datepart('minute', time)/5)*5+4 then players else null end) as Four,
    from MyTable
    group by datepart('year', time), datepart('month', time),
        datepart('day', time), datepart('hour', time),
        floor(datepart('minute', time)/5)*5
    

提交回复
热议问题