Click here to Skip to main content
15,890,043 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Here is my data:

BookISBN	LastCheckOutDate	PatronNumber
100004	11/14/2011	950101
100005	12/18/2013	950102
100005	1/18/2013	950103
100005	4/10/2013	950104
100005	10/22/2013	950105
100005	9/17/2014	950106
100005	1/11/2016	950107
100005	12/5/2011	950108


I need the BookISBN to display only once for the most recent last checkout date.

All these fields are in one table.

Even when I query the MAX(LastCheckOutDate), I still get the multiple entries for the BookISBN.

How can I get just one entry per BookISBN>

My query is:
<pre> SELECT  INV.BookISBN,MAX(LastCheckoutDate) AS LastDate,PatronNumber, BookDesc, BookPrice, QtyCheckedOut

 FROM LibraryInventory INV LEFT JOIN Books BKS ON INV.BookISBN = B.BookISBN
 
WHERE B.BookISBN IS NULL
 AND INV.BookISBN IS NOT NULL
 
AND LastCheckoutDate < DATEADD("d", -365, CONVERT(VARCHAR(10),Getdate(),101))
 
GROUP BY INV.BookISBN,PatronNumber, BookDesc, BookPrice, QtyCheckedOut


ORDER BY INV.BookISBN


What I have tried:

SQL MSDN, Google search, basic sql query MAX()
Posted
Updated 2-Mar-17 18:09pm
Comments
Peter Leow 1-Mar-17 10:01am    
That LastCheckOutDate is a varchar type?
Member 10379103 1-Mar-17 10:23am    
No - it is a datetime type.

Here is the keyword that you require: SQL FIRST() Function[^]
 
Share this answer
 
Comments
Member 10379103 1-Mar-17 10:25am    
This is only recognized in Access - got an error that FIRST is not recognized function in SQL server.
Graeme_Grant 1-Mar-17 11:01am    
If you continued reading the page, they show you alternative for other database systems. Here is an extract from that page:

SQL Server Syntax -
SELECT TOP 1 column_name FROM table_name
ORDER BY column_name ASC;
Can you use table common expression CTE?

SQL
;WITH temp AS (
SELECT ROW_NUMBER() OVER (PARTITION BY BookISBN
                          ORDER BY LastCheckOutDate DESC
                         ) AS myrownum
       ,*    
from book )
SELECT * FROM temp WHERE myrownum = 1


Output:
myrownum	BookISBN	LastCheckOutDate	PatronNumber
1	        100004	        2011-11-14	        950101
1	        100005	        2016-01-11	        950107


Now you can update the query to join LibraryInventory table, assuming all record in LibraryInventory are unique

Example:
SQL
;WITH temp AS (
SELECT ROW_NUMBER() OVER (PARTITION BY BookISBN
                          ORDER BY LastCheckOutDate DESC
                         ) AS myrownum
       ,*    
from book )
SELECT * FROM temp B JOIN LibraryInventory INV 
ON B.BookISBN = INV.BookISBN
WHERE myrownum = 1
 
Share this answer
 
select bookisbn, to_date(substr(tempcol, 1, 8), 'yyyymmdd') lastcheckoutdate, substr(tempcol, 9) patronnumber
from
(
select bookisbn, max(to_char(lastcheckoutdate, 'yyyymmdd') || patronnumber) tempcol
from libraryinventory
group by bookisbn
)
order by bookisbn

-- this will work in Oracle
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900