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
 Script Library
 Need Query Help
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

niralas
Starting Member

2 Posts

Posted - 02/19/2013 :  10:46:28  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

James K
Flowing Fount of Yak Knowledge

3744 Posts

Posted - 02/19/2013 :  12:13:26  Show Profile  Reply with Quote
If you don't already have a calendar table in your database, create one with the dates of interest like this:
Create a calendar table first, if you don't have one already like this:

CREATE TABLE #Calendar(Dt DATETIME NOT NULL PRIMARY KEY);
;WITH cte AS
(
	SELECT CAST('20130101' AS DATETIME) AS c 
	UNION ALL
	SELECT DATEADD(dd,1,c) FROM cte 
	WHERE c < '20131231'
)
INSERT INTO #Calendar SELECT c FROM cte OPTION  (MAXRECURSION 0);
Then query against that table. For example:
SELECT
	c.Dt,
	o.Office_ID
FROM
	#Calendar c
	CROSS JOIN Office_Location o 
WHERE NOT EXISTS
(
	SELECT
		*
	FROM
		Emp_Attendance ea
		INNER JOIN Employee e ON e.Emp_ID = ea.EmpID
	WHERE 
		o.Office_ID = e.Office_ID
		AND ea.Date = c.Dt
);
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 02/20/2013 :  01:21:01  Show Profile  Reply with Quote
duplicate of

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=183103

------------------------------------------------------------------------------------------------------
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.08 seconds. Powered By: Snitz Forums 2000