这是我试图执行的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
现在这完全正常工作但是由于我的应用程序的设计,人员1,2和3中有时会有值,有时只有1和2有价值,有时仅有1有时会有价值其中任何一个都有价值。
现在的问题是,如果Person 1
,Person 2
和Person 3
在所有这些中都为空,则查询会跳过整行。这是为什么?即使结果为空,我如何显示结果。
答案 0 :(得分:0)
据我所知,您已在 tblVisitTicket 和 tblVisit_AlongWith 表之间进行了内部联接。 虽然tblvisit_Alongwith可能没有特定访问的数据。
如果我是对的,那么你可以通过在这些表中使用外连接来解决问题,这样tblVisitTicket的所有数据都将可用,并且tblVisit_AlongWith的匹配数据将在那里。