SQL Server 2008 SSRS一个报告多个数据集

时间:2011-05-18 09:07:00

标签: sql sql-server sql-server-2008 ssrs-2008

我是SSRS的新手,我有一份报告需要在3个不同的查询之间切换,具体取决于用户想要查看的内容,即耗材,服务或总计(两者合并)最佳方法是什么?< / p>

以下查询:

--Consumables 
SELECT COUNT(OrderId)             AS [Consumable_Order_Amount], 
       CONVERT(DATE, DateCreated) AS [Date], 
       CASE 
         WHEN orderheader.webref = '' THEN 'Call Centre' 
         ELSE 'Web' 
       END                        AS [Consumable_Order_Type] 
FROM   OrderHeader 
WHERE  ( CONVERT(DATE, DateCreated) BETWEEN @StartDate AND @EndDate ) 
GROUP  BY CONVERT(DATE, DateCreated), 
          CASE 
            WHEN orderheader.webref = '' THEN 'Call Centre' 
            ELSE 'Web' 
          END 

--Service 
SELECT COUNT(serviceId)           AS [Service_Order_Amount], 
       CONVERT(DATE, DateCreated) AS [Date], 
       CASE 
         WHEN serviceorder.webref = '' THEN 'Call Centre' 
         ELSE 'Web' 
       END                        AS [Service_Order_Type] 
FROM   ServiceOrder 
WHERE  ( CONVERT(DATE, DateCreated) BETWEEN @StartDate AND @EndDate ) 
GROUP  BY CONVERT(DATE, DateCreated), 
          CASE 
            WHEN serviceorder.webref = '' THEN 'Call Centre' 
            ELSE 'Web' 
          END 

--Total           
;

WITH [Total_Order_Amount] 
     AS (SELECT orderid     AS [ID], 
                datecreated AS [Date], 
                webref      AS [WebRef] 
         FROM   orderheader 
         UNION 
         SELECT serviceid   AS [ID], 
                datecreated AS [Date], 
                webref      AS [WebRef] 
         FROM   serviceorder) 
SELECT COUNT(id)                  AS [Service_Order_Amount], 
       CONVERT(DATE, date) AS [Date], 
       CASE 
         WHEN webref = '' THEN 'Call Centre' 
         ELSE 'Web' 
       END                        AS [Service_Order_Type] 
FROM   [Total_Order_Amount] 
WHERE  ( CONVERT(DATE, date) BETWEEN @StartDate AND @EndDate ) 
GROUP  BY CONVERT(DATE, date), 
          CASE 
            WHEN webref = '' THEN 'Call Centre' 
            ELSE 'Web' 
          END 

3 个答案:

答案 0 :(得分:1)

你真的不想拥有3个网格,因为你需要单独维护它们,应该说要添加一个新列。但是,如果您确定查询始终具有相同的列,则可以使用存储过程决定运行哪个查询。好的,这里有关于性能和执行计划的单独问题(我们可以稍后介绍)。

答案 1 :(得分:0)

为什么不为报告创建一个参数,其中包含映射到每个查询的预定义选项列表。然后可以将此参数提供给存储过程,并且它可以确定要返回的子查询。

例如:创建一个名为QueryOption的报告参数,其数据类型为Integer
添加ConsumablesServiceTotal的可用值,值为1,2&amp; 3。 如果要为用户自动选择选项,请将默认值设置为1,2或3。

修改存储过程,使用QueryOption参数确定要返回的子查询。

IF @QueryOption = 1
BEGIN
    SELECT 'Consumables' -- Add consumables query here
END
IF @QueryOption = 2
BEGIN
    SELECT 'Service'    -- Add Service query here
END
IF @QueryOption = 3
BEGIN
    SELECT 'Total'      -- Add Total query here
END

在存储过程的DataSet属性上,确保刷新字段以便自动添加QueryOption参数。
只要sp的参数名称与报告参数名称匹配,参数值就会被送入存储过程。

答案 2 :(得分:0)

我找到了最简单/最干净的解决方案,根据报告类型将参数传递给select查询,并结合where子句中的case语句来过滤掉内存中的不同报告类型。除非有任何异议,否则我会将此标记为答案!谢谢你的帮助。

WITH [Total_Order_Amount] 
     AS (SELECT orderid     AS [ID], 
                datecreated AS [Date], 
                webref      AS [WebRef], 
                'Consumable'AS [ReportType] 
         FROM   orderheader 
         UNION 
         SELECT serviceid   AS [ID], 
                datecreated AS [Date], 
                webref      AS [WebRef], 
                'Service'   AS [ReportType] 
         FROM   serviceorder) 
SELECT COUNT(id)           AS [Order_Amount], 
       CONVERT(DATE, DATE) AS [Date], 
       CASE 
         WHEN webref = '' THEN 'Call Centre' 
         ELSE 'Web' 
       END                 AS [Order_Type] 
FROM   [Total_Order_Amount] 
WHERE  ( CONVERT(DATE, DATE) BETWEEN @StartDate AND @EndDate ) 
       AND [ReportType] = CASE @ReportType 
                          WHEN 1 THEN 'Consumable' 
                          WHEN 2 THEN 'Service' 
                          ELSE [ReportType] 
                        END 
GROUP  BY CONVERT(DATE, DATE), 
          CASE 
            WHEN webref = '' THEN 'Call Centre' 
            ELSE 'Web' 
          END