嗨,我尝试运行:
select
year,
regr_slope(sum(sale_count),year) as slope,
from products
group by year
它抛出“ 00937. 00000 - “不是单组组函数” “。当我从选择子句中删除年份时,问题消失了。我不应该能够选择我要分组的列吗?
Oracle 11.2 sqldeveloper
求救!
这是因为您正在尝试使用一个函数(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
你试过这样吗?
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