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
 Finding missing dates from the table for all emp

Author  Topic 

pnasz
Posting Yak Master

101 Posts

Posted - 2010-11-21 : 12:37:41
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

52326 Posts

Posted - 2010-11-21 : 12:45:41
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
Posting Yak Master

101 Posts

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

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-11-21 : 12:51:19
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
Posting Yak Master

101 Posts

Posted - 2010-11-21 : 12:57:23
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

52326 Posts

Posted - 2010-11-21 : 12:59:27
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
Posting Yak Master

101 Posts

Posted - 2010-11-24 : 01:53:49
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

52326 Posts

Posted - 2010-11-24 : 09:19:10
which records are duplicating? can you show some sample?

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

Go to Top of Page
   

- Advertisement -