簡體   English   中英

將月份拆分為4並獲取每天的平均值

[英]Split months into 4 and retrieve the average value per day

這是在MySQL的復雜存儲過程中創建的臨時表上查詢的輸出:

在此處輸入圖片說明

現在的目標是像我們這里一樣擁有每天的平均時長,但每個月的平均時長

所以,我想每個月參加並把它分成4組天, 每天的平均持續時間。
因此,無論一個月中有多少天,我都會有4個值。

我怎樣才能做到這一點 ?

注意:如果更簡單,我可以使用php來完成,因為我將使用這種語言的數據。

您有一個每天分組的查詢,例如:

select 
  the_date as day,
  sec_to_time(avg(timestampdiff(second, start_time, end_time))) as duration
from ...
group by the_date;

您需要每月1-7、8-14、15-21、22天結束。 使用CASE WHEN構建組。

select 
  year(the_date) as year,
  month(the_date) as month,
  case 
    when day(the_date) <=  7 then '01-07'
    when day(the_date) <= 14 then '08-14'
    when day(the_date) <= 21 then '15-21'
    else                          '22-end'
  end as day_range,
  sec_to_time(avg(timestampdiff(second, start_time, end_time))) as duration
from ...
group by year, month, day_range
order by year, month, day_range;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM