如何在 SQL Server 中的日期列中查找平均值 [英] How to find the average value in a column of dates in SQL Server

查看:40
本文介绍了如何在 SQL Server 中的日期列中查找平均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有日期列的 5000 条记录表.

I have a table of 5000 records with a date column.

我如何找到这些日期的平均值.我试过AVG(datefield),但它说Operand data type datetime is invalid for avg operator

How do I find the average of those dates. I've tried AVG(datefield), but it says Operand data type datetime is invalid for avg operator

推荐答案

如果你只想平均日期:

SELECT CAST(AVG(CAST(datefield AS INT)) AS DATETIME) FROM dates;

如果你想考虑时间:

SELECT CAST(AVG(CAST(datefield AS FLOAT)) AS DATETIME) FROM dates;

请参阅 fiddle 进行测试.

这篇关于如何在 SQL Server 中的日期列中查找平均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆