从具有多个联合值的查询中获取不同的值

时间:2012-03-18 07:02:27

标签: sql-server union

实际上我需要来自多个联合语句的不同值,但我没有得到这个。

My result which i get

这里的emp_id是578周末,不在我需要的不同行中。

我的代码是这样的。

             select  [Employee Name],emp_id,holiDAY, 
              leave,absent,weekend
              from  
               (  

                SELECT *FROM    
               (              

                      select  view_emp_info.emp_fullname+' '+'('+''+CONVERT(VARCHAR(50), 


                 (view_emp_info.EMP_ID)) +''+ ')' as [Employee  

               Name],view_emp_info.emp_id,tbl_emp_forceduty.Holiday_Name as 'HOLIDAY'    
                 ,''AS LEAVE,'' AS ABSENT,'' AS [WEEKEND]     
                 from view_emp_Info inner join tbl_emp_forceduty        



                   on view_emp_info.emp_id=tbl_emp_forceduty.emp_id              
                   where           
                   ondate=@date and           
                     view_emp_info.BRANCH_NAME=@branch and view_emp_info.emp_id not in                  

                 (Select emp_id from tbl_emp_attn_log where tdate=@date)              
                 and view_emp_info.status_id=1             

                    union              
                    select view_emp_info.emp_fullname+' '+'('+              

                   ''+CONVERT(VARCHAR(50),view_emp_info.EMP_ID) +''+ ')' as [Employee  

                 Name],view_emp_info.emp_id,'' AS HOLIDAY  

               ,isnull(tbl_org_leave_log.leavetype,' ') 'LEAVE',    
              '' AS 'ABSENT',''AS 'WEEKEND'          
                 from view_emp_info inner join tbl_org_leave_log              
              on view_emp_info.emp_id=tbl_org_leave_log.emp_id              
               where leave_date=@date and view_emp_info.BRANCH_NAME=@branch               
             and  view_emp_info.emp_id not in (Select emp_id from tbl_emp_attn_log where       

            tdate=@date)              
              and tbl_org_leave_log.Taken='0.5' and view_emp_info.status_id=1               

               union         
                 select view_emp_info.emp_fullname+' '+'('+                        

             ''+CONVERT(VARCHAR(50),view_emp_info.EMP_ID) +''+ ')' as [Employee  

            Name],view_emp_info.emp_id,'' AS HOLIDAY ,'' as [LEAVE]    


                ,'' AS 'ABSENT','Weekend'AS 'WEEKEND'          
           from view_emp_info inner join tbl_org_weekend              
          on view_emp_info.emp_id=tbl_org_weekend.emp_id  or tbl_org_weekend.emp_id=0          
     where view_emp_info.BRANCH_NAME=@branch and days=datepart(weekday,@date) and 
         is_weekend=1  and  view_emp_info.emp_id not in (Select emp_id from                                    
        tbl_emp_attn_log where tdate=@date)        

         UNION       
           select view_emp_info.emp_fullname+' '+'('+    

      ''+CONVERT(VARCHAR(50),view_emp_info.EMP_ID) +''+ ')' as [Employee Name],    
         view_emp_info.emp_id,'' AS HOLIDAY,  '' AS 'LEAVE','Absent' as 'ABSENT',''AS  
        'WEEKEND'          
  from view_emp_info inner join tbl_emp_attn_log              
on view_emp_info.emp_id!=tbl_emp_attn_log.emp_id        

             where view_emp_info.BRANCH_NAME=@branch and      
          view_emp_info.emp_id not in (Select emp_id from tbl_emp_attn_log where                                 

        tdate=@date)    



           )    
         T1  where EMP_ID IN (SELECT EMP_ID FROM VIEW_EMP_INFO WHERE              

           BRANCH_NAME=@BRANCH)
            )t2

1 个答案:

答案 0 :(得分:1)

只需group by employee_name, employee_id, holiday, leave。然后对max(absent), max(weekend)

等结果应用一些聚合函数