sql server - 在没有数据的情况下向查询结果集添加行

时间:2014-11-12 19:58:38

标签: sql-server

我有一个查询,它返回一个学年的给定实例中的测试的平均值和计数,我需要在结果集中包含某个学年的测试实例。 #39; t数据。我有下面的查询,结果集,所需的结果和传递的变量。

SELECT  y.SchoolYearDescription, c.TestPeriodTitle, ti.pkTestInstanceID, ti.fkTestPeriodID AS 'fkTestPeriodID', ISNULL(SUM(a.AverageScore * Count),0) / ISNULL(SUM(a.Count),0) /*AverageScore*/ AS 'AvgScaledScore', ISNULL(SUM(Count),0) /*Count*/ AS 'Count_Include'
    FROM RosterSummaryData_Subject_Local_Averages a
    INNER JOIN RosterSummaryData_Subject_Local g ON g.pkSummarySubjectLocalID = a.fkSummarySubjectLocalID
    RIGHT JOIN MM_SchoolYears y ON g.fkSchoolYearID = y.pkSchoolYearID
    INNER JOIN itot(@strYearIds, N',') tblYearIds ON y.pkSchoolYearID = tblYearIds.number
    INNER JOIN TestInstances ti ON ti.pkTestInstanceID = g.fkTestInstanceID
    INNER JOIN itot(@strTestInstIds, N',') tblTestInstIds ON ti.pkTestInstanceID = tblTestInstIds.number
    INNER JOIN CAHSEE_TestPeriods c ON c.pkTestPeriodID = ti.fkTestPeriodID
    WHERE g.fkRosterSetID = @intRosterSetId
        AND g.fkTestTypeID = @intTestTypeId
        AND g.fkTest_SubjectID = @intSubId
        AND a.fkScoreTypeID = @intScoreTypeId
        AND g.fkSchoolID = @intSchoolId
        AND g.fkGradeID = @intGradeId
        AND g.fkDepartmentID = @intDeptId
        AND g.fkCourseID = @intCourseId
        AND g.fkPeriodID = @intPeriodId 
        AND g.fkDemoCommonCodeID IN (SELECT number FROM itot(@strDemoCodeIds, N','))
    GROUP BY y.SchoolYearDescription, ti.pkTestInstanceID, c.TestPeriodTitle, ti.fkTestPeriodID
    ORDER BY y.SchoolYearDescription, ti.pkTestInstanceID, c.TestPeriodTitle, ti.fkTestPeriodID

返回的结果如下所示。 2012 - 2013年没有数据,因此未包含在结果中。

SchoolYearDescription   TestPeriodTitle   pkTestInstanceID   fkTestPeriodID  AvgScaledScore   Count_Include
2013-2014               2013 Fall         296                417             163              26
2014-2015               2014 Fall         300                421             169              34

但我希望结果包括2012 - 2013秋季测试管理,没有数据,并用这样的零填充得分和计数列:

SchoolYearDescription   TestPeriodTitle   pkTestInstanceID   fkTestPeriodID  AvgScaledScore   Count_Include
2012-2013               2012 Fall         292                413             0                0 
2013-2014               2013 Fall         296                417             163              26
2014-2015               2014 Fall         300                421             169              34

我将这些变量传递给查询(存储过程)

    @strYearIds = N'13,14,15',
    @strDemoCodeIds = N'0',
    @intRosterSetId = 0,
    @intSchoolId = 0,
    @intTeachId = 0,
    @intGradeId = 99,
    @intDeptId = 0,
    @intCourseId = 0,
    @intPeriodId = 0,
    @intSubId = 1139,
    @strTestInstId = N'292,296,300',
    @intTestTypeId = 25,
    @intPerfLevelReportId = 25,
    @bitIsStrand = 0

这种事情有可能完成还是相当复杂?如果需要更多信息来回答这个问题,请告诉我。

1 个答案:

答案 0 :(得分:0)

您似乎从TestInstances表中获取了核心数据,因此您希望从该表中获取行,而不管它们是否链接到您的Averages表。我认为这会奏效:

SELECT  y.SchoolYearDescription, c.TestPeriodTitle, ti.pkTestInstanceID, ti.fkTestPeriodID AS 'fkTestPeriodID', ISNULL(SUM(a.AverageScore * Count),0) / ISNULL(SUM(a.Count),0) /*AverageScore*/ AS 'AvgScaledScore', ISNULL(SUM(Count),0) /*Count*/ AS 'Count_Include'
    FROM RosterSummaryData_Subject_Local_Averages a
    RIGHT JOIN RosterSummaryData_Subject_Local g ON g.pkSummarySubjectLocalID = a.fkSummarySubjectLocalID
    RIGHT JOIN MM_SchoolYears y ON g.fkSchoolYearID = y.pkSchoolYearID
    LEFT JOIN itot(@strYearIds, N',') tblYearIds ON y.pkSchoolYearID = tblYearIds.number
    RIGHT JOIN TestInstances ti ON ti.pkTestInstanceID = g.fkTestInstanceID
    LEFT JOIN itot(@strTestInstIds, N',') tblTestInstIds ON ti.pkTestInstanceID = tblTestInstIds.number
    LEFT JOIN CAHSEE_TestPeriods c ON c.pkTestPeriodID = ti.fkTestPeriodID
    WHERE g.fkRosterSetID = @intRosterSetId
        AND g.fkTestTypeID = @intTestTypeId
        AND g.fkTest_SubjectID = @intSubId
        AND a.fkScoreTypeID = @intScoreTypeId
        AND g.fkSchoolID = @intSchoolId
        AND g.fkGradeID = @intGradeId
        AND g.fkDepartmentID = @intDeptId
        AND g.fkCourseID = @intCourseId
        AND g.fkPeriodID = @intPeriodId 
        AND g.fkDemoCommonCodeID IN (SELECT number FROM itot(@strDemoCodeIds, N','))
    GROUP BY y.SchoolYearDescription, ti.pkTestInstanceID, c.TestPeriodTitle, ti.fkTestPeriodID
    ORDER BY y.SchoolYearDescription, ti.pkTestInstanceID, c.TestPeriodTitle, ti.fkTestPeriodID