我正在输出一些数据,我想获取一些数字的每日平均值。
正如您所看到的,我要做的是计算接收/结果的行数(考虑行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
尝试计数有区别,无济于事
SUM(NY_dayscore * cAttendance) ) / count(distinct calendar_date)
我的原始代码很长,请cba将其全部发布。因此,只需尝试发布少量示例代码以获取有关该问题的指导。
答案 0 :(得分:0)
我认为您需要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
答案 1 :(得分:0)
是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
答案 2 :(得分:0)
在SQL Server 2012+中,您将使用累计平均值:
/home/pi/Desktop/MelloBot/commands/pat.js:72
help: `<usermention> / Your daily dose of pat`
^
SyntaxError: Invalid regular expression: missing /
at createScript (vm.js:80:10)
at Object.runInThisContext (vm.js:139:10)
at Module._compile (module.js:616:28)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Module.require (module.js:596:17)
at reloadParent (/home/pi/Desktop/MelloBot/node_modules/require-
reload/reload.js:29:23)
at reload (/home/pi/Desktop/MelloBot/node_modules/require-
reload/reload.js:59:12)
在SQL Server 2008中,这更加困难:
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;