Please start any new threads on our new site at We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

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

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

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

Very Important crosS Applying yaK Herder

52326 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
           FROM Emp_Attendance ea
           JOIN Employee e
           ON e.Emp_ID = ea.Emp_ID
           GROUP BY Office_ID,[Date]
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

SQL Server MVP

Go to Top of Page
  Previous Topic Topic Next 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.57 seconds. Powered By: Snitz Forums 2000