2013-02-27 161 views
0

我有兩列。第一個是「開始日期」,第二個是「結束日期」。然後使用查詢計算兩者之間的天數差異。 我想知道如何計算這個結果的月平均值。DATEDIFF結果月平均值

這是我的查詢

SELECT 
memos.sda.num_sda, 
memos.sda.fecha_gen_sda, 
memos.sda.recurso, 
fechas.bodega_recep_inf_1.num_sda, 
fechas.bodega_recep_inf_1.fecha_recep, 
DATEDIFF(fechas.bodega_recep_inf_1.fecha_recep, memos.sda.fecha_gen_sda) 
AS date_difference, 
MONTHNAME(memos.sda.fecha_gen_sda) AS nombre_mes 
FROM memos.sda 
INNER JOIN fechas.bodega_recep_inf_1 
ON memos.sda.num_sda = fechas.bodega_recep_inf_1.num_sda 
GROUP BY memos.sda.num_sda 

和結果:

_________________________________________________________________________________________ 
|num_sda |fecha_gen_sda|recurso | num_sda |fecha_recep |date_difference | nombre_mes| 
------------------------------------------------------------------------------------------- 
|25227 | 31-01-2013 |1RJB070031 | 25227 | 20-02-2013 | 20   | January 
|25228 | 31-01-2013 |1RVRV20015 | 25228 | 07-02-2013 | 7   | January 
|25229 | 31-01-2013 |1RKB010041 | 25229 | 07-02-2013 | 7   | January 
|25231 | 01-02-2013 |1RJB010585 | 25231 | 07-02-2013 | 6   | February 
|25232 | 01-02-2013 |1R¥R¥50080 | 25232 | 11-02-2013 | 10   | February 
|25235 | 01-02-2013 |1R¥R¥50022 | 25235 | 11-02-2013 | 10   | February 
|25237 | 01-02-2013 |3RBS040030 | 25237 | 12-02-2013 | 11   | February 

我所需要的平均對所有列,但單獨的一個月。

所以,如果我使用

avg(DATEDIFF(fechas.bodega_recep_inf_1.fecha_recep, memos.sda.fecha_gen_sda)) AS avg 

不行的,因爲它是唯一的平均水平行而不是完整的列

+0

AVG(DATEDIFF (fechas.bodega_recep_inf_1.fecha_recep,memos.sda.fecha_gen_sda))AS date_difference, – 2013-02-27 13:17:20

回答

0

喜歡的東西

SELECT 
AVG(DATEDIFF(fechas.bodega_recep_inf_1.fecha_recep, memos.sda.fecha_gen_sda) 
AS avg_date_difference 
FROM memos.sda 
INNER JOIN fechas.bodega_recep_inf_1 
ON memos.sda.num_sda = fechas.bodega_recep_inf_1.num_sda 
GROUP BY MONTH(memos.sda.fecha_gen_sda)