每个月平均每周通话次数

时间:2017-03-13 22:08:48

标签: sql-server average

您好,我希望每个月能获得每周平均一次的电话 此外,每个季度每周平均呼叫次数 此外,每年平均每周通话次数

我需要对上述每一个进行查询。谢谢您的帮助!

以下是原始数据集:

Name    Call        CallDate
Jack    Phone Call  2017-03-10
Laura   Meeting     2017-03-08
Helen   Phone Call  2017-02-02
Sam     Video Conf  2017-02-10
David   Skype       2017-01-01
Jack    Skype       2017-01-14
Laura   Video Conf  2014-03-09
Helen   Meeting     2017-02-12
Sam     Meeting     2017-02-12
David   Meeting     2017-03-09
David   Video Conf  2017-02-25
David   Skype       2017-02-20
Jack    Skype       2017-02-20
Jack    Phone Call  2017-03-10
Laura   Phone Call  2017-02-07
Laura   Skype       2017-02-07
Sam     Phone Call  2017-03-04
Sam     Phone Call  2017-03-04
Sam     Skype       2017-03-10

1 个答案:

答案 0 :(得分:1)

因为你的问题很奇怪所以尽力而为。

这将计算每个月的通话数除以月中的天数并乘以7

;WITH CTE as
(
  SELECT 
    dateadd(month, datediff(month,0, cast(CallDate as datetime)), 0) month
  FROM yourtable
)
SELECT
  -- day(eomonth(month)) calculates the number of days in the month
  count(*)*7./day(eomonth(month)) avgweeklycallsmonth,
  month
FROM CTE
GROUP BY month

这将计算每个季度的电话数除以季度中的天数并乘以7

;WITH CTE as
(
  SELECT dateadd(quarter, datediff(quarter,0, CallDate), 0) quarter
  FROM yourtable
)
SELECT 
  count(*)*7./
    (datediff(day,dateadd(year, datediff(year, 0, quarter), 0),
      dateadd(month,3, quarter))) avgweeklycallsquarter, 
  quarter
FROM CTE
GROUP BY quarter