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.
| 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 problemin my main table i have 100 employee attendance detailcan we write a query which will find absent for all the employees in one queryfor example main table has following fieldsempno entrydate timein timeout1 1/1/2010 8 51 2/1/2010 8 51 3/1/2010 8 51 4/1/20105 8 52 1/1/2010 8 52 2/1/2010 8 52 3/1/2010 8 52 4/1/20105 8 53 1/1/2010 8 53 2/1/2010 8 53 3/1/2010 8 53 4/1/20105 8 5I 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 MVPhttp://visakhm.blogspot.com/ |
 |
|
|
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 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-11-21 : 12:51:19
|
ok. then do like belowSELECT p.empno,p.[Date]FROM(SELECT t.empno,f.[Date]FROM dbo.CalendarTable('20100101','20100131',1)fCROSS JOIN (SELECT DISTINCT empno FROM YourTable) t)pLEFT JOIN YourTable qON q.entrydate = p.[Date]AND q.empno = p.empnoWHERE q.empno IS NULLthe code for function CalendarDate can be found belowhttp://visakhm.blogspot.com/2010/02/generating-calendar-table.html------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
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. |
 |
|
|
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 MVPhttp://visakhm.blogspot.com/ |
 |
|
|
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. |
 |
|
|
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 MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|