Home Forums SQL Server 2008 T-SQL (SS2K8) how to write join part of query so that only most recent equipment assignments are returned RE: how to write join part of query so that only most recent equipment assignments are returned

  • Hi,

    Change the 2nd like this you will get the result

    select

    distinct

    i.iphone_id,

    i.seriel_number,

    lh.Last_Date_Assigned,

    loc.emp_name,

    loc.Department

    from iphones i

    inner join (

    select iphone_id, MAX(date_assigned) as Last_Date_Assigned

    from location_history group by iphone_id

    )lh

    join location_history loch on loch.date_assigned = lh.Last_Date_Assigned and loch.iphone_id=lh.iphone_id

    join locations loc on loc.location_id = loch.location_id

    on i.iphone_id = lh.iphone_id