我很难让我的oracle开发人员查询正确输出。当我有avg函数时,它不会给我一个组组错误。当我拿出来它工作正常。我尝试过使用group而不是order by但是它告诉我它不是按表达式分组。
SELECT LGBRAND.BRAND_ID, LGBRAND.BRAND_NAME, AVG(LGPRODUCT.PROD_PRICE)AS AVGER
FROM LGPRODUCT, LGBRAND
WHERE LGPRODUCT.BRAND_ID = LGBRAND.BRAND_ID
ORDER BY BRAND_NAME;
答案 0 :(得分:13)
如果在查询中包含聚合函数(如avg,sum),则必须按所有未聚合的列进行分组。
SELECT LGBRAND.BRAND_ID, LGBRAND.BRAND_NAME, AVG(LGPRODUCT.PROD_PRICE)AS AVGER
FROM LGPRODUCT, LGBRAND
WHERE LGPRODUCT.BRAND_ID = LGBRAND.BRAND_ID
GROUP BY
LGBRAND.BRAND_ID,
LGBRAND.BRAND_NAME
ORDER BY BRAND_NAME
答案 1 :(得分:2)
SELECT LGBRAND.BRAND_ID, LGBRAND.BRAND_NAME, AVG(LGPRODUCT.PROD_PRICE)AS AVGER
FROM LGPRODUCT, LGBRAND
WHERE LGPRODUCT.BRAND_ID = LGBRAND.BRAND_ID
GROUP BY LGBRAND.BRAND_ID, LGBRAND.BRAND_NAME
ORDER BY BRAND_NAME;
或强>
SELECT LGBRAND.BRAND_ID, LGBRAND.BRAND_NAME, AVG(LGPRODUCT.PROD_PRICE)AS AVGER
FROM LGPRODUCT INNER JOIN LGBRAND
ON LGPRODUCT.BRAND_ID = LGBRAND.BRAND_ID
GROUP BY LGBRAND.BRAND_ID, LGBRAND.BRAND_NAME
ORDER BY BRAND_NAME;
注意强>
每当在SUM, COUNT, AVG, MIN, MAX,
中使用聚合函数(SELECT
..)时,该SELECT中未包含在任何聚合函数中的所有其他列都必须包含GROUP BY
子句
答案 2 :(得分:0)
您正在使用AGGREGATION函数,为此,您必须对所有必须聚合的数据进行分组。
SELECT LGBRAND.BRAND_ID, LGBRAND.BRAND_NAME, AVG(LGPRODUCT.PROD_PRICE) AS AVGER
FROM LGPRODUCT, LGBRAND
WHERE LGPRODUCT.BRAND_ID = LGBRAND.BRAND_ID
group by LGBRAND.BRAND_ID, LGBRAND.BRAND_NAME -- This line here
ORDER BY BRAND_NAME;
答案 3 :(得分:0)
对于新添加的字段,您没有使用group by,因此您收到此错误,因此通过将新添加的字段包括在内,您将解决此错误并将所有必须的数据分组汇总。