Please start any new threads on our new 
    site at https://forums.sqlteam.com.  We've got lots of great SQL Server
    experts to answer whatever question you can come up with. 
    
        
            
                
                    
                        
                            
                                | Author | 
                                
                                 Topic  | 
                             
                            
                                    | 
                                         niralas 
                                        Starting Member 
                                         
                                        
                                        2 Posts  | 
                                        
                                        
                                            
                                            
                                             Posted - 2013-02-19 : 10:44:40
                                            
  | 
                                             
                                            
                                            | I have to write a query to count attendance for office location and print office is close or Openhaving 3 tablesEmployee, Office_Location, Emp_AttendanceEmployee (Emp_ID, EMP_Name, Office_ID)Office_Location (Office_ID, Office_Address)Emp_Attendance (Date, Emp_ID, Status, Remarks)Now I have to say Office is close if no employee present on certain date | 
                                             
                                         
                                     | 
                             
       
                            
                       
                          
                            
                                    | 
                                     visakh16 
                                    Very Important crosS Applying yaK Herder 
                                     
                                    
                                    52326 Posts  | 
                                    
                                      
                                        
                                          
                                           
                                            Posted - 2013-02-19 : 11:57:58
                                          
  | 
                                         
                                        
                                          | [code]SELECT ol.*,f.[Date],CASE WHEN COALESCE(tmp.Cnt,0) = 0 THEN 'Closed' ELSE 'Open' END AS OfficeStatusFROM Office_Location olCROSS JOIN dbo.CalendarTable('20000101','20151231',0,0) fLEFT JOIN (SELECT Office_ID,[Date],COUNT(EmpID) AS Cnt           FROM Emp_Attendance ea           JOIN Employee e           ON e.Emp_ID = ea.Emp_ID           GROUP BY Office_ID,[Date]          )tmpON tmp.Office_ID = ol.Office_IDAND tmp.[Date] = f.[Date][/code]dbo.CalendarTable can be found in below linkI've consider only period from 20000101 to 20151231 so if you need bigger period please substitute required values inside the functionhttp://visakhm.blogspot.in/2010/02/generating-calendar-table.html------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/  | 
                                         
                                        
                                            | 
                                         
                                       
                                     | 
                                   
                            
                            
                                | 
                                    
                                      
                                     
                                    
                                 | 
                             
                         
                     | 
                 
             
         |   
     
     
            
              
	     |  
		
			
     
          
		 |