Return Top 1 Field in a JOIN statement

This is a nice solution in that it groups the field used in the join statement. You can use this technique in many situations. I used this code to join the sysjobhistory table by job_id where only the top 1 job was returned:

SELECT * FROM msdb.dbo.sysjobs j
INNER JOIN (SELECT job_id
FROM msdb.dbo.sysjobhistory
GROUP BY job_id) AS h ON j.job_id = h.job_id
WHERE name = 'syspolicy_purge_history'

Leave a Reply