使用regr_slope时不是单组分组功能

时间:2010-06-13 23:24:06

标签: sql oracle ora-00937

我试着跑:

 select
  year,
  regr_slope(sum(sale_count),year) as slope,
from products
group by year

它抛出“ 00937.00000 - ”不是单组组功能“ ”。当我从select子句中删除年份问题消失。我不应该能够选择我正在分组的列吗?

Oracle 11.2 sqldeveloper

求助!

2 个答案:

答案 0 :(得分:2)

这是因为你试图使用一个函数(REGR_SLOPE),它可以是另一个聚合(SUM)结果的聚合(或分析)函数 - 使用:

  SELECT x.year,
         REGR_SLOPE(sum_sales, x.year) AS slope
    FROM (SELECT y.year,
                 SUM(y.sale_count) AS sum_sales
            FROM PRODUCTS y
        GROUP BY y.year) x
GROUP BY x.year

替代使用WITH子句(Oracle 9i +):

WITH sums AS (
     SELECT y.year,
            SUM(y.sale_count)
       FROM PRODUCTS y
   GROUP BY y.year)
  SELECT x.year,
         REGR_SLOPE(sum_sales, x.year) AS slope
    FROM sums x
GROUP BY x.year

答案 1 :(得分:1)

你这样做了吗?

select
   a.year
 , regr_slope(a.sale_count,a.year) as slope,
from (SELECT year
           , sum(sale_count) sale_count
        FROM products
      GROUP BY year) a