我试图在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? (并且,通过扩展,我希望这些单元格具有替代颜色)
答案 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;分组强>
=IIF(String.IsNullOrEmpty(Fields!Points.Value),"0",Fields!Points.Value)
=IIF(Fields!Points.Value="X","Red",0)
<强>结果强>
答案 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;