SELECT Staff, Building,
CAST(ScanTime AS DATE) AS ScanDate,
MIN(ScanTime) AS FirstScanTime,
MAX(ScanTime) AS LastScanTime
FROM dbo.[SentryJuno$]
GROUP BY Staff, Building, CAST(ScanTime AS DATE);
Или, глядя на ваш запрос, возможно, вы спрашиваете:
SELECT Staff, Building,
CAST(ScanTime AS DATE) AS ScanDate,
MIN(ScanTime) AS ScanTime
FROM dbo.[SentryJuno$]
GROUP BY Staff, Building, CAST(ScanTime AS DATE)
UNION
SELECT Staff, Building,
CAST(ScanTime AS DATE) AS ScanDate,
MAX(ScanTime) AS ScanTime
FROM dbo.[SentryJuno$]
GROUP BY Staff, Building, CAST(ScanTime AS DATE)
РЕДАКТИРОВАТЬ: Ваш вопрос не ясен, читая комментарии, может быть, вы просите об этом?:
SELECT *
FROM dbo.SentryJuno$ s
INNER JOIN(
SELECT Building, CAST(Scantime AS DATE) AS ScanDate, MIN(Scantime) AS ScanTime
FROM dbo.SentryJuno$
GROUP BY Building, CAST(Scantime AS DATE)
UNION
SELECT Building, CAST(Scantime AS DATE) AS ScanDate, MAX(Scantime) AS ScanTime
FROM dbo.SentryJuno$
GROUP BY Building, CAST(Scantime AS DATE)
) fl ON fl.Building=s.Building
AND fl.ScanTime=s.Scantime;