我有两列。第一个是“开始日期”,第二个是“结束日期”。然后使用查询计算两者之间的天数差异。 我想知道如何计算此结果的月平均值。
这是我的查询
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
我需要所有列的平均值,但是按MONTH分开。
所以,如果我使用
avg(DATEDIFF( fechas.bodega_recep_inf_1.fecha_recep, memos.sda.fecha_gen_sda)) AS avg
不起作用,因为它只是行的平均值而不是完整列
答案 0 :(得分: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 )