0

我为每条记录都有一个数据集,它有一个 CompanyID、RevenueMonth、RevenueYear、Revenue

创建报告时,我将每个 CompanyID 分组并显示给定年份的月收入。但在特定年份,并非所有公司在特定月份都有任何收入。

例子:

示例记录如下所示:

CompanyID、RevenueMonth、RevenueYear、Revenue
1,05,2013,5.00
1,08,2013,6.00
1,03,2013,3.00

最终结果,我希望我的报告看起来像这样,CompanyID 为 1。

公司编号|01|02|03|04|05|06|07|08|09|10|11|12
1 0.00|0.00|3.00|0.00|5.00|0.00|0.00|6.00|0.00|0.00|0.00|0.00

在我当前的报告中,它只会用 March (03)、May (05) 和 August (08) 填充列标题。

公司编号|03|05|08
1 3.00|5.00|6.00

如何让我的报告添加一年中缺少的月份?

我希望我的问题很清楚。

4

1 回答 1

2

数据库级别

由于您一次只返回一年,您可以创建一个日历表并将其添加到您的结果集中:

尽可能简单,日期表来自 CTE:

with months as -- get required year/months
(
  select RevenueYear = 2013
    , RevenueMonth = 1
  union all
  select RevenueYear = 2013
    , RevenueMonth = RevenueMonth + 1
  from months
  where RevenueMonth < 12
)
select CompanyID = coalesce(r.CompanyID, c.companyID)
  , RevenueMonth = coalesce(r.RevenueMonth, m.RevenueMonth)
  , RevenueYear = coalesce(r.RevenueYear, m.RevenueYear)
  , Revenue = isnull(r.Revenue, 0.0)
from months m
  cross join (select distinct CompanyID from records) c -- make sure all companies included
  left join records r on m.RevenueYear = r.RevenueYear
    and m.RevenueMonth = r.RevenueMonth

SQL Fiddle 与演示

这将为结果集中的每个公司返回年/月。

从长远来看,最好从 CTE 移动到数据库中的永久日历表。

然后,您可以使用矩阵样式的 tablix 在报告中实现这一点。

报告级别

如果您希望在报告级别执行此操作,您可以设置一个包含 12 个永久列的表格样式的 tablix,每个月一个,然后使用以下表达式填充月份收入单元格:

=Sum(IIf(Fields!RevenueMonth.Value = 2, Fields!Revenue.Value, Nothing)

对于二月专栏。

这将适用于您现有的数据集,而无需更改任何数据库代码。

于 2013-08-19T22:26:04.630 回答