使用GROUP BY的标准偏差的SQL查询?

时间:2015-01-20 20:31:21

标签: sql function group-by monetdb

我遇到了标准偏差函数的问题(具体来说是MonetDB中的stddev_samp)。我尝试了以下查询但没有成功:

    select industry, avg(marketcap) as industryavg, stddev_samp(marketcap) as industrysd from cumulativeview group by industry
    select  stddev_samp(marketcap) as industrysd from cumulativeview group by industry

每个都给了我一个非常奇怪的异常,似乎stddev函数不能按子集对组进行操作,但是单独使用avg函数似乎可以在子集上正常工作,如下面的查询:

    select industry, avg(marketcap) as industryavg  from cumulativeview group by industry

当我使用where子句而不是group by:

时,标准偏差函数可以正常工作
    select  stddev_samp(marketcap) as industrysd from cumulativeview where industry='Diversified Investments'

是否有另一种方法来编写一个查询,它可以同时为每个行业提供平均值和标准差,而不必为每个行业编写单独的查询?我很困惑为什么普通函数与group by和stddev不起作用...

1 个答案:

答案 0 :(得分:6)

刚刚测试了MonetDB的Oct2014版本。从您的查询中,我推断出以下表结构:

CREATE TABLE cumulativeview (industry string, company string, marketcap double);

一些示例数据:

INSERT INTO cumulativeview VALUES ('Automotive', 'Daimler', 84784.62), 
('Automotive', 'BMW', 66852.15), ('Automotive', 'VW', 95378.54), ('Chemical', 'BASF', 70438.13), ('Chemical', 'Bayer', 105766.62);

您的查询

SELECT industry, avg(marketcap) AS industryavg, stddev_samp(marketcap) AS industrysd FROM cumulativeview GROUP BY industry;

结果

+------------+--------------------------+--------------------------+
| industry   | industryavg              | industrysd               |
+============+==========================+==========================+
| Automotive |       82338.436666666661 |       14419.659887918069 |
| Chemical   |                88102.375 |       24981.014848081126 |
+------------+--------------------------+--------------------------+

正如安东尼所说,这个错误似乎已得到解决。