I have the following SQL query which I use to get some stats (AVG, MAX, MIN and COUNT) of each group in my database. The tables are two. Teams and Users. Each team has many users.
SELECT t.id, AVG(u.Age) AS Average, MIN(u.Age) AS Minimum, MAX(u.Age) AS Maximum, COUNT(u.id) AS NumberOfUsers
FROM Teams t
INNER JOIN Users u ON t.id = u.id
WHERE t.status IS NOT NULL
GROUP BY t.id
ORDER BY Average Desc
However, the table Users has another column "Experience" and I want to get the same stats for the top 10 users in each group based on that column.
Any idea?
The solution is to use row_number()
, in this case, I think in a subquery:
SELECT t.id, AVG(u.Age) AS Average, MIN(u.Age) AS Minimum,
MAX(u.Age) AS Maximum, COUNT(u.id) AS NumberOfUsers
FROM Teams t INNER JOIN
(SELECT u.*,
ROW_NUMBER() OVER (PARTITION BY id ORDER BY experience DESC) as seqnum
FROM Users u
) u
ON t.id = u.id AND -- is this really the right join condition?
seqnum <= 10
WHERE t.status IS NOT NULL
GROUP BY t.id
ORDER BY Average Desc;
I suspect that your join conditions are wrong. I would expect the proper join condition to use something like u.TeamId
instead of u.Id
. If so, you need to change the partition by
column to be the same as the join
column.
Gordon Linoff的解决方案看起来不错,如果经验对许多用户来说都是相同的,您还可以考虑在ROW_NUMBER()上考虑DENSE_RANK()或RANK()以获得“更好”的统计数据。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.