仅显示子查询中的 1 列:列出平均价格 >= 3 且至少有 2 个不同产品的麸皮名称

Posted

技术标签:

【中文标题】仅显示子查询中的 1 列:列出平均价格 >= 3 且至少有 2 个不同产品的麸皮名称【英文标题】:display only 1 column from the subquery : list the bran_name whos avg price is >= 3 and also have at least 2 distinct products 【发布时间】:2021-03-21 07:06:18 【问题描述】:

这是我的查询:

select avg(p2.price), p2.brand_name, count(distinct p2.product_id) 
count_of_products
from product p2
group by p2.brand_name
having count(distinct p2.product_id) >= 2 and avg(p2.price) > 3

但我只需要在最终查询中显示band_name。 我试过这个:

select p1.brand_name from product p1,
(select avg(p2.price), p2.brand_name, count(distinct p2.product_id) 
count_of_products
from product p2
group by p2.brand_name
having count(distinct p2.product_id) >= 2 ) p2 and and avg(p2.price) > 3
where 
p1.brand_name = p2.brand_name

但它给了我以下错误:

>[Error] Script lines: 3-9 --------------------------
 No column name was specified for column 1 of 'p2'.

【问题讨论】:

【参考方案1】:

但我只需要在最终查询中显示 band_name

所以只需从select 子句中删除其他列:

select brand_name
from product
group by brand_name
having count(distinct product_id) >= 2 and avg(price) > 3

【讨论】:

更新了我的问题:我只需要那些平均(价格)大于 3 @GMB 的品牌 @gabs:您可以将该条件添加到having 子句中。

以上是关于仅显示子查询中的 1 列:列出平均价格 >= 3 且至少有 2 个不同产品的麸皮名称的主要内容,如果未能解决你的问题,请参考以下文章