在SELECT子句中的Expression#4未出现在GROUP BY子句中。

huangapple go评论56阅读模式
英文:

It says Expression #4 in SELECT clause is not is present in GROUP by clause

问题

选择 p.product_class_code, pc.product_class_desc, COUNT(DISTINCT p.product_id) as 产品类型数,
(p.product_price * p.product_quantity_avail) as 库存价值
从产品 p
加入产品类别 pc
按照 p.product_class_code, pc.product_class_desc
分组

按照 p.product_class_code, pc.product_class_desc 降序排序;

在这里,我需要每个 product_class_code 有多少种产品类型,这就是为什么使用 count(distinct) 函数,还有一个计算列 Inventory_value。我不知道这里有什么问题。它说选择子句中的表达式 #4 在 GROUP 子句中不存在。


<details>
<summary>英文:</summary>

select p.product_class_code,pc.product_class_desc,count(distinct p.product_id) as Product_types,
(p.product_price * p.product_quantity_avail) as Inventory_value from product p join product_class pc
group by p.product_class_code,pc.product_class_desc

order by p.product_class_code,pc.product_class_desc desc;

here I need how many types of products are there for each product_class_code, thats why the count(distinct), a Inventory_value column which is a calculated coulmn. I dont know what is wrong here. It says  Expression #4 in SELECT clause is not
is present in GROUP by clause

</details>


# 答案1
**得分**: -1

你遇到的错误可能是因为在`HAVING`子句中使用了一个在`GROUP BY`子句中不存在或不是聚合函数的表达式。要解决此问题,可以使用子查询首先计算库存值和产品类型,然后使用`HAVING`子句进行筛选。

这是修改后的查询:

```sql
SELECT subquery.product_class_code,
       subquery.product_class_desc,
       subquery.Product_types,
       subquery.Inventory_value
FROM (
    SELECT p.product_class_code,
           pc.product_class_desc,
           COUNT(DISTINCT p.product_id) AS Product_types,
           SUM(p.product_price * p.product_quantity_avail) AS Inventory_value
    FROM product p
    JOIN product_class pc ON p.product_class_code = pc.product_class_code
    GROUP BY p.product_class_code, pc.product_class_desc
) AS subquery
WHERE subquery.Inventory_value > 100000
ORDER BY subquery.Inventory_value DESC;

此修改后的查询包括一个子查询,用于执行初始聚合,外部查询根据计算的库存值对结果进行筛选。

一些额外的注意事项:

我注意到你的原始查询具有一个JOIN子句,但没有ON子句来指定连接条件。正确指定连接条件以正确组合来自连接表的行非常重要。在修改后的查询中,我添加了一个ON子句(ON p.product_class_code = pc.product_class_code),假设product_class_code是连接两个表的常见列。

在子查询中,我使用了SUM(p.product_price * p.product_quantity_avail)作为聚合函数,用于计算每个产品类别的总库存值。这似乎是您在原始查询的SELECT子句中尝试实现的内容(p.product_price * p.product_quantity_avail)SUM函数将在每个产品类别中聚合所有产品的库存值。

英文:

The error you're encountering is likely due to the use of an expression in the HAVING clause that's not in the GROUP BY clause or is not an aggregate function. To resolve this issue, you can use a subquery to first calculate the inventory values and product types, and then filter them using the HAVING clause.

Here's the modified query:

SELECT subquery.product_class_code,
       subquery.product_class_desc,
       subquery.Product_types,
       subquery.Inventory_value
FROM (
    SELECT p.product_class_code,
           pc.product_class_desc,
           COUNT(DISTINCT p.product_id) AS Product_types,
           SUM(p.product_price * p.product_quantity_avail) AS Inventory_value
    FROM product p
    JOIN product_class pc ON p.product_class_code = pc.product_class_code
    GROUP BY p.product_class_code, pc.product_class_desc
) AS subquery
WHERE subquery.Inventory_value &gt; 100000
ORDER BY subquery.Inventory_value DESC;

This modified query includes a subquery to perform the initial aggregation, and the outer query filters the results based on the calculated inventory value.

Some additional points:

I noticed that your original query has a JOIN clause but without an ON clause to specify the join condition. It is important to specify the join condition to correctly combine rows from the joined tables. In the modified query, I have added an ON clause (ON p.product_class_code = pc.product_class_code), assuming that product_class_code is the common column that connects the two tables.

In the subquery, I used SUM(p.product_price * p.product_quantity_avail) as the aggregate function to calculate the total inventory value for each product class. It seems like this is what you are trying to achieve with the original expression (p.product_price * p.product_quantity_avail) in the SELECT clause. The SUM function aggregates the inventory values across all products within each product class.

huangapple
  • 本文由 发表于 2023年6月26日 21:59:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76557392.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定