0

我正在尝试编写一个查询以获取一份报告,该报告显示哪本书被借出最多。我有一个查询要借出最多的实际书,但我需要能够显示该书的标题,该书名位于另一个表中。

SELECT * FROM LOAN;

LOANID     BOOKID     CUSTID OUTDATE   INDATE    DUEDATE

SELECT * FROM BOOK;

AUTHORID     BOOKID   BKISBN   BKSTATUS   BKTITLE  BKSERIES  BKTYPE   BKNUMBER

以下查询是我目前所拥有的并且正在获取 bookid。:

SELECT Q1.* FROM
(SELECT BOOKID, COUNT(*)
    AS BOOK_COUNT
  FROM LOAN
  GROUP BY LOAN.BOOKID) Q1,
(SELECT MAX(Q2.BOOK_COUNT)
    AS HIGH_COUNT
  FROM (SELECT BOOKID, COUNT(*)
    AS BOOK_COUNT
  FROM LOAN
  GROUP BY LOAN.BOOKID) Q2) Q3
WHERE Q1.BOOK_COUNT = Q3.HIGH_COUNT;

结果:

    BOOKID BOOK_COUNT
---------- ----------
       387          3

现在我需要 Book Table 中的 BkTitle。

有人可以帮忙吗?

4

1 回答 1

0

尝试

select b.bookid, b.BKTITLE , count(l.loanid) as cnt_loan
from book b
inner join loan l on l.bookid = b.bookid
WHERE ROWNUM < 2
group by b.bookid
order by cnt_loan desc
于 2012-04-13T20:31:13.463 回答