我尝试以更优化的方式转换下面的SQL,但它给出错误“操作数类型冲突:uniqueidentifier与int不兼容”。你能帮帮我吗
select E.EHOId, E.ReferenceId 'ReferenceNo',
(select Comp_Name from FoodAlertCRM.dbo.vCompanyPE where Comp_CompanyId = E.GroupId) 'Group',
(select Comp_Name from FoodAlertCRM.dbo.vCompanyPE where Comp_CompanyId = E.SiteId) 'Site',
E.VisitDate as 'VisitDate',
(select Name from EHO_Dropdowns where ID = E.FollowupAction) 'FollowupAction',
(select Name from EHO_LocalAuthority where ID = E.LocalAuthority) 'LocalAuthority',
(select Name from EHO_Dropdowns where ID = E.[Status]) 'Status',
(select Name from EHO_Dropdowns where ID = E.Position) 'Position',
(select Name from EHO_Dropdowns where ID = E.Structural) 'Structural',
(select Name from EHO_Dropdowns where ID = E.ConfidenceinManagement) 'ConfideninManagement',
(select Name from EHO_Dropdowns where ID = E.HygieneandSafety) 'HygieneandSafety',
(select Name from EHO_Dropdowns where ID = E.RoutineInspection) 'RoutineInspection',
(select Name from EHO_Dropdowns where ID = E.OutcomeofVisit) 'OutcomeofVisit',
e.OfficerName,e.FoodHygieneRating,e.ManageronDuty,e.Comments,
CASE E.AnnouncedVisit
When 0 Then 'No'
When 1 Then 'Yes'
Else ''
ENd as 'AnnouncedVisit',
(select u.Username from FoodAlertCRM.dbo.CDB_User u where u.UserId = e.CreatedBy ) 'CreatedBy',
(select COUNT(EHOId) from EHO_Attachments where EHOId = E.EHOId) as 'AttachmentCount'
from EHO_Log E
where E.IsActive = 1 AND e.IsDeleted = 0
Order by E.VisitDate desc
你看上面有很多内部查询,如果有数千条记录,性能将非常糟糕。所以我试图摆脱内心的疑问。
select E.EHOId, E.ReferenceId 'ReferenceNo',
(select Comp_Name from FoodAlertCRM.dbo.vCompanyPE where Comp_CompanyId = E.GroupId) 'Group',
(select Comp_Name from FoodAlertCRM.dbo.vCompanyPE where Comp_CompanyId = E.SiteId) 'Site',
E.VisitDate as 'VisitDate',
F.Name 'FollowupAction',L.Name 'LocalAuthority', St.Name 'Status',
P.Name 'Position', S.Name 'Structural', C.Name 'ConfideninManagement',
H.Name 'HygieneandSafety', R.Name 'RoutineInspection', O.Name 'OutcomeofVisit',
e.OfficerName,e.FoodHygieneRating,e.ManageronDuty,e.Comments,
CASE E.AnnouncedVisit
When 0 Then 'No'
When 1 Then 'Yes'
Else ''
ENd as 'AnnouncedVisit',
(select u.Username from FoodAlertCRM.dbo.CDB_User u where u.UserId = e.CreatedBy ) 'CreatedBy',
(select COUNT(EHOId) from EHO_Attachments where EHOId = E.EHOId) as 'AttachmentCount'
from EHO_Log E
Left Outer Join EHO_Dropdowns St ON St.Id = E.[Status]
Left Outer Join EHO_Dropdowns F ON F.Id = E.FollowupAction
Left Outer Join EHO_Dropdowns L ON L.Id = E.LocalAuthority
Left Outer Join EHO_Dropdowns P ON P.Id = E.Position
Left Outer Join EHO_Dropdowns S ON S.Id = E.Structural
Left Outer Join EHO_Dropdowns C ON C.Id = E.ConfidenceinManagement
Left Outer Join EHO_Dropdowns H ON H.Id = E.HygieneandSafety
Left Outer Join EHO_Dropdowns R ON R.Id = E.RoutineInspection
Left Outer Join EHO_Dropdowns O ON O.Id = E.OutcomeofVisit
where E.IsActive = 1 AND e.IsDeleted = 0
Order by E.VisitDate desc
上面的代码失败,我认为不允许多个左外连接,我将其转换为下面的查询,但仍然失败
select E.EHOId, E.ReferenceId 'ReferenceNo',
(select Comp_Name from FoodAlertCRM.dbo.vCompanyPE where Comp_CompanyId = E.GroupId) 'Group',
(select Comp_Name from FoodAlertCRM.dbo.vCompanyPE where Comp_CompanyId = E.SiteId) 'Site',
E.VisitDate as 'VisitDate',
D.Name 'FollowupAction',D.Name 'LocalAuthority', D.Name 'Status',
D.Name 'Position', D.Name 'Structural', D.Name 'ConfideninManagement',
D.Name 'HygieneandSafety', D.Name 'RoutineInspection', D.Name 'OutcomeofVisit',
e.OfficerName,e.FoodHygieneRating,e.ManageronDuty,e.Comments,
CASE E.AnnouncedVisit
When 0 Then 'No'
When 1 Then 'Yes'
Else ''
ENd as 'AnnouncedVisit',
(select u.Username from FoodAlertCRM.dbo.CDB_User u where u.UserId = e.CreatedBy ) 'CreatedBy',
(select COUNT(EHOId) from EHO_Attachments where EHOId = E.EHOId) as 'AttachmentCount'
from EHO_Log E
Left Outer Join EHO_Dropdowns D ON D.Id = E.[Status] AND D.Id = E.FollowupAction
AND D.Id = E.LocalAuthority AND D.Id = E.Position AND D.Id = E.Structural
AND D.Id = E.ConfidenceinManagement AND D.Id = E.HygieneandSafety
AND D.Id = E.RoutineInspection AND D.Id = E.OutcomeofVisit
where E.IsActive = 1 AND e.IsDeleted = 0
Order by E.VisitDate desc
任何想法的人?两个新查询都返回错误“操作数类型冲突:uniqueidentifier与int不兼容”。
答案 0 :(得分:1)
我认为原因可能是这一行:
Left Outer Join EHO_Dropdowns L ON L.Id = E.LocalAuthority
尝试将其更改为:
Left Outer Join EHO_LocalAuthority L ON L.Id = E.LocalAuthority
答案 1 :(得分:0)
同一桌上的多个外部加入:
解决方案:
请在外连接字段
上使用nvlNVL(EHO_Dropdowns(+),EHO_LocalAuthority(+)) = E.LocalAuthority