SQL Server:所有月份的平均每天

时间:2017-12-19 11:34:00

标签: sql-server tsql

我需要显示一个折线图,显示从1到31的月份日期。在图表上,它需要绘制一条线,显示所有月份每天使用的SMS点数的平均数量。

我的数据如下:

**STATUS      StatusDate**
DELIVRD     2015-03-16 11:57:47.000
DELIVRD     2015-04-16 13:02:57.000
DELIVRD     2015-03-20 16:02:08.000
DELIVRD     2015-04-20 16:08:41.000
DELIVRD     2015-04-20 16:35:11.000
DELIVRD     2015-03-21 07:43:49.000
DELIVRD     2015-04-21 08:20:09.000
DELIVRD     2015-03-21 11:12:00.000
DELIVRD     2015-04-21 11:20:13.000
DELIVRD     2015-04-21 12:51:26.000

需要绘制线条,显示所有月份的第16个等的平均数。

我正在使用Entity Framework和C#。 JQuery方面正在使用来自bootstrap管理主题的ChartJS。

这就是结果应该是这样的: enter image description here

2 个答案:

答案 0 :(得分:1)

这是一个查询,它将按天汇总您的数据,随时准备提供给您选择的报告包:

WITH t AS(
 SELECT
  CAST(statusdate as date) as da,
  COUNT(CASE WHEN status = 'DELIVRD' THEN 1 END) as count_delivered,
  COUNT(CASE WHEN status = 'FAILED' THEN 1 END) as count_failed -- or similar etc
 FROM
  your_table
 GROUP BY
  CAST(statusdate as date)
)

SELECT
  da,
  AVG(count_delivered),
  AVG(count_failed)
FROM
  t
GROUP BY
  da

答案 1 :(得分:1)

你想要那样的东西;

select DATEPART(DAY,StatusDateD) as [Day], AVG([count]) from (
select 
cast(StatusDate as date) as StatusDateD, 
count(*) as [count]
from Table
group by cast(StatusDate as date)
) Records group by DATEPART(DAY,StatusDateD)
order by [Day]