【发布时间】:2016-11-24 14:17:40
【问题描述】:
将一个相当冗长的 SQL 数据透视表数据集导入到 SQL Server Report Builder 3.0 中时,我遇到了麻烦,该数据集允许我将参数添加到报表结果中。我知道这要求查询对 T-SQL 友好
如果有帮助,上下文是我正在构建一份报告以查看各种市场研究小组的资格状态,并且我希望能够提供一个下拉菜单以让用户轻弹面板之间。所以结尾@parameter 将在PanelCode / PanelName 上。这是一个复合查询:
SELECT
ELT.PanelCode,
ELR.PanelName,
ELR.Year,
ELT.PeriodType,
ELT.PeriodValue,
ELT.TotalPanelists,
ELT.EligiblePanelists,
ELR.TotalEligible,
ELR.TotalVacation,
ELR.TotalExcused,
ELR.TotalInactive,
ELR.TotalConnection,
ELR.TotalCompliance
FROM --the Ineligibility Reason Pivot Table (ELR)
(SELECT
PanelCode,
PanelName,
Year,
PeriodType,
PeriodValue,
Max([Eligible]) as TotalEligible,
Max([Vacation]) as TotalVacation,
Max([Excuse]) as TotalExcused,
Max([Inactive]) as TotalInactive,
Max([Connection]) as TotalConnection,
Max([Compliance]) as TotalCompliance
FROM
(SELECT
PanelCode,
PanelName,
Year,
PeriodType,
PeriodValue,
EligibilityFailureReason,
FROM FullPanellistEligibilityView) FPR
Pivot
(count(EligibilityFailureReason) FOR EligibilityFailureReason IN ([Eligible], [Vacation], [Excuse], [Inactive], [Connection], [Compliance])) AS PVT
WHERE PeriodType <> '4 week period' and Year > 2012
GROUP BY PanelCode, PanelName, PeriodType, Year, PeriodValue) as ELR
, -- And the Eligibility Totals Query, ELT
(
SELECT
PanelCode,
PanelName,
Year,
PeriodType,
PeriodValue,
Count(Poll1s) as TotalPanelists,
Sum(Poll1s) as EligiblePanelists
FROM
(SELECT
PanelCode,
PanelName,
Year
PeriodType,
PeriodValue,
CAST(isEligible as INT) as Poll1s
FROM FullPanellistEligibilityView) FPR
GROUP BY PanelCode, PeriodType, PeriodValue) ELT
WHERE (ELT.PeriodValue=ELR.PeriodValue) and (ELT.PanelCode=ELR.PanelCode)
我一直在努力寻找在线资源,这些资源建议如何在 Report Builder 3 中处理更大的查询并使其成为参数化。除了 WHERE PanelName = @PanelName 之外,我还需要添加什么才能运行?
EDIT1:我不怀疑我已经使这个查询变得比必要的复杂得多,我是自学的。架构并不是真正必要的,因为所有这些数据都是从一个已经存在的视图FullPanellistEligibilityView 中提取的,示例数据,从视图中剥离和模拟, can be found here
【问题讨论】:
-
能否请您发布架构和一些测试数据
FullPanellistEligibilityView?这实际上看起来像一个非常简单的查询,但它比它需要的复杂得多。
标签: sql sql-server tsql reporting-services reportbuilder3.0