在MS SQL查询结果中包含null

时间:2016-01-04 23:58:01

标签: sql sql-server

我是SQL新手,但需要更新此查询以包含' NULL'以下字段的结果:

DrCity
DrST
PhoneOffice 
SmnrDate
SmnrID

我需要做什么才能获得“空白”'这些领域的结果。现在,查询至少将SmnrDate和SmnrID排除在NULL结果之外。

谢谢。

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[Report_ActiveClients_ByConsultant] 
     @EmpID varchar(10)
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
    SELECT 
           EmpID
         , LastName
         , FirstName
         , DrCity
         , DrST
         , PhoneOffice
         , [Status] 
         , Seminar.SmnrName
         , CONVERT(varchar(10), Smnr.SmnrDate, 101) AS SeminarDate
    FROM                  (SELECT EmpID
                                , Contact.ContactID
                                , LastName
                                , FirstName
                                , DrCity
                                , DrST
                                , PhoneOffice
                                , [Status]
                                , MAX(SmnrDate) AS SmnrDate 
                                FROM Consultant INNER JOIN Contact ON Consultant.EmpID = Contact.Consultant1
                                                INNER JOIN ContactNameLink ON Contact.ContactID = ContactNameLink.lnkContactID
                                                INNER JOIN ContactName ON ContactNameLink.lnkNameID = ContactName.NameID 
                                                LEFT JOIN SmnrAttnDetail ON Contact.ContactID = SmnrAttnDetail.ContactID
                                                LEFT JOIN Seminar ON SmnrAttnDetail.SmnrID = Seminar.SmnrID
                                WHERE  ((@EmpID = '' AND Consultant.Active='1') OR Consultant.EmpID = @EmpID)
                                        AND Contact.Active='Yes'
                                        AND Contact.ContactType='Client'
                                        AND (Contact.[Status] = 'ACS' 
                                        OR Contact.[Status] =  'AC'
                                        OR Contact.[Status] =  'BC'
                                        OR Contact.[Status] =  'QS'
                                        OR Contact.[Status] =  'SAT'
                                        OR Contact.[Status] = 'LC'
                                        OR Contact.[Status] = 'AS')
                                        AND ContactNameLink.[Primary] = 1

                                GROUP BY EmpID, Contact.ContactID, LastName, FirstName, DrCity, DrST, PhoneOffice, [Status]) AS Smnr
                LEFT JOIN SmnrAttnDetail ON SmnrAttnDetail.ContactID = Smnr.ContactID
                INNER JOIN Seminar ON SmnrAttnDetail.SmnrID = Seminar.SmnrID AND Seminar.SmnrDate = Smnr.SmnrDate

    GROUP BY EmpID, Smnr.ContactID, LastName, FirstName, DrCity, DrST, PhoneOffice, [Status], Seminar.SmnrName, Smnr.SmnrDate
    ORDER BY EmpID, LastName

END

最终代码:

SELECT     dbo.StatusCode.Code, dbo.Contact.ContactType, dbo.Contact.Active, dbo.Contact.Status, dbo.ContactName.Title, dbo.ContactName.FirstName, 
                      dbo.ContactName.LastName, dbo.Contact.Addr1, dbo.Contact.City AS DrCity, dbo.Contact.ST AS DrST, dbo.Contact.PhoneClinic AS PhoneOffice, dbo.Contact.DateJoined, 
                      dbo.Contact.Consultant1 AS EmpID, ISNULL(CONVERT(varchar(10), dbo.qyLastSmnrActiveReport.LastSmnr, 101),'None') AS SeminarDate, dbo.qyLastSmnrActiveReport.SmnrName
FROM         dbo.Contact INNER JOIN
                      dbo.StatusCode ON dbo.Contact.Status = dbo.StatusCode.Code INNER JOIN
                      dbo.ContactNameLink ON dbo.Contact.ContactID = dbo.ContactNameLink.lnkContactID INNER JOIN
                      dbo.Consultant ON Contact.Consultant1 = Consultant.EmpID INNER JOIN
                      dbo.ContactName ON dbo.ContactNameLink.lnkNameID = dbo.ContactName.NameID LEFT OUTER JOIN
                      dbo.qyLastSmnrActiveReport ON dbo.Contact.ContactID = dbo.qyLastSmnrActiveReport.ContactID
WHERE     (dbo.Contact.Active = N'Yes') AND (dbo.Contact.Status IN (N'AC', N'QS', N'BC', N'LS', N'ACS', N'SAT', N'ACS', N'AS')) AND (dbo.Contact.ContactType = N'CLIENT') AND 
                      (dbo.ContactNameLink.[Primary] = 1) AND ((@EmpID = '' AND dbo.Consultant.Active='1') OR Consultant1 = @EmpID)
                      ORDER BY EmpID, LastName, FirstName

1 个答案:

答案 0 :(得分:0)

替换

INNER JOIN Seminar

LEFT JOIN Seminar