我如何将我的子查询的结果与另一个 table 合并?
how do i take the results of my subqueries to join with another table?
。尝试 link 将借款人、书籍的结果汇总到 AUTHOR。
期望的结果:
AUTHORID AUTHORFIRSTNAME AUTHORLASTNAME
1 JIM SPARKS
2 JAMES ALLEN
3 MARCUS RASHFORD
20 PAUL POGBA
22 THIERRY HENRY
但我不确定如何 link 返回的顶级 authorids 来检索作者名字和姓氏,但我没有在子查询中提到作者 table
所以,如果我没看错的话,你想做这样的事情:
select authorid, authorfirstname, authorlastname
from
(select a.authorid, a.authorfirstname, a.authorlastname
from author a, borrower b, book c
where a.authorid = c.authorid and c.bookid = b.bookid
and b.borrowdate like '%2017'
group by c.bookauthor
order by count(*) desc) xx
where rownum <=5
您可以在子查询中按 count
的降序将这三个表与 rank()
分析函数连接起来,然后在主查询中取小于等于 5 的值:
SELECT authorid, authorfirstname, authorlastname
FROM
(
SELECT a.authorid, a.authorfirstname, a.authorlastname,
rank() over (order by count(*) desc) as rnk
FROM AUTHOR a
LEFT JOIN BOOK bk ON a.authorid = bk.authorid
LEFT JOIN BORROWER br ON br.bookid = bk.bookid
WHERE br.borrowdate between date'2017-01-01' and date'2017-12-31'
GROUP BY a.authorid, a.authorfirstname, a.authorlastname
)
WHERE rnk <= 5
ORDER BY rnk
如果您使用的是 DB 版本 12c+,获取 它们会更容易:
SELECT a.authorid, a.authorfirstname, a.authorlastname,
rank() over (order by count(*) desc) as rnk
FROM AUTHOR a
LEFT JOIN BOOK bk ON a.authorid = bk.authorid
LEFT JOIN BORROWER br ON br.bookid = bk.bookid
WHERE br.borrowdate between date'2017-01-01' and date'2017-12-31'
GROUP BY a.authorid, a.authorfirstname, a.authorlastname
ORDER BY rnk
FETCH FIRST 5 ROWS WITH TIES
我使用 br.borrowdate between date'2017-01-01' and date'2017-12-31'
而不是 to_char(br.borrowdate) like '%2017'
的地方可以使 borrowdate
列上的索引受益。
return 有关系的行上方的那些查询,例如如果多行与第 5 行的值匹配,它们会带来多于 5 行。
不要使用rownum
伪列进行排名,因为它的值是在排序前计算的,可能会产生错误的结果。
。尝试 link 将借款人、书籍的结果汇总到 AUTHOR。
期望的结果:
AUTHORID AUTHORFIRSTNAME AUTHORLASTNAME
1 JIM SPARKS
2 JAMES ALLEN
3 MARCUS RASHFORD
20 PAUL POGBA
22 THIERRY HENRY
但我不确定如何 link 返回的顶级 authorids 来检索作者名字和姓氏,但我没有在子查询中提到作者 table
所以,如果我没看错的话,你想做这样的事情:
select authorid, authorfirstname, authorlastname
from
(select a.authorid, a.authorfirstname, a.authorlastname
from author a, borrower b, book c
where a.authorid = c.authorid and c.bookid = b.bookid
and b.borrowdate like '%2017'
group by c.bookauthor
order by count(*) desc) xx
where rownum <=5
您可以在子查询中按 count
的降序将这三个表与 rank()
分析函数连接起来,然后在主查询中取小于等于 5 的值:
SELECT authorid, authorfirstname, authorlastname
FROM
(
SELECT a.authorid, a.authorfirstname, a.authorlastname,
rank() over (order by count(*) desc) as rnk
FROM AUTHOR a
LEFT JOIN BOOK bk ON a.authorid = bk.authorid
LEFT JOIN BORROWER br ON br.bookid = bk.bookid
WHERE br.borrowdate between date'2017-01-01' and date'2017-12-31'
GROUP BY a.authorid, a.authorfirstname, a.authorlastname
)
WHERE rnk <= 5
ORDER BY rnk
如果您使用的是 DB 版本 12c+,获取 它们会更容易:
SELECT a.authorid, a.authorfirstname, a.authorlastname,
rank() over (order by count(*) desc) as rnk
FROM AUTHOR a
LEFT JOIN BOOK bk ON a.authorid = bk.authorid
LEFT JOIN BORROWER br ON br.bookid = bk.bookid
WHERE br.borrowdate between date'2017-01-01' and date'2017-12-31'
GROUP BY a.authorid, a.authorfirstname, a.authorlastname
ORDER BY rnk
FETCH FIRST 5 ROWS WITH TIES
我使用 br.borrowdate between date'2017-01-01' and date'2017-12-31'
而不是 to_char(br.borrowdate) like '%2017'
的地方可以使 borrowdate
列上的索引受益。
return 有关系的行上方的那些查询,例如如果多行与第 5 行的值匹配,它们会带来多于 5 行。
不要使用rownum
伪列进行排名,因为它的值是在排序前计算的,可能会产生错误的结果。