我尝试将聚合函数列添加到现有结果集中。我尝试过OVER(),UNION的变种,但无法找到解决方案。
示例当前结果集:
ID ATTR VALUE
1 score 5
1 score 7
1 score 9
示例所需结果集:
ID ATTR VALUE STDDEV (score)
1 score 5 2
1 score 7 2
1 score 9 2
谢谢
答案 0 :(得分:1)
看起来像你之后:
stddev(value) over (partition by attr)
stddev(value) over (partition by id, attr)
它只取决于你需要分区的内容。基于样本数据,attr应该足够;但我可以看到ID和attr。
示例:
With CTE (ID, Attr, Value) as (
SELECT 1, 'score', 5 from dual union all
SELECT 1, 'score', 7 from dual union all
SELECT 1, 'score', 9 from dual union all
SELECT 1, 'Z', 1 from dual union all
SELECT 1, 'Z', 5 from dual union all
SELECT 1, 'Z', 8 from dual)
SELECT A.*, stddev(value) over (partition by attr)
FROM cte A
ORDER BY attr, value
DOCS表明通过向分析添加订单,可以获得每条记录的累积标准差。
给我们:
+----+-------+-------+------------------------------------------+
| ID | attr | value | stdev |
+----+-------+-------+------------------------------------------+
| 1 | Z | 1 | 3.51188458428424628280046822063322249225 |
| 1 | Z | 5 | 3.51188458428424628280046822063322249225 |
| 1 | Z | 8 | 3.51188458428424628280046822063322249225 |
| 1 | score | 5 | 2 |
| 1 | score | 7 | 2 |
| 1 | score | 9 | 2 |
+----+-------+-------+------------------------------------------+