s-s-rS - 日历报表查询
Posted
技术标签:
【中文标题】s-s-rS - 日历报表查询【英文标题】:s-s-rS - Calendar Report Query 【发布时间】:2017-04-28 16:22:28 【问题描述】:我正在制作类似于this 的日历样式报告。但是我的查询没有返回我需要的内容。我在日期表上使用RIGHT OUTER JOIN
,但我还需要通过JobID
过滤结果。当我这样做时,它显然只返回带有JobID
的结果。我觉得我在这里遗漏了一些简单的东西......任何帮助将不胜感激!
原始查询:
SELECT t1.[TimeSheetLineID] ,
t1.[TimeSheetID] ,
t1.[JobID] ,
t1.[TimeSheetDate] ,
t1.[TimeSheetCreatedOn] ,
t1.[TimeSheetLastModifiedOn] ,
t1.[StartTime] ,
t1.[EndTime] ,
t1.[TotalTime] ,
t1.[EmployeeName] ,
t2.[FullYear] ,
t2.[WeekNumber] ,
t2.[WeekDay] ,
t2.[WeekDayName] ,
t2.[MonthDay] ,
t2.[MonthName] ,
t2.[MonthNumber]
FROM [dbo].[FactTimeSheets] t1
RIGHT OUTER JOIN dbo.DateLookup t2 ON t1.TimeSheetDate = t2.DateFull
WHERE (t1.JobID = @jobNumber) AND (t2.FullYear = @year) AND (t2.MonthNumber BETWEEN @startMonth AND @endMonth)
ORDER BY FullYear, MonthNumber, MonthDay, WeekDay
我从上述查询中得到的结果:
第二次查询,提供所有需要但未按 JobID 过滤的日期:
SELECT t1.[TimeSheetLineID] ,
t1.[TimeSheetID] ,
t1.[JobID] ,
t1.[TimeSheetDate] ,
t1.[TimeSheetCreatedOn] ,
t1.[TimeSheetLastModifiedOn] ,
t1.[StartTime] ,
t1.[EndTime] ,
t1.[TotalTime] ,
t1.[EmployeeName] ,
t2.[FullYear] ,
t2.[WeekNumber] ,
t2.[WeekDay] ,
t2.[WeekDayName] ,
t2.[MonthDay] ,
t2.[MonthName] ,
t2.[MonthNumber]
FROM [dbo].[FactTimeSheets] t1
RIGHT OUTER JOIN dbo.DateLookup t2 ON t1.TimeSheetDate = t2.DateFull
WHERE (t2.FullYear = @year) AND (t2.MonthNumber BETWEEN @startMonth AND @endMonth)
ORDER BY FullYear, MonthNumber, MonthDay, WeekDay
我希望看到的结果(JobID 过滤器已取出...但我需要它):
【问题讨论】:
【参考方案1】:尝试反转它...此外,如果您在 where 子句中对外部连接表设置条件,它将像内部连接表一样
SELECT t1.[TimeSheetLineID] ,
t1.[TimeSheetID] ,
t1.[JobID] ,
t1.[TimeSheetDate] ,
t1.[TimeSheetCreatedOn] ,
t1.[TimeSheetLastModifiedOn] ,
t1.[StartTime] ,
t1.[EndTime] ,
t1.[TotalTime] ,
t1.[EmployeeName] ,
t2.[FullYear] ,
t2.[WeekNumber] ,
t2.[WeekDay] ,
t2.[WeekDayName] ,
t2.[MonthDay] ,
t2.[MonthName] ,
t2.[MonthNumber]
FROM dbo.DateLookup t2
LEFT JOIN [dbo].[FactTimeSheets] t1
ON t1.TimeSheetDate = t2.DateFull
AND (t1.JobID = @jobNumber)
WHERE (t2.FullYear = @year)
AND (t2.MonthNumber BETWEEN @startMonth AND @endMonth)
ORDER BY FullYear, MonthNumber, MonthDay, WeekDay
【讨论】:
这将更接近解决方案,但是,我担心上面的查询会在日历中留下给定工作的给定日历日没有数据的空白。 @RossBush 怎么样? 噢!我的错。出于某种原因,我把这张桌子颠倒过来了。以上是关于s-s-rS - 日历报表查询的主要内容,如果未能解决你的问题,请参考以下文章