SQL Server SSRS查询问题

时间:2018-10-15 17:55:08

标签: sql-server reporting-services

我有以下查询:

SELECT        
    pupils.txtSchoolID, pupils.txtPreName, pupils.txtSurname,
    pupils.txtForm, pupils.txtAdditionalHealth, pupils.txtAllergyNotes,
    notes.txtNote
FROM 
    TblPupilManagementPupils AS pupils 
LEFT OUTER JOIN
    TblPupilManagementHealthNotes AS notes ON pupils.txtSchoolID = notes.txtSchoolID

我只需要在notes.txtNote时拉出notes.txtType = 'Dietary',但是,当我在WHERE子句中进行设置时,我只获得Diet类型的条目,我仍然需要所有学生的表数据也。我以为左外部联接可以做到这一点。

设置WHERE子句时,我会尝试

WHERE notes.txtType = 'Dietary' 

但这还是行不通的,我已经暂时设置为参数。

3 个答案:

答案 0 :(得分:0)

使用notes.txtType = 'Dietary'子句放入条件(ON):

. . . 
FROM TblPupilManagementPupils AS pupils LEFT OUTER JOIN
     TblPupilManagementHealthNotes AS notes 
     ON pupils.txtSchoolID = notes.txtSchoolID AND notes.txtType = 'Dietary'; 

如果使用LEFT OUTER JOINED子句过滤WHERE表数据,那么您将以INNER JOIN而不是LEFT JOIN的身份进行联接。

答案 1 :(得分:0)

您必须删除where子句并将该条件放在子句上,因为

逻辑上,所有的JOIN都使用ON过滤器作为INNER JOIN执行,然后,作为后续步骤,将所有OUTER JOIN行添加回必要的一侧。完成所有JOIN后,将处理WHERE筛选器。结果是,将其应用到删除了所有预期Dietary值记录的地方

SELECT        pupils.txtSchoolID, pupils.txtPreName, pupils.txtSurname,
pupils.txtForm, pupils.txtAdditionalHealth, pupils.txtAllergyNotes,
notes.txtNote

FROM TblPupilManagementPupils AS pupils LEFT OUTER JOIN
TblPupilManagementHealthNotes AS notes
ON pupils.txtSchoolID = notes.txtSchoolID and notes.txtType = 'Dietary'

答案 2 :(得分:0)

或者,优化路线较少;使用no子句和其他on子句按原样运行查询。将数据插入到临时表中,然后按原样过滤该数据;其中notes.txtType = @Param。而此@Param则将存储过程或查询的顶部声明为@Param varchar(200),以捕获您期望的任何期望的前端值。