我有一个数据集。它有我想消除的最大峰值。我怎么做?

时间:2016-07-28 03:33:07

标签: sql sql-server

下面附有数据样本。

CTIME   WEIGHT
7/23/2016 22:49 97.94
7/23/2016 22:48 97.95
7/23/2016 22:48 97.95
7/23/2016 22:48 97.96
7/23/2016 22:48 97.96
7/23/2016 22:48 97.94
7/23/2016 22:48 97.94
7/23/2016 22:48 97.95
7/23/2016 22:48 97.95
7/23/2016 22:48 98
7/23/2016 22:48 98
7/23/2016 22:48 98
7/23/2016 22:48 98
7/23/2016 22:48 98.01
7/23/2016 22:48 98.01
7/23/2016 22:48 97.95
7/23/2016 22:48 97.95
7/23/2016 22:48 98
7/23/2016 22:48 98
7/23/2016 22:48 97.94
7/23/2016 22:48 97.94
7/23/2016 22:47 97.95
7/23/2016 22:47 97.95
7/23/2016 22:47 97.96
7/23/2016 22:47 97.96
7/23/2016 22:47 97.95
7/23/2016 22:47 97.95
7/23/2016 22:47 97.99
7/23/2016 22:47 97.99
7/23/2016 22:47 98.03
7/23/2016 22:47 97.88
7/23/2016 22:47 97.88
7/23/2016 22:47 97.88
7/23/2016 22:47 97.3
7/23/2016 22:47 95.53
7/23/2016 22:47 95.53
7/23/2016 22:47 95.58
7/23/2016 22:47 95.58
7/23/2016 22:47 95.57
7/23/2016 22:47 95.57
7/23/2016 22:47 95.51
7/23/2016 22:46 95.51
7/23/2016 22:46 95.52
7/23/2016 22:46 95.52
7/23/2016 22:46 95.52
7/23/2016 22:46 95.52
7/23/2016 22:46 95.55
7/23/2016 22:46 95.55
7/23/2016 22:46 95.53
7/23/2016 22:46 95.53
7/23/2016 22:46 95.52
7/23/2016 22:46 95.52
7/23/2016 22:46 95.54
7/23/2016 22:46 95.54
7/23/2016 22:46 95.5
7/23/2016 22:46 95.5
7/23/2016 22:46 95.51
7/23/2016 22:46 95.51
7/23/2016 22:46 95.48
7/23/2016 22:46 95.48
7/23/2016 22:46 95.52
7/23/2016 22:45 95.52
7/23/2016 22:45 95.53
7/23/2016 22:45 95.44
7/23/2016 22:45 95.44
7/23/2016 22:45 95.44
7/23/2016 22:45 95.43
7/23/2016 22:45 95.4
7/23/2016 22:45 95.4
7/23/2016 22:45 95.41
7/23/2016 22:45 95.41
7/23/2016 22:45 95.39
7/23/2016 22:45 95.39
7/23/2016 22:45 95.41
7/23/2016 22:45 95.41
7/23/2016 22:45 94.84
7/23/2016 22:45 94.84
7/23/2016 22:45 94.84
7/23/2016 22:45 93.89
7/23/2016 22:45 71.53
7/23/2016 22:45 71.53
7/23/2016 22:44 43.65
7/23/2016 22:44 43.65
7/23/2016 22:44 4.46
7/23/2016 22:44 4.46
7/23/2016 22:44 0.34
7/23/2016 22:44 0.34
7/23/2016 22:44 0.34
7/23/2016 22:44 0.34
7/23/2016 22:44 0.36
7/23/2016 22:44 0.36
7/23/2016 22:44 0.41
7/23/2016 22:44 0.41
7/23/2016 22:44 0.42
7/23/2016 22:44 0.42
7/23/2016 22:44 0.39
7/23/2016 22:44 0.39
7/23/2016 22:44 0.4
7/23/2016 22:44 0.41
7/23/2016 22:44 0.41
7/23/2016 22:44 0.41
7/23/2016 22:43 0.4
7/23/2016 22:43 0.41
7/23/2016 22:43 0.41
7/23/2016 22:43 0.41
7/23/2016 22:43 0.41
7/23/2016 22:43 0.41
7/23/2016 22:43 0.41
7/23/2016 22:43 0.47
7/23/2016 22:43 0.47
7/23/2016 22:43 0.47
7/23/2016 22:43 0.47
7/23/2016 22:43 0.47
7/23/2016 22:43 0.47
7/23/2016 22:43 0.47
7/23/2016 22:43 0.47
7/23/2016 22:43 0.49
7/23/2016 22:43 0.49
7/23/2016 22:43 0.48
7/23/2016 22:43 0.48
7/23/2016 22:43 0.49
7/23/2016 22:42 0.49
7/23/2016 22:42 0.49
7/23/2016 22:42 0.49
7/23/2016 22:42 0.48
7/23/2016 22:42 0.48
7/23/2016 22:42 0.5
7/23/2016 22:42 0.5
7/23/2016 22:42 0.51
7/23/2016 22:42 0.51
7/23/2016 22:42 0.54
7/23/2016 22:42 14.24
7/23/2016 22:42 14.24
7/23/2016 22:42 14.24
7/23/2016 22:42 105.86
7/23/2016 22:42 183.02
7/23/2016 22:42 183.02
7/23/2016 22:42 207.56
7/23/2016 22:42 207.56
7/23/2016 22:42 197.45
7/23/2016 22:42 197.45
7/23/2016 22:41 194.33
7/23/2016 22:41 194.33
7/23/2016 22:41 194.31
7/23/2016 22:41 194.31
7/23/2016 22:41 194.31
7/23/2016 22:41 194.31
7/23/2016 22:41 194.31
7/23/2016 22:41 194.31

如果你看到,有几行(有时行数更多,有时更少)包含值207.我想要捕获的值是194。 数据存储在SQL数据库(2005)中,我希望通过SQL查询获得194的值。

有没有办法做到这一点?

1 个答案:

答案 0 :(得分:0)

你应该找到目前的体重是多少"远"从时间上接近的重量测量值来看。

以下SQL代码可以是一个解决方案:

SELECT [MEASURE_ID], CTIME, [WEIGHT], avg([NEAR_TIME_WEIGHT]) [AVERAGE_WEIGHT]
 FROM
 (
  SELECT a.*, b.[WEIGHT] [NEAR_TIME_WEIGHT]
   FROM CTIME_WEIGHT a
   INNER JOIN CTIME_WEIGHT b
    ON a.[MEASURE_ID]>b.[MEASURE_ID]-8
     AND a.[MEASURE_ID]<b.[MEASURE_ID]+8
 ) sbt
 GROUP BY [MEASURE_ID], CTIME, [WEIGHT]
 HAVING [WEIGHT]*0.99<avg([NEAR_TIME_WEIGHT])
  AND [WEIGHT]*1.01>avg([NEAR_TIME_WEIGHT])
 ORDER BY [MEASURE_ID]

您必须在三列表中复制数据(我将其命名为CTIME_WEIGHT)。该表必须在采样时间之后排序一行(我将此字段命名为[MEASURE_ID])。

我的解决方案在此之前采用8个值,在当前度量之后采用8个值并使其平均值;如果当前值太低(小于99%)或太高(大于101%),则将其丢弃。