You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在SSRS的同一个分组tablix中展示两个多对多关联的数据集

SSRS 同一分组Tablix展示两个多对多关联数据集的实现方案

实现方法

方案1:SQL层预关联(推荐)

该方案性能最优、出错概率最低,直接在数据查询阶段关联两张表,生成单个数据集绑定到Tablix即可,避免SSRS表达式层面的匹配开销。示例查询逻辑如下:

SELECT 
    t1.Region,
    t1.City,
    t1.Sales,
    t1.Produced,
    t2.Requested
FROM Tabelle1 t1
LEFT JOIN Tabelle2 t2 ON t1.Region = t2.Region

如果需要按Region聚合Requested值,直接添加GROUP BY聚合逻辑即可。

方案2:SSRS表达式匹配(保留双数据集场景)

如果必须保留两个独立的数据集,可以用SSRS内置的LookupSet函数实现多对多匹配,SSRS没有Vlookup函数,且普通Lookup函数仅能返回第一个匹配结果,无法满足多对多场景:

  • 如需按关联键求和所有匹配值,在Tablix对应单元格填入如下表达式:
=SUM(LookupSet(Fields!Region.Value, Fields!Region.Value, Fields!Requested.Value, "你的第二个数据集的名称"))
  • 如需列出所有匹配的Requested值,用如下表达式拼接:
=Join(LookupSet(Fields!Region.Value, Fields!Region.Value, CStr(Fields!Requested.Value), "你的第二个数据集的名称"), ", ")

注意事项

  • 关联键(此处为Region字段)的数据类型必须完全一致,匹配前可统一转成字符串类型避免匹配失败,示例:CStr(Fields!Region.Value)
  • 表达式中的第二个数据集名称必须和SSRS项目中定义的数据集名称完全匹配,区分大小写
  • 多关联键场景下可拼接多个键值匹配,示例:Fields!Region.Value & "|" & Fields!City.Value,第二个参数保持相同拼接规则即可

测试数据SQL脚本

USE [test]
GO
/****** Object:  Table [dbo].[Tabelle1]    Script Date: 14.09.2021 21:03:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Tabelle1](
    [Region] [nvarchar](50) NULL,
    [City] [nvarchar](50) NULL,
    [Sales] [int] NULL,
    [Produced] [int] NULL
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[Tabelle2]    Script Date: 14.09.2021 21:03:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Tabelle2](
    [Region] [nvarchar](50) NULL,
    [Requested] [int] NULL
) ON [PRIMARY]
GO
INSERT [dbo].[Tabelle1] ([Region], [City], [Sales], [Produced]) VALUES (N'West', N'Boston', 5000, 400)
GO
INSERT [dbo].[Tabelle1] ([Region], [City], [Sales], [Produced]) VALUES (N'West', N'Fransico', 100, 844)
GO
INSERT [dbo].[Tabelle1] ([Region], [City], [Sales], [Produced]) VALUES (N'Nord', N'New York', 1054, 5844)
GO
INSERT [dbo].[Tabelle1] ([Region], [City], [Sales], [Produced]) VALUES (N'Nord', N'Dallas', 15474, 11841)
GO
INSERT [dbo].[Tabelle1] ([Region], [City], [Sales], [Produced]) VALUES (N'Nord', N'Berlin', 1544, 44)
GO
INSERT [dbo].[Tabelle1] ([Region], [City], [Sales], [Produced]) VALUES (N'South', N'Austin', 154, 5481)
GO
INSERT [dbo].[Tabelle1] ([Region], [City], [Sales], [Produced]) VALUES (N'South', N'Birn', 1544, 4544)
GO
INSERT [dbo].[Tabelle2] ([Region], [Requested]) VALUES (N'West', 8411)
GO
INSERT [dbo].[Tabelle2] ([Region], [Requested]) VALUES (N'Nord', 1541)
GO
INSERT [dbo].[Tabelle2] ([Region], [Requested]) VALUES (N'South', 151)
GO
INSERT [dbo].[Tabelle2] ([Region], [Requested]) VALUES (N'Nord', 15444)
GO

内容的提问来源于stack exchange,提问作者MisterXAGE_

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.04 17:18:04