行和列相同时的颜色单元格

时间:2016-04-13 07:52:45

标签: sql-server reporting-services ssrs-2008 ssrs-2012

我试图在Reporting Services / SSRS中创建一个矩阵,类似于通常用于比赛的表格。

我的表看起来有点:

Home Points Away
NL    3     DE  
DE    2     FR  
FR    5     NL  
NL    3     DE 
DE    4     FR 
FR    6     NL 

我希望我的矩阵看起来像这样:

         Away
       NL DE  FR
     NL X  6   0
Home DE 0  X   6
     FR 11 0   X

其中每个值都是给定组合的个别数字的总和

NL - > DE = 3 + 3,

DE - > FR = 2 + 4,

FR - > NL = 5 + 6

如何获得列和行相等的X-es? (并且,通过扩展,我希望这些单元格具有替代颜色)

2 个答案:

答案 0 :(得分:1)

<强>数据集

declare @tb as table (Home Varchar(5), Away Varchar(5), Points Varchar(5))

insert into @tb 
select 'NL','NL','X' union
select 'NL','DE','3' union
select 'NL','FR','5' union
select 'DE','DE','X' union
select 'FR','FR','X' union
select 'DE','FR','2'

select * from @tb

SSRS表设计&amp;分组

enter image description here

  • 按行分组
  • Away的列组
  • 表达:=IIF(String.IsNullOrEmpty(Fields!Points.Value),"0",Fields!Points.Value)
  • 右键单击表达式文本框 - &gt;属性 - &gt;背景颜色 - &gt;输入表达式:=IIF(Fields!Points.Value="X","Red",0)

<强>结果

enter image description here

答案 1 :(得分:0)

我认为你不能用SSRS矩阵做你想要的事情(或者至少我不能)。我最终创建了一个数据透视表。

DECLARE @columns NVARCHAR(MAX), @sql NVARCHAR(MAX), @PrettyColumns NVARCHAR(MAX);
SET @columns = N'';
SET @PrettyColumns = N'';


SELECT @columns += N', p.' + QUOTENAME(Home)
  FROM (SELECT p.Home FROM dbo.Table_1 AS p
  GROUP BY p.Home, p.Away) AS x;

SELECT @PrettyColumns += N', CASE WHEN (p.Home = ''' + Home+''') THEN ''X'' ELSE convert(varchar(max), p.' + QUOTENAME(Home) + ') END as ' + Home
  FROM (SELECT p.Home FROM dbo.Table_1 AS p
  GROUP BY p.Home, p.Away) AS x;  


SET @sql = N'
SELECT p.Home, ' + STUFF(@PrettyColumns, 1, 2, '') + '
FROM
(
  SELECT p.Home, p.Points, p.Away
   FROM dbo.Table_1 AS p
) AS j
PIVOT
(
  SUM(Points) FOR Away IN ('
  + STUFF(REPLACE(@columns, ', p.[', ',['), 1, 1, '')
  + ')
) AS p;';
PRINT @sql;
EXEC sp_executesql @sql;