I am needing to return the top 10 Schedules but one column, the HostName could return multiple values for that schedule. If I do a standard join with the Top 10, I get back in some cases, less than 10 schedules, but 10 overall records because a schedule can have more than one HostName related. I want the total records returned to not be limited, while ensuring that only 10 distinct ScheduleIds return.
Here is my flawed code, that works fine if the schedule only has a single HostName, but breaks if multiple.
SELECT TOP 10
s.Id AS ScheduleId
, s.GroupName
, a.Id AS AppId
, a.AppName
, (SELECT HostName FROM PatchingTargets
LEFT JOIN ScheduleTargets st on st.ScheduleId = s.Id
WHERE st.ScheduleId = s.Id) AS HostName
FROM ScheduleTickets t
LEFT JOIN Schedules s ON s.Id = t.ScheduleId
LEFT JOIN Applications a ON a.Id = s.AppId
WHERE PatchSessionId = 19 AND CollectionId IS NUll
ORDER BY a.PatchingPriority, s.Idx
Here is the typical join, that always returns 10 rows but not all of the HostNames.
SELECT TOP 10
s.Id AS ScheduleId
, s.GroupName
, a.Id AS AppId
, a.AppName
,p.HostName
FROM ScheduleTickets t
LEFT JOIN Schedules s ON s.Id = t.ScheduleId
LEFT JOIN ScheduleTargets st on st.ScheduleId = s.Id
LEFT JOIN dbo.PatchingTargets p on p.Id = st.PatchingTargetId
LEFT JOIN Applications a ON a.Id = s.AppId
WHERE PatchSessionId = 19 AND CollectionId IS NUll
ORDER BY a.PatchingPriority, s.Idx
Results without HostNames, need to add:
And here is my working solution, maybe not elegant, but it seems to work:
SELECT s.Id AS ScheduleId
, s.GroupName
, a.Id AS AppId
, a.AppName
,p.HostName
FROM ScheduleTickets t
LEFT JOIN Schedules s ON s.Id = t.ScheduleId
LEFT JOIN ScheduleTargets st on st.ScheduleId = s.Id
LEFT JOIN dbo.PatchingTargets p on p.Id = st.PatchingTargetId
LEFT JOIN SchwabApplications a ON a.Id = s.AppId
WHERE PatchSessionId = 19 AND SCCMCollectionId IS NUll
AND s.Id IN (
SELECT TOP 10 s.Id AS ScheduleId
FROM ScheduleTickets t
LEFT JOIN Schedules s ON s.Id = t.ScheduleId
LEFT JOIN SchwabApplications a ON a.Id = s.AppId
WHERE t.PatchSessionId = 19 AND t.SCCMCollectionId IS NUll
ORDER BY a.PatchingPriority, s.Idx
)
ORDER BY a.PatchingPriority, s.Idx