sql - 选择平均分最高的前10名

标签 sql sql-server select join

假设我有产品和分数表。

Product
-------
id
name

Score
-----
id
ProductId
ScoreValue

我想获取 AVERAGE 分数最高的前 10 个产品,如何在一个 select 语句中获取平均值并选择前 10 个产品?

这是我的,它选择了意外的行

SELECT TOP 10 Product.ProductName Score.Score 
FROM Product, Score 
WHERE Product.ID  IN (select top 100  productid  
                       from score 
                       group by productid 
                       order by sum(score) desc) 
order by Score.Score desc

最佳答案

这可能会做到

SELECT TOP 10 p.ProductName, avg( s.Score ) as avg_score
FROM Product p
    inner join Score s on s.product_id = p.product_id
group by p.ProductName, p.product_id
order by avg_score desc

关于sql - 选择平均分最高的前10名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16214205/

相关文章:

mysql - SQL - 无法弄清楚如何连接三个表

SQLite/Postgres SQL 查询不同列

sql-server - 无法在 Linux 上的 SQL Server 2017 Express 上启用 xp_cmdshell

javascript - jQuery ComboBox : No matter what I try, 我无法将值返回到我的选择框(无法以编程方式选择所需的选项)

mysql - 为什么带有 "HAVING"的 "REPLACE"语句会影响值?

php - 如何在 PHP 中从 MySQL 查询中获取单个值

c# - 如何使用 bot 应用程序从 C# 聊天机器人在 SQL Server 中执行存储过程?

sql-server - LAG 不是可识别的内置函数名称

mysql - 帖子和标签 - 加入限制

jquery - Rails,根据初始选择,使用部分静态数据更新第二个选择