SELECT a.surveyid,* FROM [360HRSurvey] inner join ( SELECT a.EmpCode,a.surveyid FROM surveyusers a where a.surveyid = 80 and a.EmpCode NOT IN (SELECT p.EmpID FROM empsurveyselection p WHERE p.surveyid =80)) a on a.EmpCode=sempid where empid = ( SELECT empid from [360HRSurveyEmployee] where surveyid = 80)
Comentarios
El motivo del error se explicó en Respuesta de Jaimin Soni .
Ya que desea incluir surveyid
en el resultado y este no es un atributo del [360HRSurvey]
, puede usar SELECT 80 AS surveyid, * ...
para esta consulta en particular. Si el surveyid
no Sin embargo, corregido, en una consulta más compleja, puede unirse a la tabla que tiene este atributo. Suponiendo que esta tabla es surveyusers
y que hay un FOREIGN KEY
de [360HRSurvey] (sempid)
que REFERENCES surveyusers(EmpCode)
, la consulta se puede reescribir:
SELECT a.surveyid, hrs.* FROM [360HRSurvey] AS hrs JOIN surveyusers AS a ON a.EmpCode = hrs.sempid WHERE a.surveyid = 80 -- this condition can be altered or removed AND a.EmpCode NOT IN ( SELECT p.EmpID FROM empsurveyselection p WHERE p.surveyid = a.surveyid ) AND a.empid = ( -- unclear if it"s a.empid or hrs.empid SELECT hrsemp.empid FROM [360HRSurveyEmployee] AS hrsemp WHERE hrsemp.surveyid = a.surveyid ) ;
SELECT su.surveyid,b.* FROM surveyusers SU RIGHT JOIN (SELECT * FROM [360HRSurvey] where sempid NOT IN ( SELECT a.EmpCode FROM surveyusers a where a.EmpCode IN (SELECT p.EmpID FROM empsurveyselection p where deptid = 9 ) and empid IN ( SELECT empid from [360HRSurveyEmployee] ))) B ON su.EmpCode = b.sempid Order by surveyid desc
Arreglado myslelf
Comentarios