Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
多个时间段的滑动窗口平均值-SQL Server_Sql_Sql Server_Aggregate Functions - Fatal编程技术网

多个时间段的滑动窗口平均值-SQL Server

多个时间段的滑动窗口平均值-SQL Server,sql,sql-server,aggregate-functions,Sql,Sql Server,Aggregate Functions,我有一个SQL查询,返回在过去30天内注册的用户的平均注册时间: select avg(datediff(dd, acquisitiontime,createdate)) from users where createdate > getdate() - 30 and acquisitionmedium = 'cpc' and acquisitionsource = 'google' and acquisitiontime is not null 我想看看这是如何随着时间的推移而改变

我有一个SQL查询,返回在过去30天内注册的用户的平均注册时间:

select avg(datediff(dd, acquisitiontime,createdate))  from users where 
createdate > getdate() - 30 and
acquisitionmedium = 'cpc' and
acquisitionsource = 'google' and
acquisitiontime is not null
我想看看这是如何随着时间的推移而改变的

如何更改此查询,以便生成一个表(月份,该月份注册的平均时间)?

select
DATEADD(月,-n.number,getdate())自此日期起一个月,
本月平均值(datediff(dd、acquisitiontime、createdate)
来自用户
join master..spt_值n在n.type='P'和n.number之间,介于1和24之间
其中createdate>DATEADD(月,-n.number,getdate())

如果您想要日历月,那么只需稍微修改从数字序列返回的日期范围即可。
select
    DATEADD(month, -n.number, getdate()) OneMonthFromThisDate,
    avg(datediff(dd, acquisitiontime, createdate)) AverageInThisMonth
from users
join master..spt_values n on n.type='P' and n.number between 1 and 24
where createdate >  DATEADD(month, -n.number, getdate())
  and createdate <= DATEADD(month, 1-n.number, getdate())
  and acquisitionmedium = 'cpc'
  and acquisitionsource = 'google'
  and acquisitiontime is not null
group by n.number, DATEADD(month, -n.number, getdate())
order by n.number