当前位置: 代码迷 >> Sql Server >> 小白求指导解决方案
  详细解决方案

小白求指导解决方案

热度:82   发布时间:2016-04-24 10:05:16.0
小白求指导
我想要每10天统计一次频率,直到半年怎么办。频率=天数*人数/人次
select 
 (DATEDIFF(day,min(InDate),'20140906'))*count(distinct deviceid)/count(*) as 频率
from  [dbo].[DeviceDetails] where InDate<'20140906' and InDate>'20120826'
 union
 select 
 (DATEDIFF(day,min(InDate),'20140906'))*count(distinct deviceid)/count(*) as 频率
from  [dbo].[DeviceDetails] where InDate<'20140826' and InDate>'20140816'

一直union下去,

等,好多union好麻烦,有好没有简单的写法 


------解决方案--------------------
月份做变量 写动态语句或者写WHILE循环 不过效率或许还不如你这个高。
------解决方案--------------------
try this,

select (DATEDIFF(day,min(InDate),'20140906'))*count(distinct deviceid)/count(*) as 频率
 from dbo.DeviceDetails
 group by datediff(day,InDate,getdate())/10
  相关解决方案