views:

44

answers:

2

I have two table

  1. Book (BookID, BookName)
  2. Issue (BookID, IssueDate, Qty)

So, how can i find the maximum "issued book name" for a specified date.

+3  A: 
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

if by "maximum" you mean "the name of the book that has the largest quantity issued on a given date".

davek
+1, for being able to read his mind.
Aaronaught
:) ask Mr. link how he got my point ?
dan
A: 

Something like this :

select BookName from Book 
where BookId = (select BookId from Issue where IssueDate = @yourdate 
AND Qty = (select MAX(Qty) from  Issue))
dada686
its bringing null records
dan
tnx my frnd it working
dan
@dada686, your second (nested) sub-select is not restricted to the desired `@yourdate`. So, if the highest `Qty` of any title ever issued in a day was 500 copies of "The C Programming Language" on 2010-01-01, a record never met before or since, your query as written will return the empty set for any day other than New Year's Day, 2010.
pilcrow