我需要撰写一个查询,以显示平均产品价格最高的品牌的品牌ID,品牌名称,品牌类型和平均价格
我可以通过输入以下内容获得所有品牌的平均价格:
SQL> SELECT LGPRODUCT.BRAND_ID, BRAND_NAME, BRAND_TYPE, ROUND(AVG(PROD_PRICE),2)
FROM BOFF.LGPRODUCT, BOFF.LGBRAND
WHERE LGPRODUCT.BRAND_ID = LGBRAND.BRAND_ID
GROUP BY LGPRODUCT.BRAND_ID, BRAND_NAME, BRAND_TYPE
ORDER BY LGPRODUCT.BRAND_ID;
但如果我输入ROUND(MAX(AVG(PROD_PRICE)),2)我收到错误,有什么建议吗?
答案 0 :(得分:0)
您可以使用窗口函数In [200]: df
Out[200]:
color cost temp newcol
0 blue 12.0 80.4 mango
1 red 8.1 81.2 banana
2 pink 24.5 83.5 apple
In [201]: df_out
Out[201]:
color cost temp newcol original_idx
0 blue 11.5 80.4 mango 0
1 blue 12 80.4 mango 0
2 blue 12.5 80.4 mango 0
3 red 7.6 81.2 banana 1
4 red 8.1 81.2 banana 1
5 red 8.6 81.2 banana 1
6 pink 24 83.5 apple 2
7 pink 24.5 83.5 apple 2
8 pink 25 83.5 apple 2
根据平均值标记排名,然后过滤以获得排名靠前的行:
rank
如果您必须使用select *
from (
select lgproduct.brand_id,
brand_name,
brand_type,
round(avg(prod_price), 2),
rank() over (
order by round(avg(prod_price), 2) desc
) as rnk
from boff.lgproduct,
boff.lgbrand
where lgproduct.brand_id = lgbrand.brand_id
group by lgproduct.brand_id,
brand_name,
brand_type
) t
where rnk = 1
order by brand_id;
,请尝试:
having