[英]How to get the average of a DATEDIFF()?
I have the following query: 我有以下查询:
SELECT
DATEDIFF(day, DateUsed, DateExpires) AS DaysBetweenExpirationAndUse
FROM tblOffer
How could i get the average number of days from the DaysBetweenExpirationAndUse
column? 我如何从
DaysBetweenExpirationAndUse
列中获取平均天数?
This should work fine: 这应该工作正常:
SELECT AVG(DATEDIFF(d, DateUsed, DateExpires)) AS AvgDaysBetweenExpirationAndUse
FROM tbl
If you want decimal places in your AVG: 如果要在AVG中保留小数位数:
SELECT AVG(DATEDIFF(d, DateUsed, DateExpires)*1.0) AS AvgDaysBetweenExpirationAndUse
FROM tbl
If you want to select the AVG
and other fields too, without grouping: 如果您也想选择
AVG
和其他字段而不进行分组:
SELECT *
,AVG(DATEDIFF(d, DateUsed, DateExpires)*1.0) OVER() AS AvgDaysBetweenExpirationAndUse
FROM tbl
this sql could execute? 这个SQL可以执行?
SELECT AVG(t.a) from
(
SELECT DATEDIFF(d, DateUsed, DateExpires) AS a
FROM tbl
) as t
this is my test: 这是我的测试:
but good answer is: 但好的答案是:
SELECT AVG(DATEDIFF(d, DateOne, DateTwo)*1.0) AS avgDate
FROM Test
SELECT AVG(DATEDIFF(d, DateUsed, DateExpires)) FROM tbl
should work fine. 应该工作正常。 Note, that since
DATEDIFF
returns an integer value, the result also will be an integer. 请注意,由于
DATEDIFF
返回整数值,因此结果也将是整数。
If you want the "exact" (as far as floating point gets) average, use 如果要“精确”(就浮点而言)平均值,请使用
SELECT AVG(CAST(DATEDIFF(d, DateUsed, DateExpires) AS FLOAT)) FORM tbl
DATEDIFF
function has nothing to do with this problem of calculating the average(AVG) of INT values. DATEDIFF
函数与计算INT值的平均值(AVG)无关。 The behavior of SQL Server is different than others servers but is according to ANSI SQL standard SQL Server的行为与其他服务器不同,但符合ANSI SQL标准
If AVG is specified and DT is exact numeric (personal note: INT is an exact numeric), then the data type of the result is exact numeric with implementation- defined precision not less than the precision of DT and implementation-defined scale not less than the scale of DT.
如果指定了AVG并且DT是精确数字(请注意:INT是一个精确数字),则结果的数据类型是精确数字,实现定义的精度不小于DT的精度,实现定义的小数位数不小于DT的规模。
which is (in my opinion) not to helpful in this case. (在我看来)在这种情况下没有帮助。 Most of people will expect that
AVG(INT value)
will result in a NUMERIC/DECIMAL/FLOAT/REAL
result but the result will be a truncated INT value. 大多数人会期望
AVG(INT value)
会导致NUMERIC/DECIMAL/FLOAT/REAL
结果,但结果将是截断的INT值。 The solution is to convert [TINY|SMALL|BIT]INT values to DECIMAL/NUMERIC
using CONVERT(DECIMAL(p,s)/NUMERIC(p,s), ...)
or by simply multiply with 1.
or 1.0
. 解决方案是使用
CONVERT(DECIMAL(p,s)/NUMERIC(p,s), ...)
或简单地乘以1.
或1.0
将[TINY | SMALL | BIT] INT值转换为DECIMAL/NUMERIC
。
Example: 例:
SELECT ColINT, SQL_VARIANT_PROPERTY(ColINT,'BaseType') AS Col_DataType
FROM
(
SELECT 10 AS ColINT UNION ALL SELECT 9
) x;
/*
ColINT Col_DataType
----------- ------------
10 int
9 int
*/
SELECT AVG(ColINT) AS Avg#1, SQL_VARIANT_PROPERTY(AVG(ColINT),'BaseType') AS Avg#1_DataType
FROM
(
SELECT 10 AS ColINT UNION ALL SELECT 9
) x;
/*
Avg#1 Avg#1_DataType
----------- --------------
9 int
*/
SELECT AVG(ColINT) AS Avg#2, SQL_VARIANT_PROPERTY(AVG(ColINT),'BaseType') AS Avg#2_DataType
FROM
(
SELECT 10 AS ColINT UNION ALL SELECT 9
) x;
/*
Avg#2 Avg#2_DataType
----------- --------------
9 int
*/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.