[英]Converting SQL pivot table to T-SQL for Report Builder 3.0
我一直在費心地將相當冗長的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
, FullPanellistEligibilityView
,從視圖中剝離並FullPanellistEligibilityView
示例數據, 可以在這里找到
要設置數據驅動的參數選擇,您需要做兩件事。
首先,您需要創建一個數據集以填充參數下拉菜單。 這需要以正確的順序列出您希望用戶能夠選擇的所有值。 這可以為顯示給用戶的Label和返回值分別返回一列:
select distinct PanelCode -- Parameter Value
,PanelName -- Parameter Label
from FullPanellistEligibilityView
order by PanelName
創建一個參數,並為該數據集設置可用值,並為Label
和Value
屬性使用適當的列。
其次,您需要向數據集添加過濾器。 我已經自由地在上面重寫了您的查詢,以使用派生表/公用表表達式/ cte代替您的PIVOT
。 下面的代碼包含對SSRS參數的引用,一旦選擇,SSRS參數將為該參數插入“值”。 顯然,由於我沒有您的架構,因此未測試此代碼,但是設計應該足夠容易理解:
with t
as
(
select PanelCode
,PeriodValue
,count(isEligible) as TotalPanelists -- I'm assuming this is a BIT column, in which case it shouldn't have any null values. If it does, you will need to handle this with count(isnull(isEligible,0))
,Sum(CAST(isEligible as INT)) as EligiblePanelists
from FullPanellistEligibilityView
where PanelCode = @PanelCode -- This will filter your data due to the INNER JOIN below.
group by PanelCode
,PeriodType
,PeriodValue
)
select e.PanelCode
,e.PanelName
,e.Year
,e.PeriodType
,e.PeriodValue
,t.TotalPanelists
,t.EligiblePanelists
,sum(case when e.EligibilityFailureReason = 'Eligible' then 1 else 0 end) as TotalEligible,
,sum(case when e.EligibilityFailureReason = 'Vacation' then 1 else 0 end) as TotalVacation,
,sum(case when e.EligibilityFailureReason = 'Excuse' then 1 else 0 end) as TotalExcused,
,sum(case when e.EligibilityFailureReason = 'Inactive' then 1 else 0 end) as TotalInactive,
,sum(case when e.EligibilityFailureReason = 'Connection' then 1 else 0 end) as TotalConnection,
,sum(case when e.EligibilityFailureReason = 'Compliance' then 1 else 0 end) as TotalCompliance
from FullPanellistEligibilityView e
inner join t
on(e.PanelCode = t.PanelValue
and e.PeriodValue = t.PeriodValue
)
where e.PeriodType <> '4 week period'
and e.Year > 2012
group by e.PanelCode
,e.PanelName
,e.Year
,e.PeriodType
,e.PeriodValue
,t.TotalPanelists
,t.EligiblePanelists
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.