Click here to Skip to main content
15,886,519 members
Please Sign up or sign in to vote.
4.20/5 (2 votes)
See more: , +
This is the SQL query I am trying to execute :

SQL
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. (Using SQL SERVER 2012 if it matters)
Posted
Updated 22-Apr-14 20:49pm
v2
Comments
Herman<T>.Instance 23-Apr-14 5:51am    
tried LEFT JOIN in stead of INNER JOIN?

Change it to:
SQL
SELECT VisitID, VisitDate, IssuedBy, VisitPurpose, CompanyName, SPname as 'SalePerson', [1] as 'Person 1', [2] as 'Person 2', [3] as 'Person 3'
FROM (
SELECT VisitID, VisitDate, IssuedBy, VisitPurpose, CompanyName, SPname, [1], [2], [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
) AS MT
 
Share this answer
 
The problem is that you need to make sure that there is at least one record in your tblVisitTicket table for this query to work. If there are none the subquery you use for PIVOT source will return zero rows. The best way to fix it is to LEFT JOIN it with a fake numbers table that will contain numbers 1, 2, 3, like this:

SQL
SELECT
    VisitID,
    VisitDate,
    IssuedBy,
    VisitPurpose,
    CompanyName,
    SPname as 'SalePerson',
    [1] as 'Person 1',
    [2] as 'Person 2',
    [3] as 'Person 3'
FROM (
        SELECT
            WK.VisitID,
            WK.VisitDate,
            WK.IssuedBy,
            WK.VisitPurpose,
            WK.CompanyName,
            WK.SPname,
            N.n AS EngineerNo,
            WK.[AlongwithName]
        FROM
        (SELECT 1 AS n UNION ALL SELECT 2 AS n UNION ALL SELECT 3 AS n ) N -- this is your fake numbers table
        LEFT JOIN
        (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 ) WK ON N.n = WK.EngineerNo
    ) AS DT
    PIVOT(MAX([AlongwithName])
        FOR [EngineerNo]
        IN([1], [2], [3])
    ) AS PT
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900