Sqlserver
 sql >> Datenbank >  >> RDS >> Sqlserver

Beitritt zum MAX-Datensatz in der Gruppe

In SQL Server 2005+ :

SELECT  *
FROM    job j
OUTER APPLY
        (
        SELECT  TOP 1 *
        FROM    jobstatus js
        WHERE   js.jobid = j.jobid
        ORDER BY
                js.date DESC
        ) js

In SQL Server 2000 :

SELECT  *
FROM    job j
LEFT JOIN
        jobstatus js
ON      js.id =
        (
        SELECT  TOP 1 id
        FROM    jobstatus jsi
        WHERE   jsi.jobid = j.jobid
        ORDER BY
                jsi.date DESC
        )

Diese Abfragen behandeln mögliche Duplikate am Date richtig.