如何在SSRS 2012中将Grand Total显示为堆积图表中的单独栏

时间:2017-04-14 17:29:05

标签: sql-server tsql reporting-services stacked-chart

如何在堆叠图表中将Grand Total显示为单独的栏? 我按类别组细分了New BusinessRenewal。那么我怎么能有另一个顶级栏,它会显示$New Business的{​​{1}}总和? 这样的事情:

enter image description here

编辑:

下面是我的主要SELECT语句。我创造了 在第一个Renewal我创建了EQGrandTotal'作为另一个UNION ALL,我总结了按描述细分的所有类型的保费。

BusinessType

所以我得到了另一个" BusinessType"但是这条线是纯色的。 我对 SELECT SUM(CASE WHEN LineOfCoverage = 'Earthquake' AND BusinessType = 'New Business' AND Description = 'Bound' THEN [Bound Premium] ELSE 0 END) as EQBoundNB, SUM(CASE WHEN LineOfCoverage = 'Earthquake' AND BusinessType = 'New Business' AND Description = 'Lost' THEN [LostPremium] ELSE 0 END) as EQLostNB, SUM(CASE WHEN LineOfCoverage = 'Earthquake' AND BusinessType = 'New Business' AND Description = 'Not Taken Up' THEN [NotTakenUpPremium] ELSE 0 END) as EQNtUpNB, SUM(CASE WHEN LineOfCoverage = 'Earthquake' AND BusinessType = 'New Business' AND Description = 'Quoted' THEN [QuotedPremium] ELSE 0 END) as EQQuotedNB, /* Earthquake, Renewal, by Description */ SUM(CASE WHEN LineOfCoverage = 'Earthquake' AND BusinessType = 'Renewal' AND Description = 'Bound' THEN [Bound Premium] ELSE 0 END) as EQBoundRN, SUM(CASE WHEN LineOfCoverage = 'Earthquake' AND BusinessType = 'Renewal' AND Description = 'Lost' THEN [LostPremium] ELSE 0 END) as EQLostRN, SUM(CASE WHEN LineOfCoverage = 'Earthquake' AND BusinessType = 'Renewal' AND Description = 'Not Taken Up' THEN [NotTakenUpPremium] ELSE 0 END) as EQNtUpRN, SUM(CASE WHEN LineOfCoverage = 'Earthquake' AND BusinessType = 'Renewal' AND Description = 'Quoted' THEN [QuotedPremium] ELSE 0 END) as EQQuotedRN, /* Wind, New Business, by Description */ SUM(CASE WHEN LineOfCoverage = 'Wind' AND BusinessType = 'New Business' AND Description = 'Bound' THEN [Bound Premium] ELSE 0 END) as WindBoundNB, SUM(CASE WHEN LineOfCoverage = 'Wind' AND BusinessType = 'New Business' AND Description = 'Lost' THEN [LostPremium] ELSE 0 END) as WindLostNB, SUM(CASE WHEN LineOfCoverage = 'Wind' AND BusinessType = 'New Business' AND Description = 'Not Taken Up' THEN [NotTakenUpPremium] ELSE 0 END) as WindNtUpNB, SUM(CASE WHEN LineOfCoverage = 'Wind' AND BusinessType = 'New Business' AND Description = 'Quoted' THEN [QuotedPremium] ELSE 0 END) as WindQuotedNB, /* Wind, Renewal, by Description */ SUM(CASE WHEN LineOfCoverage = 'Wind' AND BusinessType = 'Renewal' AND Description = 'Bound' THEN [Bound Premium] ELSE 0 END) as WindBoundRN, SUM(CASE WHEN LineOfCoverage = 'Wind' AND BusinessType = 'Renewal' AND Description = 'Lost' THEN [LostPremium] ELSE 0 END) as WindLostRN, SUM(CASE WHEN LineOfCoverage = 'Wind' AND BusinessType = 'Renewal' AND Description = 'Not Taken Up' THEN [NotTakenUpPremium] ELSE 0 END) as WindNtUpRN, SUM(CASE WHEN LineOfCoverage = 'Wind' AND BusinessType = 'Renewal' AND Description = 'Quoted' THEN [QuotedPremium] ELSE 0 END) as WindQuotedRN, CASE WHEN LineOfCoverage = 'Earthquake' THEN 'Earthquake' ELSE 'Wind' END as LineOfCoverage, 'Earthquake' as Earthquake, 'Wind' as Wind, BusinessType, Description, NULL as Total FROM cte_Quotes GROUP BY LineOfCoverage,BusinessType,Description UNION ALL SELECT NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL, CASE WHEN LineOfCoverage = 'Earthquake' THEN 'Earthquake' ELSE 'Wind' END as LineOfCoverage, NULL, NULL, 'EQGrandTotal' as BusinessType, --NULL, Description, -- description SUM(CASE WHEN LineOfCoverage = 'Earthquake' AND BusinessType = 'New Business' AND Description = 'Bound' THEN [Bound Premium] ELSE 0 END)+ SUM(CASE WHEN LineOfCoverage = 'Earthquake' AND BusinessType = 'New Business' AND Description = 'Lost' THEN [LostPremium] ELSE 0 END) + SUM(CASE WHEN LineOfCoverage = 'Earthquake' AND BusinessType = 'New Business' AND Description = 'Not Taken Up' THEN [NotTakenUpPremium] ELSE 0 END)+ SUM(CASE WHEN LineOfCoverage = 'Earthquake' AND BusinessType = 'New Business' AND Description = 'Quoted' THEN [QuotedPremium] ELSE 0 END) + /* Earthquake, Renewal, by Description */ SUM(CASE WHEN LineOfCoverage = 'Earthquake' AND BusinessType = 'Renewal' AND Description = 'Bound' THEN [Bound Premium] ELSE 0 END)+ SUM(CASE WHEN LineOfCoverage = 'Earthquake' AND BusinessType = 'Renewal' AND Description = 'Lost' THEN [LostPremium] ELSE 0 END) + SUM(CASE WHEN LineOfCoverage = 'Earthquake' AND BusinessType = 'Renewal' AND Description = 'Not Taken Up' THEN [NotTakenUpPremium] ELSE 0 END)+ SUM(CASE WHEN LineOfCoverage = 'Earthquake' AND BusinessType = 'Renewal' AND Description = 'Quoted' THEN [QuotedPremium] ELSE 0 END) as Total FROM cte_Quotes GROUP BY Description,LineOfCoverage,BusinessType 列的表达式:

Total

enter image description here

编辑2: @Richards 已移除=Sum(IIF(Fields!BusinessType.Value = "EQGrandTotal" and Fields!LineOfCoverage.Value = "Earthquake", Fields!Total.Value,0)) ,而是添加了4个新列:

UNION ALL

SSRS的结果: 我在图表数据中添加了SELECT SUM(CASE WHEN LineOfCoverage = 'Earthquake' AND BusinessType = 'New Business' AND Description = 'Bound' THEN [Bound Premium] ELSE 0 END) as EQBoundNB, SUM(CASE WHEN LineOfCoverage = 'Earthquake' AND BusinessType = 'New Business' AND Description = 'Lost' THEN [LostPremium] ELSE 0 END) as EQLostNB, SUM(CASE WHEN LineOfCoverage = 'Earthquake' AND BusinessType = 'New Business' AND Description = 'Not Taken Up' THEN [NotTakenUpPremium] ELSE 0 END) as EQNtUpNB, SUM(CASE WHEN LineOfCoverage = 'Earthquake' AND BusinessType = 'New Business' AND Description = 'Quoted' THEN [QuotedPremium] ELSE 0 END) as EQQuotedNB, /* Earthquake, Renewal, by Description */ SUM(CASE WHEN LineOfCoverage = 'Earthquake' AND BusinessType = 'Renewal' AND Description = 'Bound' THEN [Bound Premium] ELSE 0 END) as EQBoundRN, SUM(CASE WHEN LineOfCoverage = 'Earthquake' AND BusinessType = 'Renewal' AND Description = 'Lost' THEN [LostPremium] ELSE 0 END) as EQLostRN, SUM(CASE WHEN LineOfCoverage = 'Earthquake' AND BusinessType = 'Renewal' AND Description = 'Not Taken Up' THEN [NotTakenUpPremium] ELSE 0 END) as EQNtUpRN, SUM(CASE WHEN LineOfCoverage = 'Earthquake' AND BusinessType = 'Renewal' AND Description = 'Quoted' THEN [QuotedPremium] ELSE 0 END) as EQQuotedRN, /* Wind, New Business, by Description */ SUM(CASE WHEN LineOfCoverage = 'Wind' AND BusinessType = 'New Business' AND Description = 'Bound' THEN [Bound Premium] ELSE 0 END) as WindBoundNB, SUM(CASE WHEN LineOfCoverage = 'Wind' AND BusinessType = 'New Business' AND Description = 'Lost' THEN [LostPremium] ELSE 0 END) as WindLostNB, SUM(CASE WHEN LineOfCoverage = 'Wind' AND BusinessType = 'New Business' AND Description = 'Not Taken Up' THEN [NotTakenUpPremium] ELSE 0 END) as WindNtUpNB, SUM(CASE WHEN LineOfCoverage = 'Wind' AND BusinessType = 'New Business' AND Description = 'Quoted' THEN [QuotedPremium] ELSE 0 END) as WindQuotedNB, /* Wind, Renewal, by Description */ SUM(CASE WHEN LineOfCoverage = 'Wind' AND BusinessType = 'Renewal' AND Description = 'Bound' THEN [Bound Premium] ELSE 0 END) as WindBoundRN, SUM(CASE WHEN LineOfCoverage = 'Wind' AND BusinessType = 'Renewal' AND Description = 'Lost' THEN [LostPremium] ELSE 0 END) as WindLostRN, SUM(CASE WHEN LineOfCoverage = 'Wind' AND BusinessType = 'Renewal' AND Description = 'Not Taken Up' THEN [NotTakenUpPremium] ELSE 0 END) as WindNtUpRN, SUM(CASE WHEN LineOfCoverage = 'Wind' AND BusinessType = 'Renewal' AND Description = 'Quoted' THEN [QuotedPremium] ELSE 0 END) as WindQuotedRN, CASE WHEN LineOfCoverage = 'Earthquake' THEN 'Earthquake' ELSE 'Wind' END as LineOfCoverage, 'Earthquake' as Earthquake, 'Wind' as Wind, BusinessType, Description, /* Test @Richards*/ SUM(CASE WHEN Description = 'Bound' THEN [Bound Premium] ELSE 0 END) as TotalBound, SUM(CASE WHEN Description = 'Lost' THEN [LostPremium] ELSE 0 END) as TotalLost, SUM(CASE WHEN Description = 'Not Taken Up' THEN [NotTakenUpPremium] ELSE 0 END) as TotalNTU, SUM(CASE WHEN Description = 'Quoted' THEN [QuotedPremium] ELSE 0 END) as TotalQuoted FROM cte_Quotes GROUP BY LineOfCoverage,BusinessType,Description 作为值,看起来这些总数会位于条形图的顶部。

enter image description here

编辑3: 为每个SUM(TotalBound)

添加了UNION ALL语句Totals
Description

SSRS中的结果:

enter image description here

1 个答案:

答案 0 :(得分:1)

正如我原来的评论中所述,向SQL中添加UNION会将摘要数据汇总到适当的级别,这使得这成为可能。

这是一种有用的技巧,可以在报表编写时使用该工具。

这是一个简单的例子(来自我很方便的数据库):

SELECT T.account_type_desc AS category, COUNT(1) AS cnt
FROM account A JOIN account_type T
ON A.account_type_key = T.account_type_key
GROUP BY T.account_type_desc

UNION ALL

SELECT 'Grand Total' AS category, COUNT(1) AS cnt
FROM account A

很高兴我能帮忙!!