0

我正在尝试从我们的服务台获取一份报告,该报告将按用户为我提供给定一周内打开和关闭的问题的数量。

周由提交查询的日期确定。

定义打开的问题的数量很容易,因为它只是计算那一周的 ID 数量。

问题是,因为我是在开放日期之前这样做的,所以我无法弄清楚如何计算在同一时期已关闭的问题数量。

数据示例如下:

    ID  | Open Date  | Closed Date
    1   | 01/01/2013 | 01/01/2013
    2   | 05/01/2013 | 05/01/2013
    3   | 05/01/2013 | 10/01/2013
    4   | 10/01/2013 | 10/01/2013
    5   | 15/01/2013 |

在这个例子中,我希望报告说:

  Week | Opened | Closed
  1    | 3      | 2
  2    | 1      | 2
  3    | 1      | 0

我确定这是一个常见问题,但我已经搜索了一整天,但找不到解决方案。

我正在使用 SSRS 2008 R2。

谢谢,

菲尔

4

1 回答 1

2

您可以使用DATEPART获取一年中的周数:

更新

正如 Conrad Fix 在评论中所说,您应该将年份添加到结果集中:

SELECT  ISNULL(A.[Year],B.[Year]) [Year],
        ISNULL(A.[Week],B.[Week]) [Week],
        ISNULL(A.Opened,0) Opened,
        ISNULL(B.Closed,0) Closed
FROM (  SELECT  YEAR(OpenDate) [Year],
                DATEPART(WEEK,OpenDate) [Week],
                COUNT(*) Opened
        FROM YourTable 
        WHERE [OpenDate] IS NOT NULL
        GROUP BY YEAR(OpenDate), DATEPART(WEEK,OpenDate)) A
FULL JOIN ( SELECT  YEAR(ClosedDate) [Year],
                    DATEPART(WEEK,ClosedDate) [Week],
                    COUNT(*) Closed
            FROM YourTable 
            WHERE [ClosedDate] IS NOT NULL
            GROUP BY YEAR(ClosedDate), DATEPART(WEEK,ClosedDate)) B
    ON A.[Year] = B.[Year] AND A.[Week] = B.[Week]
ORDER BY [Year], [Week]

结果:

╔══════╦══════╦════════╦════════╗
║ Year ║ Week ║ Opened ║ Closed ║
╠══════╬══════╬════════╬════════╣
║ 2013 ║    1 ║      3 ║      2 ║
║ 2013 ║    2 ║      1 ║      2 ║
║ 2013 ║    3 ║      1 ║      0 ║
╚══════╩══════╩════════╩════════╝

这是一个带有演示的sqlfiddle 。

于 2013-06-17T18:10:14.047 回答