Question : One record per latest date

I have table tblSales in this table I have next fields and data:

Product            Category       Salesmen      SoldDate
AAAA            1111            John B.            02/14/2010
AAAA            1111            Bill J.            03/15/2010
BBBB            2222            Janet L.                           04/15/2010
BBBB            3333            Ken M.            05/01/2010
BBBB            4444            Peter  S.                           05/01/2010
CCCC            4444            John B.            02/16/2010
CCCC            5555            John B.            02/16/2010
CCCC            5555            Ken M.            01/20/2010

I need to pull out just one record per product with latest sold date.


Result should be :

Product       Category      Salesmen      SoldDate
AAAA            1111            Bill J.            03/15/2010
BBBB            3333            Ken M.            05/01/2010
CCCC            4444            John B.            02/16/2010

To point out : For me it is not important if record for product BBBB is 3333 Ken M.  05/01/2010 Or BBBB  4444  Jonh B. 05/01/2010 as Both have the same latest Date 05/01/2010
The same is with  record for product  CCCC  4444 John B. 02/16/2010  it could be CCCC 5555 John B. 02/16/2010 as Both have the same sold date 02/16/2010
I just need select statement that will pull this result as above   from  my table tlbSales  in SQL Server 2008.
One record per Product that has latest sold date:

Answer : One record per latest date

Then just build a temporary table that holds ALL the names in a single field, along with an address:

Currentdb.Execute "Select Address, csz INTO YourTempTable FROM Select Distinct Address, csz FROM YourExistingTable"

Now add a SendTo field:

Currentdb.Execute "ALTER TABLE YourTempTable ADD COLUMN SendTo TEXT(255)"

Now build a recordset where you can loop through all names for a specific Address + csz, and concatenate the names:

Dim rst As DAO.Recordset
Dim rstNames as DAO.Recordset
Dim sNames as String

set rst = Currentdb.OpenRecordset("SELECT * FROM YourTempTable")

Do Until rst.EOF
  Set rstNames = Currentdb.Openrecordset("SELECT * FROM YourExistingTable WHERE [Address]='" & rst("Address") & "' AND csz='" & rst("csz") & "')"
  sNames = ""
  Do until rstNames.EOF
    sNames = sNames & vbCrLf & rstNames("First_Name") & " " & rstNames("Last_Name")
    rstNames.movenext
  Loop
  rst.Edit
  rst("SendTo") = sNames
  rst.Update
Loop
Random Solutions  
 
programming4us programming4us