使用2个不同的数据源创建报告

时间:2016-06-01 15:17:49

标签: sql sql-server ssrs-2008 linked-server

我需要创建一个在不同服务器上使用2个不同数据集的报告。

单独运行时查询运行正常。但我无法将它们联系起来。

查询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]

QUERY2:

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:应如下所示:

Ethnicity Table

我已经检查过,第二个数据库在第一个数据库下被列为链接服务器。

报告设计如下:

Report_Design

创建了一个结合两个数据集的视图:

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

但是在执行View之后,我只看到了Ethnicity和Staf_Count字段,并且缺少了StudCount ..请告诉我哪里出错了...

2 个答案:

答案 0 :(得分:1)

感谢您的示例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]

然后,您可以让用户单击StudentCountEmployeeCount字段,并查看与此聚合关联的学生/员工。有关如何执行此操作的步骤,请参阅研究后的StackOverflow上的新问题。

答案 1 :(得分:1)

您可以使用查找功能从第二个数据集中获取学生数。在这种情况下,种族将是你的关键。所以表达式是:

=Lookup(Fields!Ethnic_Origin.Value, Fields!Ethnicity.Value, Fields!EthCount.Value, "StudentEthnicity")

资源:https://msdn.microsoft.com/en-us/library/ee210531.aspx

相关问题