我拿着两个日期字段之间的平均分钟数 - 在最近5天xreports - 这工作得很好:尾均值的计算(平均)列
select avg(datediff(minute, findDateTime, reportClosedDateTime)) as avd from xreports
where findDateTime > dateadd(day, -5, getdate())
然而,也有不少离群歪斜这个平均值,所以我想采用四分位数范围 - 即失去最高和最低的25%。我发现这个article。
解释如何做一个真正的列,但我不能得到它的2列之间有区别的AVG()的工作 - 这是最好的,我可以这样做:
declare @pp float
set @pp = .25
select avg(datediff(minute, findDateTime, reportClosedDateTime)) as avd
from xreports xr
where findDateTime > dateadd(day, -5, getdate())
and
(select count(*) from xReports xr1
where xr1.finddatetime <= xr.finddatetime) >=
(select @pp*count(*) from xReports)
and
(select count(*) from xReports xr2
where xr2.avd >= xr.avd) >=
(select @pp*count(*) from xReports)
但是,“avd”列未被识别。
我该怎么做?
thx。
无法识别列'avd',因为您无法在同一查询级别中引用别名。把它放在子查询中。无论如何,这个查询有优化空间,但只是给你一个杆。 –
我不太确定该怎么做?你会如何建议优化它? – niico