I have two tables:
CREATE TABLE #HOURS
(DAY INTEGER,
HOUR INTEGER)
CREATE TABLE #PERSONS
(DAY INTEGER, HOUR INTEGER,
Name NVARCHAR(50))
GO
INSERT #HOURS VALUES (1, 5)
INSERT #HOURS VALUES (1, 6)
INSERT #HOURS VALUES (1, 8)
INSERT #HOURS VALUES (1, 10)
INSERT #HOURS VALUES (1, 14)
INSERT #HOURS VALUES (1, 15)
INSERT #HOURS VALUES (1, 16)
INSERT #HOURS VALUES (1, 17)
INSERT #HOURS VALUES (1, 18)
INSERT #PERSONS VALUES (1, 5, 'Steve')
INSERT #PERSONS VALUES (1, 6, 'Steve')
INSERT #PERSONS VALUES (1, 7, 'Steve')
INSERT #PERSONS VALUES (1, 8, 'Steve')
INSERT #PERSONS VALUES (1, 10, 'Steve')
INSERT #PERSONS VALUES (1, 14, 'Steve')
INSERT #PERSONS VALUES (1, 15, 'Steve')
INSERT #PERSONS VALUES (1, 16, 'Steve')
INSERT #PERSONS VALUES (1, 17, 'Steve')
INSERT #PERSONS VALUES (1, 10, 'Jim')
INSERT #PERSONS VALUES (1, 11, 'Jim')
INSERT #PERSONS VALUES (1, 12, 'Jim')
INSERT #PERSONS VALUES (1, 13, 'Jim')
GO
Hours shows the work hours and #Persons shows the persons that entered the system on hourly base. I'd like to find the persons whose work hours matches the hours table. But he or she can skip two work hours.
I've tried this:
select t.Day, sum(t.Nulls)
from
(select h.Hour, h.Day
, Case
WHEN p.Hour is null Then 1 ELSE 0 END Nulls
from #HOURS h
left join #PERSONS P on h.Hour = p.Hour AND h.Day = p.Day) t
group by t.Day
HAVING sum(t.Nulls) < 2
But this only works when there is not different persons on the same day ;)
Any suggestions?