Tsql 如果给定期间内没有数据,则向报表添加列标题

Tsql 如果给定期间内没有数据,则向报表添加列标题,tsql,reporting-services,Tsql,Reporting Services,我有一个数据集,每个记录都有一个公司ID,RevenueMonth,RevenueYear,Revenue 当我创建报告时,我对每个公司ID进行分组,并显示它们在给定年份的每月收入。 但在某一年,并非所有公司都有某个月的收入 例如: 示例记录如下所示: CompanyID, RevenueMonth, RevenueYear, Revenue 1,05,2013,5.00 1,08,2013,6.00 1,03,2013,3.00 公司ID,收入月,收入月,收入 1,05,2013,5.00 1

我有一个数据集,每个记录都有一个公司ID,RevenueMonth,RevenueYear,Revenue

当我创建报告时,我对每个公司ID进行分组,并显示它们在给定年份的每月收入。 但在某一年,并非所有公司都有某个月的收入

例如:

示例记录如下所示:

CompanyID, RevenueMonth, RevenueYear, Revenue 1,05,2013,5.00 1,08,2013,6.00 1,03,2013,3.00 公司ID,收入月,收入月,收入 1,05,2013,5.00 1,08,2013,6.00 1,03,2013,3.00 最终结果,我希望我的报告与CompanyID 1的报告类似

Company ID|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 公司编号| 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 在我当前的报告中,它只会用3月(03)、5月(05)和8月(08)填充列标题

公司编号| 03 | 05 | 08 1 3.00|5.00|6.00 如何让我的报告添加一年中缺失的月份


我希望我的问题是清楚的

数据库级

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

尽可能简单,日期表来自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
二月专栏


这将在不更改任何数据库代码的情况下使用现有数据集。

尝试创建一个月表以加入。你也可以试试@MrCleanX,谢谢你的回复,但是从你发布的链接来看,我对如何实现它有点困惑。今晚我会再看一遍。如果你能提供另一个例子,那就太好了。谢谢,非常感谢。我使用了报告级别选项。这一切都很顺利。当我在数据库级别也需要它时,我会把它藏起来。我感谢你提供两种方式。做得好。
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
=Sum(IIf(Fields!RevenueMonth.Value = 2, Fields!Revenue.Value, Nothing)