计算distriubtion的平均值

时间:2016-07-14 22:50:22

标签: sql sql-server

说我有一张包含以下信息的表格:

持有期间计算为开始时间与天数之间的天数。结束日期

   ID   Start_Date   End_Date   Reading    Holding_Period   

   1     20140501     20160410    55        710
   2     20150407     20151231    82        268
   3     20160104     20160529    110       146
   4     20140209     20160209    39        730
   5     20131211     20151101    25        690

对于这个数据集,我使用下面的频率分布和平均保持期(hp)。

 select YEAR(enddate)*100 + MONTH(enddate) as salemonth
 ,avg(hp) as hp
 ,sum(reading_0_50) as reading_0_50
 ,sum(reading_50_100) as reading_50_100
 ,sum(reading_100_150) as reading_100_150 
 from
 (
  select x.id
  ,x.enddate as enddate
  ,sum(x.hp) as hp
  ,sum(x.reading_0_50) as reading_0_50
  ,sum(x.reading_50_100) as reading_50_100
  ,sum(x.reading_100_150) as reading_100_150
  from
  (  select id
     ,hp
     ,enddate
     ,case when reading >= 0 and reading <= 50 THEN 1 ELSE 0 END as   reading_0_50
     ,case when reading > 50 and reading<= 100 THEN 1 ELSE 0 END  as reading_50_100
     ,case when reading > 100 and reading<= 150 THEN 1 ELSE 0 END as reading_100_150
   from readings
  )x
  group by x.id,x.enddate
 ) p
 group by YEAR(enddate)*100 + MONTH(enddate)

这给了我输出

 salemonth  hp  reading_0_50    reading_50_100  reading_100_150

 201511    690    1                 0             0
 201512    268    0                 1             0
 201602    730    1                 0             0
 201604    710    0                 1             0
 201605    146    0                 0             1

然而,平均惠普是整个月

我的问题是:。如果我要通过Binned Bucket获得普通HP,我该如何在SQL中执行此操作?

提前感谢您阅读。

1 个答案:

答案 0 :(得分:0)

select YEAR(enddate)*100 + MONTH(enddate) as salemonth
 ,avg(hp) as hp
 ,sum(reading_0_50_count) as reading_0_50
 ,sum(reading_50_100_count) as reading_50_100
 ,sum(reading_100_150_count) as reading_100_150 
 ,case when sum(reading_0_50_count) = 0 then 0 else sum(reading_0_50_total) / sum(reading_0_50_count) end as reading_0_50_avg
 ,case when sum(reading_50_100_count) = 0 then 0 else sum(reading_50_100_total) / sum(reading_50_100_count) end as reading_50_100_avg
 ,case when sum(reading_100_150_count) = 0 then 0 else sum(reading_100_150_total) / sum(reading_100_150_count) end  as reading_100_150_avg
 from
 (
  select x.id
  ,x.enddate as enddate
  ,sum(x.hp) as hp
  ,sum(x.reading_0_50_count) as reading_0_50_count
  ,sum(x.reading_50_100_count) as reading_50_100_count
  ,sum(x.reading_100_150_count) as reading_100_150_count
  ,sum(x.reading_0_50_total) as reading_0_50_total
  ,sum(x.reading_50_100_total) as reading_50_100_total
  ,sum(x.reading_100_150_total) as reading_100_150_total
  from
  (  select id
     ,hp
     ,enddate
     ,case when reading >= 0 and reading <= 50 THEN 1 ELSE 0 END as   reading_0_50_count
     ,case when reading > 50 and reading<= 100 THEN 1 ELSE 0 END  as reading_50_100_count
     ,case when reading > 100 and reading<= 150 THEN 1 ELSE 0 END as reading_100_150_count

     ,case when reading >= 0 and reading <= 50 THEN hp ELSE 0 END as   reading_0_50_total
     ,case when reading > 50 and reading<= 100 THEN hp ELSE 0 END  as reading_50_100_total
     ,case when reading > 100 and reading<= 150 THEN hp ELSE 0 END as reading_100_150_total
   from readings
  )x
  group by x.id,x.enddate
 ) p
 group by YEAR(enddate)*100 + MONTH(enddate)