在这里,我需要数据' Eac_Card_Number
'来自另一张表' Acc_Emp_AccessCard acc
'
其中emp.emp_id = acc.Eac_EmpId
和max(acc.Eac_startDate)
。
如何在以下查询中集成此条件:
Select CASE
WHEN gen.Category_Name = 'Male' THEN 'Mr'
WHEN gen.Category_Name = 'Female' THEN 'Ms'
END as Emp_Salutation
,l.Login_Id as LgInfo_Nt_Login_Id
,org.Category_Name as emp_Organisation
,gen.Category_Name as emp_gender_id
,emp.Emp_category
,emp.emp_id
from prj_employee_detail emp
left join prj_category gen on emp.emp_gender_id = gen.Category_Id
left join prj_category org on emp.emp_Organisation = org.Category_Id
left join Prj_Login_Info l on emp.emp_id=l.LgInfo_Resource_Id
where l.lginfo_client_id = 0 and emp.emp_number not in ('0','')
答案 0 :(得分:2)
您可以使用这样的派生表加入
Select CASE
WHEN gen.Category_Name = 'Male' THEN 'Mr'
WHEN gen.Category_Name = 'Female' THEN 'Ms'
END as Emp_Salutation
,l.Login_Id as LgInfo_Nt_Login_Id
,org.Category_Name as emp_Organisation
,gen.Category_Name as emp_gender_id
,emp.Emp_category
,emp.emp_id
,acc.Eac_Card_Number
from prj_employee_detail emp
left join prj_category gen on emp.emp_gender_id = gen.Category_Id
left join prj_category org on emp.emp_Organisation = org.Category_Id
left join Prj_Login_Info l on emp.emp_id=l.LgInfo_Resource_Id
left outer join (select Row_Number() over(partition by Eac_EmpId order by Eac_startDate desc) rn, Eac_EmpId,Eac_Card_Number from Acc_Emp_AccessCard ) acc on acc.EAC_EmpID = emp.emp_id and acc.rn=1
where l.lginfo_client_id = 0 and emp.emp_number not in ('0','')
答案 1 :(得分:0)
您是否在select语句中尝试了子查询?那是我的第一个猜测:
Select CASE
WHEN gen.Category_Name = 'Male' THEN 'Mr'
WHEN gen.Category_Name = 'Female' THEN 'Ms'
END as Emp_Salutation
,l.Login_Id as LgInfo_Nt_Login_Id
,org.Category_Name as emp_Organisation
,gen.Category_Name as emp_gender_id
,emp.Emp_category
,emp.emp_id
,(select Eac_Card_Number from Acc_Emp_AccessCard acc
where emp.emp_id = acc.Eac_EmpId and max(acc.Eac_startDate)) as card_number
from prj_employee_detail emp
left join prj_category gen on emp.emp_gender_id = gen.Category_Id
left join prj_category org on emp.emp_Organisation = org.Category_Id
left join Prj_Login_Info l on emp.emp_id=l.LgInfo_Resource_Id
where l.lginfo_client_id = 0 and emp.emp_number not in ('0','')