This is the SQL query I am trying to execute :
SELECT
VisitID,
VisitDate,
IssuedBy,
VisitPurpose,
CompanyName,
SPname as 'SalePerson',
[1] as 'Person 1',
[2] as 'Person 2',
[3] as 'Person 3'
FROM (
SELECT
TD.VisitID,
TD.VisitDate,
TD.IssuedBy,
TD.VisitPurpose,
TC.CompanyName,
SP.SPname,
ROW_NUMBER() OVER(PARTITION BY TD.VisitID ORDER BY TE.AlongwithID) AS EngineerNo,
EN.[AlongwithName]
FROM
tblVisitTicket AS TD
INNER JOIN tblVisit_Alongwith AS TE
ON TD.VisitID = TE.VisitID
INNER JOIN tblAlongWith AS EN
ON TE.AlongwithID = EN.AlongwithID
INNER JOIN tblCompany AS TC
ON TD.CompanyID = TC.CompanyID
INNER JOIN tblSalePerson AS SP
ON TD.SalePersonID = SP.SalePersonID
WHERE TD.VisitStatus = 1
) AS DT
PIVOT(MAX([AlongwithName])
FOR [EngineerNo]
IN([1], [2], [3])
) AS PT
Now this is working perfectly however due to the design of my application there will be sometimes values in Person 1, 2 and 3 and sometimes there will be value in only 1 and 2 and sometimes in only 1 and sometimes there will be no value in any of them.
Now the problem is that if Person 1
, Person 2
and Person 3
has null in all of them, the whole row will be skipped by the query. Why is that? How do I display result even when there is null in them.