django aggregation to lower resolution using grouping by a date range

后端 未结 4 747
南旧
南旧 2021-01-05 07:18

horrible title, but let me explain: i\'ve got this django model containing a timestamp (date) and the attribute to log - f.e. the number of users consuming some ressource -

4条回答
  •  天命终不由人
    2021-01-05 08:06

    After long trying i made it as SQL-statement:

    SELECT FROM_UNIXTIME(AVG(UNIX_TIMESTAMP(date))), SUM(value)
    FROM `my_table`
    WHERE date BETWEEN SUBTIME(NOW( ), '0:30:00') AND NOW()
    GROUP BY UNIX_TIMESTAMP(date) DIV 300
    ORDER BY date DESC
    

    with

    start_time = SUBTIME(NOW( ), '0:30:00')
    end_time = NOW()
    period = 300 # in seconds
    

    in the end - not really hard - and indeed independent from the time resolution of the samplings in the origin table.

提交回复
热议问题