最多发行的书籍在一天内

时间:2010-01-05 17:00:38

标签: sql sql-server-2005

我有两张桌子

  1. Book(BookID,BookName)
  2. 问题(BookID,IssueDate,Qty)
  3. 那么,我怎样才能找到指定日期的最大“已发行图书名称”。

2 个答案:

答案 0 :(得分:3)

select top 1 book.bookid, book.bookname from
(
  select bookid, sum(qty) as s
  from issue
  where issuedate = @issuedate
  group by bookid
) grp
inner join book 
  on book.bookid = grp.bookid
order by s

如果“最大”,则表示“在给定日期发出的数量最大的图书的名称”。

答案 1 :(得分:0)

这样的事情:

select BookName from Book 
where BookId = (select BookId from Issue where IssueDate = @yourdate 
AND Qty = (select MAX(Qty) from  Issue))