使用 2 个不同的数据源创建报告
Create a report using 2 different data sources
我需要创建一个在不同服务器上使用 2 个不同数据集的报告。
单独 运行 时查询 运行 很好。但是我无法link他们。
查询 1:
SELECT Employee.[Person Number],
Employee.[Ethnic Origin],
COUNT(Employee.[Person Number]) AS Staf_Count
FROM Employee
GROUP BY Employee.[Person Number],
Employee.[Ethnic Origin]
查询 2:
SELECT DISTINCT y.StYear,
s.Student_ID,
s.Ethnicity,
COUNT(s.Ethnicity) AS EthCount,
CASE s.STUD_Ethnicity
WHEN 31 THEN 'White - English / Welsh / Scottish / Northern Irish / British'
WHEN 32 THEN 'White - Irish'
WHEN 33 THEN 'White - Gypsy or Irish Traveller'
WHEN 34 THEN 'White - Any Other White background'
WHEN 35 THEN 'Mixed / Multiple Ethnic group - White and Black Caribbean'
WHEN 36 THEN 'Mixed / Multiple Ethnic group - White and Black African'
WHEN 37 THEN 'Mixed / Multiple Ethnic group - White and Asian'
WHEN 38 THEN 'Mixed / Multiple Ethnic group - Any Other Mixed / multiple ethnic background'
WHEN 39 THEN 'Asian / Asian British - Indian'
WHEN 40 THEN 'Asian/ Asian British - Pakistani'
WHEN 41 THEN 'Asian / Asian British - Bangladeshi'
WHEN 42 THEN 'Asian / Asian British - Chinese'
WHEN 43 THEN 'Asian / Asian British - Any other Asian background'
WHEN 44 THEN 'Black / African / Caribbean / Black British - African'
WHEN 45 THEN 'Black / African / Caribbean / Black British - Caribbean'
WHEN 46 THEN 'Black / African / Caribbean / Black British - Any other Black / African / Caribbean background'
WHEN 47 THEN 'Other ethnic group - Arab'
WHEN 98 THEN 'Any Other'
WHEN 99 THEN 'Not provided'
END AS Ethnicity
FROM dbo.STUDstudent s
LEFT JOIN dbo.GNICodes g ON s.Ethnicity = g.GNIC_Code
INNER JOIN dbo.STYRstudentYR y ON s.Student_ID = y.Student_ID
WHERE STYR_Year = @Year
GROUP BY s.Student_ID,
s.Ethnicity
O/P:应该如下所示:
我已经检查过,第二个数据库被列为第一个数据库下的 linked 服务器。
报表设计如下所示:
创建了一个合并两个数据集的视图:
CREATE VIEW Staff_Student_Ethnicity
AS
SELECT DISTINCT
[Ethnic Origin] COLLATE SQL_Latin1_General_CP1_CI_AI AS Ethnicity,
COUNT([Person Number]) AS Staf_Count
FROM Employee.[Monitoring with Organisation As At Evaluation Date]
GROUP BY [Ethnic Origin]
UNION ALL
SELECT DISTINCT
Ethnicity COLLATE SQL_Latin1_General_CP1_CI_AI ,
COUNT(STUD_Ethnicity) AS StudCount
FROM SQL10.NG.[dbo].[Student_Ethnicity]
GROUP BY Ethnicity
但是在执行视图后,我只看到种族和 Staf_Count 字段并且 StudCount 丢失了。请告诉我哪里错了...
感谢您的示例 Aruna 中的错误和屏幕截图。我相信具有更多 SSRS 知识的人会解释为什么聚合列必须来自与您的类别轴标识符相同的来源,我只知道在许多可视化平台(如 Spotfire 和 Tableau)中都是这种情况,而且似乎也是这种情况。但是,有一个解决方法。
与其从不同的数据元素引入两个不同的数据源,不如在服务器端处理关系。由于您的服务器已经链接,因此在其中一个服务器上创建一个 STORED PROCEDURE
来组合两个数据表。您可以更轻松地控制数据与预期结果的关系。然后,将其用作 SSRS 报告的数据源。
CREATE VIEW Staff_Student_Ethnicity
AS
SELECT DISTINCT
[Ethnic Origin] COLLATE SQL_Latin1_General_CP1_CI_AI AS Ethnicity,
COUNT([Person Number]) AS Staf_Count,
NULL as StudCount
FROM Employee.[Monitoring with Organisation As At Evaluation Date]
GROUP BY [Ethnic Origin]
UNION ALL
SELECT DISTINCT
Ethnicity COLLATE SQL_Latin1_General_CP1_CI_AI ,
NULL as Staf_Count,
COUNT(STUD_Ethnicity) AS StudCount
FROM SQL10.NG.[dbo].[Student_Ethnicity]
GROUP BY Ethnicity
编辑以进行向下钻取
如果您想获得聚合的详细信息以在 SSRS 中构建分层报告,那么您将不得不从查询中引入这些详细信息。因此,您最好在 SSRS 中为您的计数进行聚合,而不是进行两次查询。如下所示的一个简单程序应该可以为您合并数据。然后,您可以将其带入 SSRS 并根据需要聚合列。我放入了一个 PersonType
字段供您在 SSRS 中的 IF
语句中使用。即 IF([PersonType] = 'Student' then Count([PersonNumber])) as [StudentCount]
。我知道这种语法对于 SSRS 来说并不准确,但我只是给出了一个逻辑示例。
CREATE PROCEDURE usp_staff_student_sthnicity()
AS
SELECT DISTINCT
[Ethnic Origin] COLLATE SQL_Latin1_General_CP1_CI_AI AS Ethnicity,
[Person Number] AS PersonNumber,
'Staff' as PersonType
FROM Employee.[Monitoring with Organisation As At Evaluation Date]
UNION ALL
SELECT DISTINCT
Ethnicity COLLATE SQL_Latin1_General_CP1_CI_AI,
Student_ID AS PersonNumber
'Student' as PersonType
FROM SQL10.NG.[dbo].[Student_Ethnicity]
然后,您可以让用户单击 StudentCount
或 EmployeeCount
字段并查看与此聚合关联的学生/员工。不过,关于如何执行此操作的步骤取决于研究后 Whosebug 上的一个新问题 post。
您可以使用 Lookup 函数从第二个数据集中获取学生人数。在这种情况下,种族将是您的关键。所以表达式将是:
=Lookup(Fields!Ethnic_Origin.Value, Fields!Ethnicity.Value, Fields!EthCount.Value, "StudentEthnicity")
我需要创建一个在不同服务器上使用 2 个不同数据集的报告。
单独 运行 时查询 运行 很好。但是我无法link他们。
查询 1:
SELECT Employee.[Person Number],
Employee.[Ethnic Origin],
COUNT(Employee.[Person Number]) AS Staf_Count
FROM Employee
GROUP BY Employee.[Person Number],
Employee.[Ethnic Origin]
查询 2:
SELECT DISTINCT y.StYear,
s.Student_ID,
s.Ethnicity,
COUNT(s.Ethnicity) AS EthCount,
CASE s.STUD_Ethnicity
WHEN 31 THEN 'White - English / Welsh / Scottish / Northern Irish / British'
WHEN 32 THEN 'White - Irish'
WHEN 33 THEN 'White - Gypsy or Irish Traveller'
WHEN 34 THEN 'White - Any Other White background'
WHEN 35 THEN 'Mixed / Multiple Ethnic group - White and Black Caribbean'
WHEN 36 THEN 'Mixed / Multiple Ethnic group - White and Black African'
WHEN 37 THEN 'Mixed / Multiple Ethnic group - White and Asian'
WHEN 38 THEN 'Mixed / Multiple Ethnic group - Any Other Mixed / multiple ethnic background'
WHEN 39 THEN 'Asian / Asian British - Indian'
WHEN 40 THEN 'Asian/ Asian British - Pakistani'
WHEN 41 THEN 'Asian / Asian British - Bangladeshi'
WHEN 42 THEN 'Asian / Asian British - Chinese'
WHEN 43 THEN 'Asian / Asian British - Any other Asian background'
WHEN 44 THEN 'Black / African / Caribbean / Black British - African'
WHEN 45 THEN 'Black / African / Caribbean / Black British - Caribbean'
WHEN 46 THEN 'Black / African / Caribbean / Black British - Any other Black / African / Caribbean background'
WHEN 47 THEN 'Other ethnic group - Arab'
WHEN 98 THEN 'Any Other'
WHEN 99 THEN 'Not provided'
END AS Ethnicity
FROM dbo.STUDstudent s
LEFT JOIN dbo.GNICodes g ON s.Ethnicity = g.GNIC_Code
INNER JOIN dbo.STYRstudentYR y ON s.Student_ID = y.Student_ID
WHERE STYR_Year = @Year
GROUP BY s.Student_ID,
s.Ethnicity
O/P:应该如下所示:
我已经检查过,第二个数据库被列为第一个数据库下的 linked 服务器。
报表设计如下所示:
创建了一个合并两个数据集的视图:
CREATE VIEW Staff_Student_Ethnicity
AS
SELECT DISTINCT
[Ethnic Origin] COLLATE SQL_Latin1_General_CP1_CI_AI AS Ethnicity,
COUNT([Person Number]) AS Staf_Count
FROM Employee.[Monitoring with Organisation As At Evaluation Date]
GROUP BY [Ethnic Origin]
UNION ALL
SELECT DISTINCT
Ethnicity COLLATE SQL_Latin1_General_CP1_CI_AI ,
COUNT(STUD_Ethnicity) AS StudCount
FROM SQL10.NG.[dbo].[Student_Ethnicity]
GROUP BY Ethnicity
但是在执行视图后,我只看到种族和 Staf_Count 字段并且 StudCount 丢失了。请告诉我哪里错了...
感谢您的示例 Aruna 中的错误和屏幕截图。我相信具有更多 SSRS 知识的人会解释为什么聚合列必须来自与您的类别轴标识符相同的来源,我只知道在许多可视化平台(如 Spotfire 和 Tableau)中都是这种情况,而且似乎也是这种情况。但是,有一个解决方法。
与其从不同的数据元素引入两个不同的数据源,不如在服务器端处理关系。由于您的服务器已经链接,因此在其中一个服务器上创建一个 STORED PROCEDURE
来组合两个数据表。您可以更轻松地控制数据与预期结果的关系。然后,将其用作 SSRS 报告的数据源。
CREATE VIEW Staff_Student_Ethnicity
AS
SELECT DISTINCT
[Ethnic Origin] COLLATE SQL_Latin1_General_CP1_CI_AI AS Ethnicity,
COUNT([Person Number]) AS Staf_Count,
NULL as StudCount
FROM Employee.[Monitoring with Organisation As At Evaluation Date]
GROUP BY [Ethnic Origin]
UNION ALL
SELECT DISTINCT
Ethnicity COLLATE SQL_Latin1_General_CP1_CI_AI ,
NULL as Staf_Count,
COUNT(STUD_Ethnicity) AS StudCount
FROM SQL10.NG.[dbo].[Student_Ethnicity]
GROUP BY Ethnicity
编辑以进行向下钻取
如果您想获得聚合的详细信息以在 SSRS 中构建分层报告,那么您将不得不从查询中引入这些详细信息。因此,您最好在 SSRS 中为您的计数进行聚合,而不是进行两次查询。如下所示的一个简单程序应该可以为您合并数据。然后,您可以将其带入 SSRS 并根据需要聚合列。我放入了一个 PersonType
字段供您在 SSRS 中的 IF
语句中使用。即 IF([PersonType] = 'Student' then Count([PersonNumber])) as [StudentCount]
。我知道这种语法对于 SSRS 来说并不准确,但我只是给出了一个逻辑示例。
CREATE PROCEDURE usp_staff_student_sthnicity()
AS
SELECT DISTINCT
[Ethnic Origin] COLLATE SQL_Latin1_General_CP1_CI_AI AS Ethnicity,
[Person Number] AS PersonNumber,
'Staff' as PersonType
FROM Employee.[Monitoring with Organisation As At Evaluation Date]
UNION ALL
SELECT DISTINCT
Ethnicity COLLATE SQL_Latin1_General_CP1_CI_AI,
Student_ID AS PersonNumber
'Student' as PersonType
FROM SQL10.NG.[dbo].[Student_Ethnicity]
然后,您可以让用户单击 StudentCount
或 EmployeeCount
字段并查看与此聚合关联的学生/员工。不过,关于如何执行此操作的步骤取决于研究后 Whosebug 上的一个新问题 post。
您可以使用 Lookup 函数从第二个数据集中获取学生人数。在这种情况下,种族将是您的关键。所以表达式将是:
=Lookup(Fields!Ethnic_Origin.Value, Fields!Ethnicity.Value, Fields!EthCount.Value, "StudentEthnicity")