select avg(error) as total,
avg(case when timestamp > cast(getdate() as date) then error end) as today,
avg(case when timestamp > dateadd(day, -6, cast(getdate() as date) then error end) as this_week,
. . .
from t;
我不知道你对“今天”、“本周”等的确切定义。上面是条件聚合的一个例子。
t
只有一次。
如果您想在单独的行中显示,您可以取消打印数据。我的首选方法是
cross apply
:
with t as (
select avg(error) as total,
avg(case when timestamp > cast(getdate() as date) then error end) as today,
avg(case when timestamp > dateadd(day, -6, cast(getdate() as date) then error end) as this_week,
. . .
from t
)
select v.*
from t cross apply
(values ('Total', total), ('Today', today), ('This week', this_week), . . .
) v(period, error);