SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 SQL query from multiple table
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

niralas
Starting Member

2 Posts

Posted - 02/19/2013 :  10:44:40  Show Profile  Reply with Quote
I have to write a query to count attendance for office location and print office is close or Open

having 3 tables

Employee, Office_Location, Emp_Attendance

Employee (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

India
52325 Posts

Posted - 02/19/2013 :  11:57:58  Show Profile  Reply with Quote

SELECT ol.*,f.[Date],
CASE WHEN COALESCE(tmp.Cnt,0) = 0 THEN 'Closed' ELSE 'Open' END AS OfficeStatus
FROM Office_Location ol
CROSS JOIN dbo.CalendarTable('20000101','20151231',0,0) f
LEFT 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]
          )tmp
ON tmp.Office_ID = ol.Office_ID
AND tmp.[Date] = f.[Date]


dbo.CalendarTable can be found in below link

I've consider only period from 20000101 to 20151231 so if you need bigger period please substitute required values inside the function

http://visakhm.blogspot.in/2010/02/generating-calendar-table.html

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.03 seconds. Powered By: Snitz Forums 2000