Question

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.

Was it helpful?

Solution

As far as I could understand, you have made inner join between tblVisitTicket and tblVisit_AlongWith tables. While there is possibility that tblvisit_Alongwith has no data for specific visit.

If I am right, then you problem can be solved by using outer join in these tables, so that all data of tblVisitTicket will be available and matching data of tblVisit_AlongWith will be there.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top