如何根据 SQL 的汇总结果计算每日平均值?
How to calculate daily average from aggregate results with SQL?
我正在输出一些数据,我想提取一些数字的日平均值。
如您所见,我想做的是计算行数 received/results(考虑行 ID),然后将其除以日值以获得每日平均值。(30/1 ) , (64/2) 等我已经尝试了所有方法,但我用这个 运行 挡住了墙。
就目前而言,我猜想使这项工作需要某种子查询。我只是不知道如何获取用于除法的日期(行号 1、2、3、4 等)。
SELECT calendar_date, SUM(NY_dayscore * cAttendance)
FROM vw_Appointments
WHERE status = 'Confirmed'
Group by calendar_date
尝试使用 distinct 进行计数,但无济于事
SUM(NY_dayscore * cAttendance) ) / count(distinct calendar_date)
我的原始代码很长,cba 到 post 全部。所以只是尝试 post 一个小的示例代码来获得关于这个问题的指导。
我觉得你需要sum(showed) over (..)/row_number() over (..)
WITH Table1(date, showed) AS
(
SELECT '2019-01-02', 30 UNION ALL
SELECT '2019-01-03', 34 UNION ALL
SELECT '2019-01-03', 41 UNION ALL
SELECT '2019-01-04', 48
)
SELECT date,
sum(showed) over (order by date) /
row_number() over (order by date)
as daily_average
FROM Table1
GROUP BY showed, date;
date daily_average
2019-01-02 30
2019-01-03 52
2019-01-03 35
2019-01-04 38
你是不是ROW_NUMBER()
不见了?
SELECT
calendar_date,
SUM(NY_dayscore * cAttendance) / (ROW_NUMBER() OVER (ORDER BY calendar_date ASC)) AS average
FROM vw_Appointments
WHERE status = 'Confirmed'
GROUP BY calendar_date
ORDER BY calendar_date
在 SQL Server 2012+ 中,您将使用累积平均值:
select calendar_date, sum(NY_dayscore * cAttendance),
avg(sum(NY_dayscore * cAttendance)) over (order by calendar_date) as running_average
from vw_appointments a
where status = 'Confirmed'
group by calendar_date
order by calendar_date;
在 SQL Server 2008 中,这更困难:
with a as (
select calendar_date, sum(NY_dayscore * cAttendance) as showed
from vw_appointments a
where status = 'Confirmed'
group by calendar_date
)
select a.*, a2.running_average
from a outer apply
(select avg(showed) as running_average
from a a2
where a2.calendar_date <= a.calendar_date
) a2
order by calendar_date;
我正在输出一些数据,我想提取一些数字的日平均值。
如您所见,我想做的是计算行数 received/results(考虑行 ID),然后将其除以日值以获得每日平均值。(30/1 ) , (64/2) 等我已经尝试了所有方法,但我用这个 运行 挡住了墙。
就目前而言,我猜想使这项工作需要某种子查询。我只是不知道如何获取用于除法的日期(行号 1、2、3、4 等)。
SELECT calendar_date, SUM(NY_dayscore * cAttendance)
FROM vw_Appointments
WHERE status = 'Confirmed'
Group by calendar_date
尝试使用 distinct 进行计数,但无济于事
SUM(NY_dayscore * cAttendance) ) / count(distinct calendar_date)
我的原始代码很长,cba 到 post 全部。所以只是尝试 post 一个小的示例代码来获得关于这个问题的指导。
我觉得你需要sum(showed) over (..)/row_number() over (..)
WITH Table1(date, showed) AS
(
SELECT '2019-01-02', 30 UNION ALL
SELECT '2019-01-03', 34 UNION ALL
SELECT '2019-01-03', 41 UNION ALL
SELECT '2019-01-04', 48
)
SELECT date,
sum(showed) over (order by date) /
row_number() over (order by date)
as daily_average
FROM Table1
GROUP BY showed, date;
date daily_average
2019-01-02 30
2019-01-03 52
2019-01-03 35
2019-01-04 38
你是不是ROW_NUMBER()
不见了?
SELECT
calendar_date,
SUM(NY_dayscore * cAttendance) / (ROW_NUMBER() OVER (ORDER BY calendar_date ASC)) AS average
FROM vw_Appointments
WHERE status = 'Confirmed'
GROUP BY calendar_date
ORDER BY calendar_date
在 SQL Server 2012+ 中,您将使用累积平均值:
select calendar_date, sum(NY_dayscore * cAttendance),
avg(sum(NY_dayscore * cAttendance)) over (order by calendar_date) as running_average
from vw_appointments a
where status = 'Confirmed'
group by calendar_date
order by calendar_date;
在 SQL Server 2008 中,这更困难:
with a as (
select calendar_date, sum(NY_dayscore * cAttendance) as showed
from vw_appointments a
where status = 'Confirmed'
group by calendar_date
)
select a.*, a2.running_average
from a outer apply
(select avg(showed) as running_average
from a a2
where a2.calendar_date <= a.calendar_date
) a2
order by calendar_date;