带有表达式的 SSRS 困境

SSRS Predicament with expressions

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

数据集行是使用 UNION ALL 彼此独立生成的。 我需要在我的报告中按原样显示这些行,但我需要添加一个额外的行来计算 Total Won / Total Lost,因此结果应该如下所示:

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

基本上我需要以某种方式创建一个表达式,该表达式将只计算 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.

我没法完全测试它,因为我没有时间构建示例数据,但它应该可以工作...

如果您将其用作报告的数据集查询,那么您应该能够添加一个简单的矩阵,其中行组按 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