获取所有分组中最大计数分组结果的记录
Get records for the maximum count grouped results among all group
我有三本table的书、会员和外借。我想找到成员拥有最大书籍数量的书籍的名字、姓氏和书名。我尝试了以下
- 为了获得拥有最大记录的成员,我根据 member_id 对记录进行分组,并降序以获得最大数量。分组记录仅包含一个 member_id(3),但我希望所有 member_id(2 和 3)都获得“书名”,所以我使用 GROUP_CONCAT 来获得它。
SELECT m.firstname, m.lastname, l.member_id, GROUP_CONCAT(l.book_id) as book_id, count(*) as max_c from member m
join loan l
ON m.member_id = l.member_id
group by l.member_id
order by max_c desc limit 1
- 然后我尝试使用以下查询获取所需的记录
SELECT m.firstname, m.lastname, b.title from book b
join loan l ON b.book_id = l.book_id
where l.book_id IN
(SELECT m.firstname, m.lastname, l.member_id, GROUP_CONCAT(l.book_id) as book_id, count(*) as max_c from member m
join loan l
ON m.member_id = l.member_id
group by l.member_id
order by max_c desc limit 1)
期望的输出是
预期输出如下所示
firstname lastname title
Mark Sacks The Cow
Mark Sacks XML for beginners
我在 sqlfiddle 的 SQLite(SQL.js) 中尝试这个,请帮助我实现这个。
本书table
book_id isbn title author publish_year
1 444222666325 Mars Mark Sas 2001
2 784566512135 The Cow Williams 1996
3 488984115444 XML for beginners Jake Snow 2005
4 544465545655 Into Thin Air Jon Kraka 1990
5 878745656513 And Tango Makes PeterParnel 1956
6 564564123213 Swimmy Leo Lionni 2010
7 132125645678 XML and XQuery Lee Cakes 2014
8 132154548746 Happy Places Steve Zus 1998
9 788897998754 The Mascot Kevin Bacon 1987
10 878561132116 XQuery for begin Virginia 1800
成员 table
member_id lastname firstname
1001 Smith John
2123 Sacks Mark
3456 Johnson Susan
4223 States Nick
5987 Stew Martha
贷款 table
member_id book_id loan_date due_date
5987 4 2017-09-13 2017-09-30
2123 3 2017-09-13 2017-09-15
4223 9 2017-09-13 2019-09-13
1001 5 2017-10-15 2017-10-19
2123 2 2017-10-15 2017-11-15
您想在此处使用 COUNT
作为分析函数,以保留所有行,然后应用 RANK
查找排名最高的作者以及所有记录:
WITH cte AS (
SELECT m.firstname, m.lastname, b.title, COUNT(*) OVER (PARTITION BY m.member_id) cnt
FROM member m
INNER JOIN loan l ON l.member_id = m.member_id
INNER JOIN book b ON b.book_id = l.book_id
),
cte2 AS (
SELECT *, RANK() OVER (ORDER BY cnt DESC) rnk
FROM cte
)
SELECT firstname, lastname, title
FROM cte2
WHERE rnk = 1;
我有三本table的书、会员和外借。我想找到成员拥有最大书籍数量的书籍的名字、姓氏和书名。我尝试了以下
- 为了获得拥有最大记录的成员,我根据 member_id 对记录进行分组,并降序以获得最大数量。分组记录仅包含一个 member_id(3),但我希望所有 member_id(2 和 3)都获得“书名”,所以我使用 GROUP_CONCAT 来获得它。
SELECT m.firstname, m.lastname, l.member_id, GROUP_CONCAT(l.book_id) as book_id, count(*) as max_c from member m
join loan l
ON m.member_id = l.member_id
group by l.member_id
order by max_c desc limit 1
- 然后我尝试使用以下查询获取所需的记录
SELECT m.firstname, m.lastname, b.title from book b
join loan l ON b.book_id = l.book_id
where l.book_id IN
(SELECT m.firstname, m.lastname, l.member_id, GROUP_CONCAT(l.book_id) as book_id, count(*) as max_c from member m
join loan l
ON m.member_id = l.member_id
group by l.member_id
order by max_c desc limit 1)
期望的输出是 预期输出如下所示
firstname lastname title
Mark Sacks The Cow
Mark Sacks XML for beginners
我在 sqlfiddle 的 SQLite(SQL.js) 中尝试这个,请帮助我实现这个。
本书table
book_id isbn title author publish_year
1 444222666325 Mars Mark Sas 2001
2 784566512135 The Cow Williams 1996
3 488984115444 XML for beginners Jake Snow 2005
4 544465545655 Into Thin Air Jon Kraka 1990
5 878745656513 And Tango Makes PeterParnel 1956
6 564564123213 Swimmy Leo Lionni 2010
7 132125645678 XML and XQuery Lee Cakes 2014
8 132154548746 Happy Places Steve Zus 1998
9 788897998754 The Mascot Kevin Bacon 1987
10 878561132116 XQuery for begin Virginia 1800
成员 table
member_id lastname firstname
1001 Smith John
2123 Sacks Mark
3456 Johnson Susan
4223 States Nick
5987 Stew Martha
贷款 table
member_id book_id loan_date due_date
5987 4 2017-09-13 2017-09-30
2123 3 2017-09-13 2017-09-15
4223 9 2017-09-13 2019-09-13
1001 5 2017-10-15 2017-10-19
2123 2 2017-10-15 2017-11-15
您想在此处使用 COUNT
作为分析函数,以保留所有行,然后应用 RANK
查找排名最高的作者以及所有记录:
WITH cte AS (
SELECT m.firstname, m.lastname, b.title, COUNT(*) OVER (PARTITION BY m.member_id) cnt
FROM member m
INNER JOIN loan l ON l.member_id = m.member_id
INNER JOIN book b ON b.book_id = l.book_id
),
cte2 AS (
SELECT *, RANK() OVER (ORDER BY cnt DESC) rnk
FROM cte
)
SELECT firstname, lastname, title
FROM cte2
WHERE rnk = 1;