【发布时间】:2021-05-18 03:22:18
【问题描述】:
我有一个表收入的收入值数据集。
我需要计算当前日期前 60 天的每一天的标准偏差。 当计算标准偏差时,我运行它如下:
SELECT
[Date],Brand,[Country],[Marketing Channel],
STDEV(Revenue) OVER (PARTITION BY Brand, Country,[Marketing Channel]
ORDER BY [Date] ROWS BETWEEN 2 PRECEDING and 1 PRECEDING)
as STD
FROM RevTable
问题是分区内的极值会使标准偏差向上或向下倾斜。我希望过滤分区本身的极值。
如何过滤掉这种结构中的极值(我想到的是基于百分比的过滤)?
| Date | Brand | Country | Marketing Channel | Revenue |
|---|---|---|---|---|
| 1/1/2021 | FunGames | Canada | SEO | $ 3,578,834 |
| 1/1/2021 | FunGames | Canada | Social Networks | $ 90,000 |
| 1/1/2021 | FunGames | Netherlands | SEO | $ 682,943 |
| 1/2/2021 | FunGames | Canada | SEO | $ 3,731,849 |
| 1/2/2021 | FunGames | Canada | Social Networks | $ 257 |
| 1/2/2021 | FunGames | Netherlands | SEO | $ 627,272 |
| 1/3/2021 | FunGames | Canada | SEO | $ 2,418,136 |
| 1/3/2021 | FunGames | Canada | Social Networks | $ 40 |
| 1/3/2021 | FunGames | Netherlands | SEO | $ 479,642 |
| 1/4/2021 | FunGames | Canada | SEO | $ 2,231,254 |
| 1/4/2021 | FunGames | Canada | Social Networks | $ - |
| 1/4/2021 | FunGames | Netherlands | SEO | $ 635,715 |
| 1/5/2021 | FunGames | Canada | SEO | $ 2,686,366 |
| 1/5/2021 | FunGames | Canada | Social Networks | $ 177 |
| 1/5/2021 | FunGames | Netherlands | SEO | $ 499,026 |
| 1/5/2021 | FunGames | Netherlands | Social Networks | $ - |
| 1/6/2021 | FunGames | Canada | SEO | $ 2,096,472 |
| 1/6/2021 | FunGames | Canada | Social Networks | $ 465 |
| 1/6/2021 | FunGames | Netherlands | SEO | $ 653,359 |
| 1/6/2021 | FunGames | Netherlands | Social Networks | $ - |
| 1/7/2021 | FunGames | Canada | SEO | $ 2,962,476 |
| 1/7/2021 | FunGames | Canada | Social Networks | $ 663 |
| 1/7/2021 | FunGames | Netherlands | SEO | $ 747,990 |
| 1/8/2021 | FunGames | Canada | SEO | $ 3,092,163 |
| 1/8/2021 | FunGames | Canada | Social Networks | $ 156 |
| 1/8/2021 | FunGames | Netherlands | SEO | $ 655,688 |
| 1/8/2021 | FunGames | Netherlands | Social Networks | $ - |
| 1/9/2021 | FunGames | Canada | SEO | $ 3,110,117 |
| 1/9/2021 | FunGames | Canada | Social Networks | $ 153 |
| 1/9/2021 | FunGames | Netherlands | SEO | $ 571,313 |
| 1/9/2021 | FunGames | Netherlands | Social Networks | $ - |
| 1/10/2021 | FunGames | Canada | SEO | $ 3,024,675 |
| 1/10/2021 | FunGames | Canada | Social Networks | $ 68 |
| 1/10/2021 | FunGames | Netherlands | SEO | $ 462,699 |
| 1/10/2021 | FunGames | Netherlands | Social Networks | $ 563 |
| 1/11/2021 | FunGames | Canada | SEO | $ 2,552,153 |
| 1/11/2021 | FunGames | Canada | Social Networks | $ 275 |
| 1/11/2021 | FunGames | Netherlands | SEO | $ 725,954 |
期望的输出:
| Date | Brand | Country | Marketing Channel | STD |
|---|---|---|---|---|
| 1/1/2021 | FunGames | Canada | SEO | 522429 |
| 1/1/2021 | FunGames | Netherlands | SEO | 97543 |
| 1/2/2021 | FunGames | Canada | Social Networks | 27069 |
| 1/2/2021 | FunGames | Netherlands | Social Networks | 251.66 |
但分区内没有异常值
【问题讨论】:
-
鉴于您的原始查询正在查找 60 天的数据,而您的虚拟数据不包含任何接近该数据的地方,您能否更新您的示例脚本和所需输出以提供与您的虚拟数据匹配的值?例如,在虚拟数据上运行当前脚本实际上不会返回 1 月 1 日和 2 日的 any 值。
-
就我个人而言,如果您可以将脚本更新为只需要 3-4 天的数据,然后在虚拟数据中包含这几天内的几个异常值示例,我会发现它很有用。然后,您可以从中提供当前输出和所需输出。
-
当然。原始数据集太大,所以我理解您为什么没有收到数据。我会尽量缩短它
-
虽然脚本现在引用的天数减少了,但我建议您在示例数据上实际运行它,因为它仍然返回无意义的值。同样,拥有 Current 和 Desired 输出也会非常有帮助
-
什么是“极值”?
标签: sql sql-server tsql window-functions