柱的标准偏差与日明显

时间:2012-12-17 05:05:20

标签: sql sql-server sql-server-2012

我需要一个查询来查找日期列的标准偏差。我的表有大约30,000行,时间戳每10分钟一次。请使用列timestamp和TF查找以下示例数据。我还需要得到其余列的偏差

Timestamp                  TF
2012-05-17 19:15:35.000    10
2012-05-17 19:25:35.000    10
2012-05-17 19:35:35.000    10
2012-05-17 19:45:35.000    10
2012-05-17 19:55:35.000    10
2012-05-17 20:05:35.000    10
2012-05-17 20:15:35.000    10
2012-05-17 20:26:46.000    10
2012-05-18 00:06:48.000    10
2012-05-18 00:16:48.000    10
2012-05-18 00:26:48.000    10
2012-05-18 00:36:48.000    10
2012-05-18 00:46:48.000    10
2012-05-18 00:56:48.000    10
2012-05-18 01:06:48.000    10
2012-05-18 01:16:48.000    10
2012-05-18 01:26:48.000    10
2012-05-18 01:36:49.000    10

需要输出为

Timestamp        TF
2012-05-17     0
2012-05-18       0
---             --
---             --
---             --
---             --

请帮忙!!在此先感谢!!

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

编辑:我转置DAYDATEDAY返回日期; DATE返回截断到当天的时间。答案已更新,以包含该修复程序。

您需要使用聚合函数,按日分组:

SELECT DATE(Timestamp), STDDEV(TV) FROM MyTable GROUP BY DATE(Timestamp);

我假设有一个DATE()函数。

可能没有STDDEV功能。如果没有STDDEV(x)=SQRT(AVG((x-AVG(x))^2),则可能必须将其实现为已连接的子查询:

SELECT DATE(Timestamp), SQRT(AVG((a.TF-b.mean)*(a.TF-b.mean)) FROM MyTable a LEFT JOIN
    (SELECT DATE(Timestamp) day, AVG(TF) mean FROM MyTable GROUP BY DATE(Timestamp)) b
        ON DATE(a.Timestamp)=b.day 
    GROUP BY b.day;