表上的 SQL AVG

标签 sql postgresql

我有下表

enter image description here

我正在尝试获取每个 prod_type_desc 的平均价格,我有以下代码让我来到这里,但无法弄清楚其余部分。

select * from (select price, prod_type_desc from products NATURAL JOIN producttypes NATURAL JOIN refproducttypes) as a

我试过从上面选择 AVG(price), prod_type_desc 但那不起作用。

最佳答案

您需要在 product_type_desc 上使用 GROUP BY

select prod_type_desc, avg(price) from products group by prod_type_desc

关于表上的 SQL AVG,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21272222/

相关文章:

c# - 使用 Dapper 将项目列表(可能为空)作为 IN 子句的参数传递

mysql - 在现有列的值中添加字符

sql - 检查整个表中的单个值

postgresql - 无法从主机连接到在 Vagrant 中运行的 PostgreSQL 服务器

javascript - Web SQL 事务在 javascript 函数中无法正确执行

mysql - 将多个查询合并为一个查询时,如何将列求和

sql - 在一列中选择可选数量的值

python - 使用请求库的脚本在尝试从 Postgres 表提供 url 时抛出 "no connection adapters found"错误

sql - 仅当连接明确时才更新行

php - 从 MySQL 表的一列获取字符串值并更新同一行的其他列值