Это должно помочь вам в правильном направлении. (Полный пример) . Теперь вы сможете сгруппировать по тому, что вам нужно, и суммировать поле Dups .
DECLARE @Table TABLE(
ID INT,
[Date] DATETIME,
[Status] VARCHAR(100),
[Physician ID] INT
)
INSERT INTO @Table SELECT 44397479,'10/13/2009 17:40','NOSHOW' , NULL
INSERT INTO @Table SELECT 44397480,'10/13/2009 16:40','CHECKED OUT' ,108
INSERT INTO @Table SELECT 44397481,'10/13/2009 14:32','CHECKED OUT' ,108
INSERT INTO @Table SELECT 44397482,'10/14/2009 10:44','RESCHEDULED' ,108
INSERT INTO @Table SELECT 44397483,'8/26/2009 12:35','RESCHEDULED' ,108
INSERT INTO @Table SELECT 44397484,'10/13/2009 15:26','CHECKED OUT' ,108
INSERT INTO @Table SELECT 56084463,'10/10/2009 10:00','PENDING' ,108
INSERT INTO @Table SELECT 56287658,'10/14/2009 10:44','PENDING' ,108
INSERT INTO @Table SELECT 54728032,'10/13/2009 15:56','CHECKED OUT' ,108
SELECT *,
CASE
WHEN t.[Status] = 'PENDING'
AND EXISTS(
SELECT *
FROM @Table
WHERE DATEPART(hh, [Date]) = DATEPART(hh, t.[Date])
AND DATEPART(MI, [Date]) = DATEPART(MI, t.[Date])
AND ID <> t.ID)
AND [Physician ID] = t.[Physician ID]
AND Status = 'RESCHEDULED'
THEN 0
ELSE 1
END Dups
FROM @Table t