Home Forums SQL Server 7,2000 T-SQL Getting the last entry based on a previous entry. RE: Getting the last entry based on a previous entry.

  • too mach scans

    select

    *

    from

    reporthistory rh

    where

    not exists (select * from reporthistory rh1 where rh1.Report_id = rh.Report_id and rh1.ReportHistoryID > rh.ReportHistoryID)

    and (select top 1 rh1.Report_status_id from reporthistory rh1 where rh1.Report_id = rh.Report_id and rh1.ReportHistoryID < rh.ReportHistoryID Order by rh1.ReportHistoryID desc) = 2

    and rh.Report_status_id = 1

    I Have Nine Lives You Have One Only
    THINK!