1

我有一张像

| id | user | bottle |  count |
| 1  | foo  | beer   |    2   |
| 2  | bar  | beer   |    5   |
| 3  | som1 | beer   |    6   |
| 4  | som2 | beer   |    4   |
| 5  | som1 | wine   |    1   |

等等

如何获得每个组(啤酒、葡萄酒等)STDDEV()的价值MIN()MAX()价值? 即我怎么得到count

| bottle |      stddev     |

|  beer  |stddev of 2 and 4|

?

另外,我怎样才能获得像上面这样的查询返回的总行数?

到目前为止我已经尝试过

SELECT STD(count) as stddev, bottle
FROM drinks WHERE count < (
    SELECT MAX(count) FROM drinks)
              AND count > (
    SELECT MIN(count) FROM drinks)
GROUP BY bottle;

但这适用于MAX()整个桌子,而不仅仅是 GROUP BY

4

1 回答 1

3

使用聚合和连接来引入极值,然后将它们过滤掉并计算标准偏差:

select bottle, STDEV(d.count)
from drd.inks d join
     (select bottle, MIN(count) as minc, MAX(count) as maxc
      from drinks
      group by bottle
     ) dsum
     on d.bottle = dsum.bottle and
        d.count not in (dsum.minc, dsum.maxc)
group by d.bottle
于 2013-07-01T15:45:50.037 回答