Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 使用多重联接从另一个表获取数据_Sql Server - Fatal编程技术网

Sql server 使用多重联接从另一个表获取数据

Sql server 使用多重联接从另一个表获取数据,sql-server,Sql Server,这里,我需要另一个表中的数据“Eac\u卡号””Acc\u Emp\u AccessCard Acc 其中emp.emp\u id=acc.Eac\u EmpId和max(acc.Eac\u startDate) 如何在以下查询中集成此条件: Select CASE WHEN gen.Category_Name = 'Male' THEN 'Mr' WHEN gen.Category_Name = 'Female' THEN 'Ms'

这里,我需要另一个表中的数据“
Eac\u卡号”
Acc\u Emp\u AccessCard Acc
其中
emp.emp\u id=acc.Eac\u EmpId
max(acc.Eac\u 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','')

您可以像这样使用驱动表进行连接

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','')

是否在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','')

如何选择acc.Eac\U卡_Number@CodeManiacAcc\U emp\U AccessCard中的每个emp的卡号都是唯一的吗?@CodeManiac如果是,您可以将其添加到group by and select子句中,或者在select子句中使用Max(EAX\U卡号)。卡号不是唯一的。我必须选择具有特定emp的最大eac\U开始日期的eac\U卡号_id@CodeManiac我很高兴这有帮助:)