您的 SQL Fiddle 设置为使用 MYSQL,而不是 SQL Server 2008,因此我无法根据您的数据测试我的答案:但是,根据我对您的要求的理解并假设您正在查询 SQL 2008 数据库,以下示例应该可以工作为您(对我的表变量的引用显然会替换为您的实际表)。
DECLARE @StartDate DATE = '06/04/2014'
DECLARE @EndDate DATE = GETDATE();
DECLARE @LogTimePeriod TABLE (LogTimePeriodID INT IDENTITY(1,1), TimePeriod VARCHAR(20))
INSERT INTO @LogTImePeriod (TimePeriod) SELECT '00:00 - 07:59'
INSERT INTO @LogTImePeriod (TimePeriod) SELECT '08:00 - 15:59'
INSERT INTO @LogTImePeriod (TimePeriod) SELECT '16:00 - 23:59'
DECLARE @logs TABLE (LogDataID INT IDENTITY(1,1), LogDate DATE, SomeInformation VARCHAR(10), LogTimePeriodID INT)
INSERT INTO @logs (SomeInformation, LogDate, LogTimePeriodID) SELECT 'abc', '6/4/2014', 1
INSERT INTO @logs (SomeInformation, LogDate, LogTimePeriodID) SELECT 'def', '6/4/2014', 2
INSERT INTO @logs (SomeInformation, LogDate, LogTimePeriodID) SELECT 'ghi', '6/4/2014', 3
INSERT INTO @logs (SomeInformation, LogDate, LogTimePeriodID) SELECT 'abc', '6/5/2014', 1
INSERT INTO @logs (SomeInformation, LogDate, LogTimePeriodID) SELECT 'def', '6/5/2014', 2;
WITH dates AS (
SELECT CAST(@StartDate AS DATETIME) 'date'
UNION ALL
SELECT DATEADD(dd, 1, t.date)
FROM dates t
WHERE DATEADD(dd, 1, t.date) <= @EndDate)
SELECT ltp.LogTimePeriodID, ltp.TimePeriod, dates.date
FROM
@LogTimePeriod ltp
INNER JOIN
dates ON 1=1
LEFT JOIN
@logs ld ON
ltp.LogTimePeriodID = ld.LogTimePeriodID AND
dates.date = ld.LogDate
WHERE ld.LogDataID IS NULL
OPTION (MAXRECURSION 1000) -- 0 is unlimited, 1000 limits to 1000 rows