SQL查询为RDLC报告创建视图

时间:2018-08-18 20:23:48

标签: sql sql-server reporting-services rdlc sql-server-data-tools

我有这种格式的报告

report format in two sections-Objectives measures feeds from the Objective table while the job role competencies feeds from the competence table

“目标”表中要传递给报告的列是

ObjectiveID, GoalDescription, Objective, Measure, Expectation, Comment

反对

S/N, Departmental Objective/Initiative, Employee Tasks, Performance Measure, 
Performance Expectation,  Comments

分别为列。

以下是“目标”表的数据:

Data for Objective table

对于“职位角色能力”部分,这些列是

CompetenceID, CompetenceAreaDetailsDescription, CompetenceAreaDetailsExpectation

反对

S/N, Behaviours/Skills/Expectation, Expectations

分别为列。

以下是“能力”表的数据:

data for Competence table

Objective表中的记录总数为9。这是一对多的列,即被评估者可以有许多目标。 Competence表中的记录总数为13,也是一对多的表,即被评估者可以具有许多能力。

两个表中唯一匹配的数据是AppraiseeId

我的问题:如何创建一个视图以连接这两个表,以便用作报表的数据源。

为了进一步说明,我想显示Objective表中的6条记录,其中AppraiseeId = 6在“目标,任务,度量和目标”部分中,因为它已分配了6个目标并显示能力表中“职位角色能力”部分中仅分配给他/她的3个能力。

我编写和重写了很多查询,但只导致许多重复的记录。

CREATE VIEW [dbo].PlanningReport
AS
    SELECT
        "SpecificObjective"."ObjectiveId" "ObjectiveID",
        "SpecificObjective"."AppraiseeId" "AppraiseeID",
        "SpecificObjective"."Objective" "Objective",
        "SpecificObjective"."Expectation" "Expectation",
        "SpecificObjective"."Measure" "Measure",
        "SpecificObjective"."Comment" "Comment",
        "Competence"."CompetenceAreaDetailsDescription",
        "CompetenceAreaDetailsDescription",
        "Competence"."CompetenceAreaDetailsExpectation",
        "CompetenceAreaDetailsExpectation",
        "Competence"."ID" "CompetenceID",
        "Competence"."AppraiseeId" "CompetenceAppraiseId",
    FROM
        "dbo"."SpecificObjective" "SpecificObjective"
    LEFT JOIN 
        "dbo"."Employees" "Appraisee" ON "SpecificObjective"."AppraiseeId" = "Appraisee"."UserId"
    INNER JOIN
        "dbo"."Competence" "Competence" ON "SpecificObjective".AppraiseeId = "Competence".AppraiseeId
GO

我被困住了。请就该怎么做提供建议。

2 个答案:

答案 0 :(得分:1)

如果我是我,我不会将“ SpecificObjective”加入“ Competence”。
如果“ AppraiseeId”是它们之间唯一的公共字段?
然后,这是一个多对多关系,并且产生的笛卡尔联接效果不是那么有用。

最好只创建2个视图。
因为报告中的两个部分似乎是分开的。

CREATE VIEW [dbo].[vwUserSpecificObjectives] AS
SELECT
 obj.ObjectiveId AS ObjectiveID
,obj.AppraiseeId AS AppraiseeID
,obj.Objective
,obj.Expectation
,obj.Measure
,obj.Comment
FROM dbo.SpecificObjective AS obj;
GO

CREATE VIEW [dbo].[vwUserCompetences] AS
SELECT
 comp.CompetenceAreaDetailsDescription
,comp.CompetenceAreaDetailsExpectation
,comp.ID AS CompetenceID
,comp.AppraiseeId AS CompetenceAppraiseId
FROM dbo.Competence AS comp;
GO

如果您的工具仅支持一个表/视图数据源,那将无济于事。
但是,如果您的工具可以从XML中获取数据,那么您可以选择“目标和能力”作为XML。

您可以在 db <> fiddle here

上对其进行测试

示例:

CREATE VIEW [dbo].[PlanningReport]
AS
SELECT 
 emp.ID AS EmpID,
 emp.FullName AS EmpFullName,
 (
       SELECT
         obj.ObjectiveId AS "@ID",
         obj.Objective AS "Objective",
         obj.Expectation AS "Expectation",
         obj.Measure AS "Measure",
         obj.Comment AS "Comment"
        FROM dbo.SpecificObjective AS obj
        WHERE obj.AppraiseeId = emp.ID
        FOR XML PATH('Objective'), ROOT('Objectives'), TYPE
 ) AS Objectives,
 (
        SELECT
        comp.ID AS "@ID",
        comp.CompetenceAreaDetailsDescription,
        comp.CompetenceAreaDetailsExpectation
        FROM dbo.Competence AS comp
        WHERE comp.AppraiseeId = emp.ID
        FOR XML PATH('Competence'), ROOT('Competences'), TYPE
 ) AS Competences
FROM dbo.Employee emp;

答案 1 :(得分:1)

我会做一个UNION,如下所示。然后,您可以过滤AppraiseeId上的视图,并为报告的每个部分(按RowTypeId区分)选择所需的列。

CREATE Report_View

AS

Select 

ObjectiveId, 
AppraiseeId,
1 AS RowTypeId,
Description, 
Objective,
Expectation,
Measure,
Comment

FROM Objective


UNION ALL

SELECT
NULL AS ObjectiveId,
AppraiseeId,
2 AS RowTypeId,
CompetenceAreaDetailsDescription AS Description
NULL AS Objective
CompetenceAreaDetailsExpectation AS Expectation
NULL AS Measure
NULL AS Comment

FROM Competence