I have a Product table with the columns
AcctNum
ProdCode
InvoiceDate
I can have multiple rows for a given AcctNum:
123 A 01/01/2005
123 B 01/02/2005
123 C 01/03/2005
234 C 02/01/2004
345 A 01/01/2005
345 B 01/02/2005
I need the max(InvoiceDate) and if the max for a given AcctNum is a ProdCode
= B. So if the latest InvoiceDate is for a given AcctNum is B then return
that row.
123 B 01/02/2005
Would not be returned because the max Invoice date for AcctNum 123 is
ProdCode C.
345 B 01/02/2005
Would be returned because the max Invoice date for AcctNum 345 is ProdCode
B.
I can solve this using a cursor fairly easily by using a distinct AcctNum in
the cursor select and getting the max InvoiceDate for each AcctNum. This is
a costly and I'm looking for a solution using temp tables or a query to
handle this problem.
I hope I have made this clear enough (sorry if I was too verbose). Thanks in
advance for your help.
-p