0

我在 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)
4

1 回答 1

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单元格中的值

无论如何,上述数据集是创建类似日历的报告的更方便的基础。

于 2013-04-30T12:21:31.463 回答