2017-03-03 22 views
1

我有一個報表,它有一個鑽取子報表,當它有多個關係到多個項目時,它與子報表無關。SSRS子報表多次運行,我只希望它運行一次

主要報表查詢即使

SELECT DISTINCT 
         cat.CategoryName AS 'Category Name', sub.SubCategoryName AS 'SubCategory Name', cur.Status, cur.PastConsiderationFlag, cur.Model, cur.Version, cur.Vendor, cur.AvailableDate AS 'Available Date', cur.EndOfProduction AS 'End of Production', 
         cur.EndOfSupport AS 'End of Support', dep.DepartmentName AS 'Department Name', emp.FirstName + ' ' + emp.LastName AS 'Tech Owner', emp2.FirstName + ' ' + emp2.LastName AS 'Tech Contact', 
         cur.NumOfDevices AS '# of Devices', cur.UpgradeDuration AS 'Upgrade Duration', cur.FiscalConsideration AS 'Fiscal Consideration', cur.Description, cur.SupportingComments, cur.CurrencyId, STUFF 
          ((SELECT  ', ' + pl.PlatformName AS Expr1 
           FROM   Platform AS pl LEFT OUTER JOIN 
                  Currency_Platform AS cp ON cur.CurrencyId = cp.CurrencyId 
           WHERE  (pl.PlatformId = cp.PlatformId) FOR XML PATH('')), 1, 1, '') AS 'Platforms', ISNULL(STUFF 
          ((SELECT  ', ' + cu2.Model AS Expr1 
           FROM   Currency AS cu2 RIGHT OUTER JOIN 
                  Currency_Dependency AS cd ON cur.CurrencyId = cd.CurrencyId 
           WHERE  (cu2.CurrencyId = cd.DependencyId) FOR XML PATH('')), 1, 1, ''), 'N/A') AS 'Dependencies', ISNULL(STUFF 
          ((SELECT  ', ' + cu2.Model AS Expr1 
           FROM   Currency AS cu2 RIGHT OUTER JOIN 
                  Currency_Affected AS ca ON cur.CurrencyId = ca.CurrencyId 
           WHERE  (cu2.CurrencyId = ca.AffectedId) FOR XML PATH('')), 1, 1, ''), 'N/A') AS 'Affected Apps', Currency_Platform.PlatformId 
FROM   Currency AS cur INNER JOIN 
         SubCategory AS sub ON cur.SubCategoryId = sub.SubCategoryId INNER JOIN 
         Category AS cat ON sub.CategoryId = cat.CategoryId LEFT OUTER JOIN 
         Employee AS emp ON cur.OwnerId = emp.EmployeeId LEFT OUTER JOIN 
         Employee AS emp2 ON cur.ContactId = emp2.EmployeeId LEFT OUTER JOIN 
         Department AS dep ON cur.PortfolioOwnerId = dep.DepartmentId LEFT OUTER JOIN 
         Currency_Platform ON cur.CurrencyId = Currency_Platform.CurrencyId 

這是一個明顯的選擇,子報表將運行等於它屬於平臺的數量。我將在這裏包含子報表的查詢。

;with cte as (
-- anchor elements: where curr.Status = 1 and not a dependent 
    select 
     CurrencyId 
    , Model 
    , Version 
    , ParentId  = null 
    , ParentModel = convert(varchar(128),'') 
    , Root   = curr.Model 
    , [Level]  = convert(int,0) 
    , [ParentPath] = convert(varchar(512),Model + Version) 
    from dbo.Currency as curr 
    where curr.Status = 1 
    /* anchor's do not depend on any other currency */ 
    and not exists (
     select 1 
     from dbo.Currency_Dependency i 
     where curr.CurrencyId = i.DependencyId 
    ) 
    -- recursion begins here 
    union all 
    select 
     CurrencyId = c.CurrencyId 
    , Model  = c.Model 
    , Version  = c.Version 
    , ParentId  = p.CurrencyId 
    , ParentModel = convert(varchar(128),p.Model + p.Version) 
    , Root   = p.Root 
    , [Level]  = p.[Level] + 1 
    , [ParentPath] = convert(varchar(512),p.[ParentPath] + ' > ' + c.Model + ' ' + c.Version) 
    from dbo.Currency as c 
    inner join dbo.Currency_Dependency as dep 
     on c.CurrencyId = dep.DependencyId 
    inner join cte as p 
     on dep.CurrencyId = p.CurrencyId 
) 
select CurrencyId, ParentPath, Model + ' ' + Version AS 'Model' from cte 
WHERE CurrencyId = @CurrencyId 

當我單獨運行子報表時,一切都很好。當我通過傳遞CurrencyId作爲參數的主報告打開子報表時,它的數量與其所屬平臺的數量相同。

有沒有一種方法可以通過改進查詢來糾正這種情況,或者我更喜歡強制子報表只運行一次?

非常感謝您的光臨。

+1

您可以使用SQL Server Profiler來看看有多少次與參數是什麼子報表查詢正在運行?您的第一個查詢返回多少個值,以及子報表運行多少次? – jambonick

+0

我不確定SQL Server Profiler是什麼,但給了我一下來查看它,我會看到它產生的信息。 – David

+1

看看這裏供將來使用https://www.codeproject.com/Articles/21371/SQL-Server-Profiler-Step-by-Step。仍然在sql server中運行您的第一個查詢並向我們顯示其結果 – jambonick

回答

1

您可以使用SQL Server Profiler檢查以下事項。

  1. 多少次,用什麼參數是報表查詢跑
  2. 多少值第一個查詢返回
+0

再次感謝您向我展示該工具。這很容易找到問題。 – David

1

我不認爲你的問題更多關於SSRS而不是關於你的T-SQL代碼。我要猜測並說子報表對象在報表的報表詳細信息部分。這意味着子報表將爲主查詢數據集中的每一行渲染一次。我不知道你的容器報告實際上是什麼樣子,但你有一個選擇可能是將子報表包含在頁眉或頁腳部分,並使其從MAX(),MIN()運行,其值爲你知道每行都是一樣的。

+0

我又看了一遍主查詢,發現它包含在查詢中的PlatformId能夠用它作爲參數來過濾報告。 我非常感激你看着這個雖然! – David