# Miscellaneous > SQL Scripts >  Help with sql statement

## ptrealtor

Please looking for help writing a sql statement.  I have a table with the following columns

Seller Name
Buyer Name
Buyer Address
Buyer City
Buyer State
Buyer Zip
Sale Date
Unit Number
Complex Name

The table contains all of the past and present owners of condos in different complexes.  How can I write a statement which pulls out all of the present owners (Buyer Name, Buyer Address, etc) for a particular complex.  I appreciate anyones help.

----------


## stecal

What does Sale Date mean? Date when bought or date when sold?

----------


## ptrealtor

Sale Date is the date the property closed

----------


## stecal

If no sales date, then the property has not sold. If not sold, then the current record where sales date is null should be what you are looking for.

----------


## ptrealtor

There will always be a sales date.  The field will never be null.

----------


## bannor

Given the fields you have listed, how would you decide who is a present owner and who isn't if you were looking at the table data?

----------


## rmiao

Pull row with most recent sale date for a particular complex.

----------


## schanda

First get the max sale date by complex and unit
Then get all the details you want.

select Buyer_Name, Buyer_Address
  from table_name t
        ,(select complex_name, unit_number, max(sale_date)
            from table_name
          group by complex_name, unit_number) b
 where a.complex_name = b.complex_name
    and a.unit_number = b.unit_number
    and a.sale_date = b.sale_date

----------

