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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 SQL query from multiple table

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

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 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]
[/code]

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
   

- Advertisement -