我在SSRS中创建了一个简单的表,其中左侧有员工,每列的编号为1 - 31.当员工预订假期时,使用if函数将X放入他们度假的日期如果日期介于日期和日期之间。
这样可以正常工作,但是当月内预订了多个假期时,员工将会有重复的行,而不是表格中的同一行。添加组功能时,它仅显示他们在当月预订的第一个假期。是否可以将所有行显示在一个?
这是我的SQL:
SELECT *
,firstname + ' ' + lastname AS 'full'
,datediff(d, holidaystart, holidayend) AS 'days'
,DAY(DATEADD(s, - 1, DATEADD(mm, DATEDIFF(m, 0, holidaystart) + 1, 0))) AS LastDay
FROM dbo.holdates
WHERE Month(holidaystart) = Param1
AND holyear = '8'
AND holidaystat = 'Approved'
AND location = ISNULL(Param2, location)
AND (holyear = @Param3 - 2005)
OR Month(holidayend) = Param1
AND (holyear = Param3 - 2005)
AND holidaystat = 'Approved'
AND location = ISNULL(Param2, location)
ORDER BY (firstname + ' ' + lastname)
答案 0 :(得分:0)
如果您使用CTE生成要报告的日期范围,然后将其与实际假日日期相关联,则会变得更加容易。 CTE看起来像这样:
DECLARE @fromDate DATE = '20130701',
@toDate DATE = '20130801';
WITH dates
AS ( SELECT @fromDate "date"
UNION ALL
SELECT DATEADD(dd, 1, "date")
FROM dates
WHERE DATEADD(dd, 1, "date") <= @toDate
)
-- Continued below...
然后根据此CTE选择您的数据:
SELECT dates."date"
,CASE WHEN h.holidaystart IS NOT NULL THEN 'X' ELSE NULL END AS HasHoliday
,firstname + ' ' + lastname AS "full"
,datediff(d, holidaystart, holidayend) AS "days"
FROM dates
LEFT JOIN @holdates h ON dates."date" BETWEEN h.holidaystart AND h.holidayend
WHERE Month(holidaystart) = Param1
AND h.holyear = '8'
AND h.holidaystat = 'Approved'
AND h.location = ISNULL(Param2, location)
ORDER BY (firstname + ' ' + lastname)
这将在这些行中给出结果集:
date HasHoliday full
--------------- ----------- ---------
2013-07-01 NULL NULL
2013-07-02 NULL NULL
2013-07-03 NULL NULL
2013-07-04 NULL NULL
2013-07-05 X John
2013-07-05 X Mark
2013-07-06 X John
2013-07-06 X Mark
2013-07-07 X John
etc.
现在在SSRS中,您需要做的就是在以下位置添加一个矩阵:
HasHoliday
值无论如何,上述数据集是创建类似日历的报告的更方便的基础。