【问题标题】:SSRS Predicament with expressions带有表达式的 SSRS 困境
【发布时间】:2017-11-13 20:47:04
【问题描述】:

我有一个如下所示的 SSRS 数据集:

数据集行是使用 UNION ALL 相互独立生成的。 我需要在我的报告中按原样显示这些行,但我需要添加一个额外的行来计算总赢/总损失,所以结果应该如下所示:

这只是示例,因为我有更多列(每月 1 个)并且整个内容按产品细分,所以如果我有 10 个不同的产品,我将有 10 个不同的 tablix 表。

基本上,我需要以某种方式创建一个表达式,该表达式仅计算 3 行中的 2 行中的值(基于 Status 列的值),并考虑到某些值可能为零。

这是查询(为了更好地理解,我将其简化了一点):

select * from
(
select 'Created' as 'State', fo.groupidname, fo.businessidname ' Business', fo.opportunityid
from FilteredOpportunity fo
where fo.regionidname = 'Americas Region'
and fo.createdon >= dateadd(year, -1, getdate())
and fo.regionalfeeincome >= 250000
) created
pivot
(
count(created.opportunityid)
for created.groupidname in ([Boston], [Chicago], [Colombia], [Group D.C.], [Houston], [Los Angeles], [New York], [San Francisco], [Seattle], [Toronto])
) pivCreated
union all
select * from
(
select 'Won' as 'State', fo.groupidname, fo.businessidname ' Business', fo.opportunityid
from FilteredOpportunity fo
where regionidname = 'Americas Region'
and fo.actualclosedate >= dateadd(year, -1, getdate())
and regionalfeeincome >= 250000
and fo.jna is not null 
) won
pivot
(
count(won.opportunityid)
for won.groupidname in ([Boston], [Chicago], [Colombia], [Group D.C.], [Houston], [Los Angeles], [New York], [San Francisco], [Seattle], [Toronto])
) pivWon
union all
select * from
(
select 'Lost' as 'State', fo.groupidname, fo.businessidname ' Business', fo.opportunityid
from FilteredOpportunity fo
where fo.regionidname = 'Americas Region'
and fo.actualclosedate >= dateadd(year, -1, getdate())
and fo.regionalfeeincome >= 250000
and fo.sys_phasename <> 'Pre-Bid'
) lost
pivot
(
count(lost.opportunityid)
for lost.groupidname in ([Boston], [Chicago], [Colombia], [Group D.C.], [Houston], [Los Angeles], [New York], [San Francisco], [Seattle], [Toronto])
) pivLost

TIA -TS。

【问题讨论】:

  • 这可能是在 SQL 的服务器端最容易做到的。你能发布你的数据集查询吗?
  • 嗨艾伦,这有点复杂。我想不出在我的 SQL 查询中执行此操作的方法。我正在使用 PIVOT 和 UNION ALL 来加入 3 个单独的查询。
  • 嗨,托尼,我可能有点离题,但我认为你在这里把事情复杂化了。如果我是你,我会将数据透视留给 SSRS,在那里做起来要容易得多,这将大大简化你的查询。我可以发布一个关于如何执行此操作的半通用答案,但我无法从您的查询中看到月份名称的来源,也许这是您为了清楚起见而去掉的一点?无论如何,如果您可以从 FilteredOpportunity 表/视图中发布一小部分测试数据样本,那么我将添加一个答案。我认为它比你想象的要简单(希望如此!)
  • 另外,您所关注的 groupidname(城市)发生了什么,因为我在您的示例输出中没有看到它。
  • 嗨 Alan,这个月最初只是为了更容易理解。我有组(城市)而不是月份。我正在考虑在不使用 SQL 中的数据透视的情况下转储数据,然后让 SSRS 处理它,但我仍然不太清楚它是否会处理我需要它处理的比率的计算,而且在 SQL 中很难做到这一点因为我仍然会运行 3 个不同的查询并使用 UNION 来连接数据。

标签: reporting-services expression ssrs-tablix


【解决方案1】:

我无法对此进行全面测试,因为我没有时间构建示例数据,但它应该可以工作...

如果您将其用作报表的数据集查询,那么您应该能够添加一个简单的矩阵,其中包含State 的行组和City 的列组

/*
You can optimise this a bit but I've kept it fairly procedural so it's easier to follow
*/
-- First do your 3 basic queries but this time we don't pivot and we dump the results into temp tables
-- I've also removed columns that don't appear to be used based on your sample output and remaned a column to City to make it easier to read
select 'Total Created' as 'State', fo.groupidname as City, COUNT(*) AS Cnt
INTO #Created
    from FilteredOpportunity fo
    where fo.regionidname = 'Americas Region'
        and fo.createdon >= dateadd(year, -1, getdate())
        and fo.regionalfeeincome >= 250000

select 'Total Won' as 'State', fo.groupidname as City, COUNT(*) AS Cnt
INTO #Won
    from FilteredOpportunity fo
    where fo.regionidname = 'Americas Region'
        and fo.createdon >= dateadd(year, -1, getdate())
        and fo.regionalfeeincome >= 250000
        and fo.jna is not null 

select 'Total Lost' as 'State', fo.groupidname as City, COUNT(*) AS Cnt
INTO #Lost
    from FilteredOpportunity fo
    where fo.regionidname = 'Americas Region'
        and fo.createdon >= dateadd(year, -1, getdate())
        and fo.regionalfeeincome >= 250000
        and fo.sys_phasename <> 'Pre-Bid'

-- Now we calculate your Ratio
SELECT
    'Win Ratio' as 'State' 
    , coalesce(w.City, l.City) as City -- use coalesce in case 1 side of join is null
    , CASE WHEN ISNULL(l.cnt,0) = 0 THEN 0 ELSE w.Cnt/l.Cnt END as Cnt -- if lost is null or 0 return 0 else calc ratio
into #Ratio
 FROM #Won w
    full join #Lost l on w.City = l.City

-- finaly, get the results and add a sort to help the report row sorting.
SELECT 1 as Sort, * FROM #Created
UNION SELECT 2, * FROM #Won
UNION SELECT 3, * FROM #Lost
UNION SELECT 4, * FROM #Ratio

【讨论】:

  • 嗨艾伦,非常感谢您的帮助。我正在努力完成这项工作。我不知道我是否在原始帖子中提到了这一点,但我有一个额外的警告,因为我有一个额外的字段需要打破结果集。我有业务领域,每个城市/组可以有多个业务。我也遇到了#ration 临时表的问题。它为此表中的所有行计算 0 或 NULL。我会继续试验,但也许你能更快地发现一些东西?
  • 你确定你完全加入了吗?
  • 是的,我什至尝试了下面的查询。我在业务表中有 19 行,所以如果我使用 FULL JOIN,无论波士顿是否有某些业务,我都应该在此查询中获得 19 行,对吗?我刚刚得到波士顿所有业务的 10 行: select ab.name, fo.groupidname, count(*) from business ab full external join FilteredOpportunity fo on ab.businessid = fo.businessid where fo.groupidname = '波士顿'组由 ab.name, fo.groupidname
  • 嗨,托尼,我在英国,所以现在是晚上 9 点。让我们看看明天某个时候我们是否可以进入聊天室并解决这个问题。一旦一切都整理好,我们可以回来发布最终答案,而不是污染这个问题。同时,如果您可以从您的基表中编写一些测试数据,那么我们就可以使用它,这将节省一些时间,并希望也能为我澄清一些事情。
  • 嗨,艾伦,我几乎把所有东西都整理好了。问题是我没有注意和错误的数据类型。所有的行实际上都返回了,只是我希望某些行在顶部,甚至没有费心往下看页面,但它们就在那里。我还使用 CAST 来计算 Ratio,一切正常。唯一剩下的就是按城市(列)计算总数。我在 Totals 的 tablix 中添加了一个额外的行,并为每一列执行:Sum(Fields!Boston.Value) 等等。我显然想从计算中排除赢率行。我怎样才能做到这一点?
猜你喜欢
  • 1970-01-01
  • 2013-10-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多