如何 select COUNT(*) 的最大值 (SQL postgresql)
how to select the max of COUNT(*) (SQL postgresql)
我从 NOSQL 切换到 SQL 但我找不到如何 select count(*)
的最大值
我创建了用户 table、帖子和评论。
我想 select 发帖数和评论数最多的前 10 名用户
SELECT
fullname,
(SELECT COUNT(*)
FROM posts WHERE posts.author_id = users.id)
AS total_posts,
(SELECT COUNT(*)
FROM comments WHERE comments.author_id = users.id)
AS total_comments
FROM users
您可以将 ORDER BY
与 LIMIT
结合使用。例如:
select *
from (
-- your query here
) x
order by total_posts desc
limit 10
首先,使用SQL数据库需要在关联的table之间定义JOIN
语句来获取引用的关系数据。
由于您需要获得 TOP 10
个具有最多 post 的用户,因此当用户具有相同的 post 计数并且应该使用 LIMIT 10
的传统方法时会出现问题视为同等级。例如:4个人有5个post,3个人有6个post..
为了解决上面相同的 post 计数问题,我们将选择 RANK query,这会将具有相同 post 计数的人视为相同等级。
SELECT users.fullname, posting_rank_stats.total_posts, posting_rank_stats.posting_rank
FROM users
INNER JOIN
(
SELECT author_id, total_posts, RANK() OVER (ORDER BY post_author_count.total_posts DESC) AS posting_rank
FROM
(
SELECT COUNT(*) AS total_posts, author_id
FROM posts
GROUP BY author_id
) post_author_count
) posting_rank_stats
ON users.id = posting_rank_stats.author_id
-- we only want to search for top 10
WHERE posting_rank_stats.posting_rank <= 10;
破碎成小块:
1.Get post 每个用户计数
首先,我们得到每个用户的 post 计数,这需要 COUNT
函数,并且 GROUP BY author_id
SELECT COUNT(*) AS total_posts, author_id
FROM posts
GROUP BY author_id
2。计算 post count
的排名
使用RANK()
函数计算post的排名。排名位置由total_posts
的最高人数决定
SELECT author_id, total_posts, RANK() OVER (ORDER BY post_author_count.total_posts DESC) AS posting_rank
FROM
(
SELECT COUNT(*) AS total_posts, author_id
FROM posts
GROUP BY author_id
) post_author_count
3。总结
最后一步是将 users
table 与 posting_rank_stats
table 连接起来以获得结果。由于我们只想进入最多 post 的 TOP 10 用户,因此您需要添加条件
posting_rank_stats.posting_rank <= 10
在 WHERE
子句中
应用相同的概念也可以获得评论排名。
这是db fiddle I created, combine both posting rank and commenting rank
SELECT users.id , users.fullname, posting_rank_stats.posting_rank , posting_rank_stats.total_posts, commenting_rank_stats.comment_rank, commenting_rank_stats.total_comments
FROM users
LEFT JOIN
(
SELECT author_id, total_posts, RANK() OVER (ORDER BY post_author_count.total_posts DESC) AS posting_rank
FROM
(
SELECT COUNT(*) AS total_posts, author_id
FROM posts
GROUP BY author_id
) post_author_count
) posting_rank_stats ON users.id = posting_rank_stats.author_id
LEFT JOIN
(
SELECT author_id, total_comments, RANK() OVER (ORDER BY comment_author_count.total_comments DESC) AS comment_rank
FROM
(
SELECT COUNT(*) AS total_comments, author_id
FROM comments
GROUP BY author_id
) comment_author_count
) commenting_rank_stats ON users.id = commenting_rank_stats.author_id;
id
fullname
posting_rank
total_posts
comment_rank
total_comments
1
Jackson
1
3
5
1
2
Marry
4
1
1
5
3
Josh
4
1
2
3
4
Harley
1
3
4
2
5
Gordon
4
1
5
1
6
Barney
4
1
2
3
7
Gman
4
1
8
Stephan
3
2
9
Lucy
10
Jordan
11
Bill
12
Rosh
13
Lee
select * from (
select u.fullname,count(p.posts) as totalposts, count(c.comments) as totalcomments
from users u join posts p
on u.id=p.author_id
join comment c on u.id=c.comments
group by 1
ORDER BY count(p.posts),count(c.comments) desc ) k
limit 10;
我从 NOSQL 切换到 SQL 但我找不到如何 select count(*)
的最大值我创建了用户 table、帖子和评论。 我想 select 发帖数和评论数最多的前 10 名用户
SELECT
fullname,
(SELECT COUNT(*)
FROM posts WHERE posts.author_id = users.id)
AS total_posts,
(SELECT COUNT(*)
FROM comments WHERE comments.author_id = users.id)
AS total_comments
FROM users
您可以将 ORDER BY
与 LIMIT
结合使用。例如:
select *
from (
-- your query here
) x
order by total_posts desc
limit 10
首先,使用SQL数据库需要在关联的table之间定义JOIN
语句来获取引用的关系数据。
由于您需要获得 TOP 10
个具有最多 post 的用户,因此当用户具有相同的 post 计数并且应该使用 LIMIT 10
的传统方法时会出现问题视为同等级。例如:4个人有5个post,3个人有6个post..
为了解决上面相同的 post 计数问题,我们将选择 RANK query,这会将具有相同 post 计数的人视为相同等级。
SELECT users.fullname, posting_rank_stats.total_posts, posting_rank_stats.posting_rank
FROM users
INNER JOIN
(
SELECT author_id, total_posts, RANK() OVER (ORDER BY post_author_count.total_posts DESC) AS posting_rank
FROM
(
SELECT COUNT(*) AS total_posts, author_id
FROM posts
GROUP BY author_id
) post_author_count
) posting_rank_stats
ON users.id = posting_rank_stats.author_id
-- we only want to search for top 10
WHERE posting_rank_stats.posting_rank <= 10;
破碎成小块:
1.Get post 每个用户计数
首先,我们得到每个用户的 post 计数,这需要 COUNT
函数,并且 GROUP BY author_id
SELECT COUNT(*) AS total_posts, author_id
FROM posts
GROUP BY author_id
2。计算 post count
的排名使用RANK()
函数计算post的排名。排名位置由total_posts
SELECT author_id, total_posts, RANK() OVER (ORDER BY post_author_count.total_posts DESC) AS posting_rank
FROM
(
SELECT COUNT(*) AS total_posts, author_id
FROM posts
GROUP BY author_id
) post_author_count
3。总结
最后一步是将 users
table 与 posting_rank_stats
table 连接起来以获得结果。由于我们只想进入最多 post 的 TOP 10 用户,因此您需要添加条件
posting_rank_stats.posting_rank <= 10
在 WHERE
子句中
应用相同的概念也可以获得评论排名。
这是db fiddle I created, combine both posting rank and commenting rank
SELECT users.id , users.fullname, posting_rank_stats.posting_rank , posting_rank_stats.total_posts, commenting_rank_stats.comment_rank, commenting_rank_stats.total_comments
FROM users
LEFT JOIN
(
SELECT author_id, total_posts, RANK() OVER (ORDER BY post_author_count.total_posts DESC) AS posting_rank
FROM
(
SELECT COUNT(*) AS total_posts, author_id
FROM posts
GROUP BY author_id
) post_author_count
) posting_rank_stats ON users.id = posting_rank_stats.author_id
LEFT JOIN
(
SELECT author_id, total_comments, RANK() OVER (ORDER BY comment_author_count.total_comments DESC) AS comment_rank
FROM
(
SELECT COUNT(*) AS total_comments, author_id
FROM comments
GROUP BY author_id
) comment_author_count
) commenting_rank_stats ON users.id = commenting_rank_stats.author_id;
id | fullname | posting_rank | total_posts | comment_rank | total_comments |
---|---|---|---|---|---|
1 | Jackson | 1 | 3 | 5 | 1 |
2 | Marry | 4 | 1 | 1 | 5 |
3 | Josh | 4 | 1 | 2 | 3 |
4 | Harley | 1 | 3 | 4 | 2 |
5 | Gordon | 4 | 1 | 5 | 1 |
6 | Barney | 4 | 1 | 2 | 3 |
7 | Gman | 4 | 1 | ||
8 | Stephan | 3 | 2 | ||
9 | Lucy | ||||
10 | Jordan | ||||
11 | Bill | ||||
12 | Rosh | ||||
13 | Lee |
select * from (
select u.fullname,count(p.posts) as totalposts, count(c.comments) as totalcomments
from users u join posts p
on u.id=p.author_id
join comment c on u.id=c.comments
group by 1
ORDER BY count(p.posts),count(c.comments) desc ) k
limit 10;