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
 Finding missing dates from the table for all emp
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

pnasz
Yak Posting Veteran

97 Posts

Posted - 11/21/2010 :  12:37:41  Show Profile  Reply with Quote
I have asked question before also but its not clear please help me to solve this problem

in my main table i have 100 employee attendance detail
can we write a query which will find absent for all the employees in one query


for example main table has following fields


empno entrydate timein timeout

1 1/1/2010 8 5
1 2/1/2010 8 5
1 3/1/2010 8 5
1 4/1/20105 8 5
2 1/1/2010 8 5
2 2/1/2010 8 5
2 3/1/2010 8 5
2 4/1/20105 8 5
3 1/1/2010 8 5
3 2/1/2010 8 5
3 3/1/2010 8 5
3 4/1/20105 8 5

I don't have any other table. from this table can i find date absent for all employee in one query.
For example in the above table empno 1 is present on 1/1/2010,2/1/2010 and 3/1/2010. He is absent for all days except these three days. how to get the all absent dates for all employee.
can we find all dates absent for emp 1,2,3 in one query

visakh16
Very Important crosS Applying yaK Herder

India
52309 Posts

Posted - 11/21/2010 :  12:45:41  Show Profile  Reply with Quote
what is period for which you want to consider attendance of employees? i mean start and end date?

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

Go to Top of Page

pnasz
Yak Posting Veteran

97 Posts

Posted - 11/21/2010 :  12:46:44  Show Profile  Reply with Quote
for one month that is 1/1/2010 to 31/1/2010
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52309 Posts

Posted - 11/21/2010 :  12:51:19  Show Profile  Reply with Quote
ok. then do like below


SELECT p.empno,p.[Date]
FROM
(
SELECT t.empno,f.[Date]
FROM dbo.CalendarTable('20100101','20100131',1)f
CROSS JOIN (SELECT DISTINCT empno FROM YourTable) t
)p
LEFT JOIN YourTable q
ON q.entrydate = p.[Date]
AND q.empno = p.empno
WHERE q.empno IS NULL

the code for function CalendarDate can be found below

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

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

Go to Top of Page

pnasz
Yak Posting Veteran

97 Posts

Posted - 11/21/2010 :  12:57:23  Show Profile  Reply with Quote
Thanx a lot can you explain a bit.
This will give me all the missing date from my main table.

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52309 Posts

Posted - 11/21/2010 :  12:59:27  Show Profile  Reply with Quote
yup. for each employee it will return absent dates between the passed start and end dates. read the blog to understand how function works.

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

Go to Top of Page

pnasz
Yak Posting Veteran

97 Posts

Posted - 11/24/2010 :  01:53:49  Show Profile  Reply with Quote
the query above is working fine but what if some records are duplicating.
i have tried using distinct but its not showing the desired result.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52309 Posts

Posted - 11/24/2010 :  09:19:10  Show Profile  Reply with Quote
which records are duplicating? can you show some sample?

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