Author |
Topic |
vipinjha123
Starting Member
45 Posts |
Posted - 2012-06-01 : 02:25:50
|
Dear All,i want to capture only those employee who is absent for continously 3 working days.i am posting a sample atterndnce report of 1 employeewhere you will see ststus like P-presentw-weekly off(sat and sunday)H-National holidayPL-paid leaveA-absentin given post emp M0000 is absnt continous 3 working daysso this employee must come in my report outputEMPID Attendence Date StatusM0000 05/31/12 P M0000 05/30/12 P M0000 05/29/12 P M0000 05/28/12 P M0000 05/27/12 W M0000 05/26/12 W* M0000 05/26/12 W* M0000 05/25/12 P M0000 05/24/12 AM0000 05/23/12 AM0000 05/22/12 AM0000 05/21/12 AM0000 05/20/12 W M0000 05/19/12 W M0000 05/18/12 P M0000 05/17/12 P M0000 05/16/12 P M0000 05/15/12 P M0000 05/14/12 P M0000 05/13/12 W M0000 05/12/12 W M0000 05/11/12 P M0000 05/10/12 PL M0000 05/09/12 P M0000 05/08/12 P M0000 05/07/12 P M0000 05/06/12 W M0000 05/05/12 W M0000 05/04/12 P M0000 05/03/12 P M0000 05/02/12 P M0000 05/01/12 H regardsVipin jha |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2012-06-01 : 02:50:21
|
[code]select a.EmpID, fr_date = a.Attendence_Date, to_date = dateadd(day, 3, Attendence_Date)from Attendence awhere Status = 'A'and exists ( select x.EmpID from Attendence x where x.EmpID = a.EmpID and x.Attendence_Date >= a.Attendence_Date and x.Attendence_Date <= dateadd(day, 3, a.Attendence_Date) and x.Status = 'A' group by x.EmpID having count(*) = 3 )[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
vinu.vijayan
Posting Yak Master
227 Posts |
Posted - 2012-06-01 : 02:58:22
|
[quote]Originally posted by khtan
--Creating TableCreate Table Ex(EMPID varchar(10), AttendenceDate Date, Status char(2) )--Inserting Sample DataInsert Into ExSelect 'M0000', '05/31/12', 'P' Union ALLSelect 'M0000', '05/30/12', 'P'Union ALLSelect 'M0000', '05/29/12', 'P' Union ALLSelect 'M0000', '05/28/12', 'P'Union ALLSelect 'M0000', '05/27/12', 'W' Union ALLSelect 'M0000', '05/26/12', 'W*' Union ALLSelect 'M0000', '05/26/12', 'W*' Union ALLSelect 'M0000', '05/25/12', 'P' Union ALLSelect 'M0000', '05/24/12', 'A'Union ALLSelect 'M0000', '05/23/12', 'A'Union ALLSelect 'M0000', '05/22/12', 'A'Union ALLSelect 'M0000', '05/21/12', 'A'Union ALLSelect 'M0000', '05/20/12', 'W'Union ALLSelect 'M0000', '05/19/12', 'W' Union ALLSelect 'M0000', '05/18/12', 'P' Union ALLSelect 'M0000', '05/17/12', 'P' Union ALLSelect 'M0000', '05/16/12', 'P' Union ALLSelect 'M0000', '05/15/12', 'P' Union ALLSelect 'M0000', '05/14/12', 'P' Union ALLSelect 'M0000', '05/13/12', 'W' Union ALLSelect 'M0000', '05/12/12', 'W' Union ALLSelect 'M0000', '05/11/12', 'P' Union ALLSelect 'M0000', '05/10/12', 'PL' Union ALLSelect 'M0000', '05/09/12', 'P'Union ALLSelect 'M0000', '05/08/12', 'P' Union ALLSelect 'M0000', '05/07/12', 'P' Union ALLSelect 'M0000', '05/06/12', 'W' Union ALLSelect 'M0000', '05/05/12', 'W' Union ALLSelect 'M0000', '05/04/12', 'P' Union ALLSelect 'M0000', '05/03/12', 'P' Union ALLSelect 'M0000', '05/02/12', 'P' Union ALLSelect 'M0000', '05/01/12', 'H'--Query For Your Requirement;With CTEAs(Select *, ROW_NUMBER() Over (Partition By Status Order By AttendenceDate) As rn From Ex Where Status = 'A')Select a.EMPID,(Case When DateDiff(DD, a.AttendenceDate , d.AttendenceDate) >= 3 Then 'Absent For More Than Three Days' Else 'Not absent For More Than Three Days' End)As AttendenceStatusFrom CTE As aJOIN CTE As d ON a.EMPID = d.EMPID AND d.rn = (a.rn + 3) N 28° 33' 11.93148"E 77° 14' 33.66384" |
 |
|
vipinjha123
Starting Member
45 Posts |
Posted - 2012-06-01 : 03:39:18
|
thanks khtanas per your query i modified my query like belwoselect a.EMP_STAFFID ,a.trans_type, fr_date = a.ATTENDANCE_DATE, to_date = dateadd(day, 3, ATTENDANCE_DATE)from ATTENDANCE_REGISTER awhere TRANS_TYPE = 'A'and exists ( select x.EMP_STAFFID from ATTENDANCE_REGISTER x where x.EMP_STAFFID = a.EMP_STAFFID and x.ATTENDANCE_DATE >= a.ATTENDANCE_DATE and x.ATTENDANCE_DATE <= dateadd(day, 3, a.ATTENDANCE_DATE) and x.TRANS_TYPE = 'A' group by x.EMP_STAFFID having count(*) = 3 )but i ma not getting any data regards,Vipin jha |
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2012-06-01 : 03:42:55
|
what does that query return ? KH[spoiler]Time is always against us[/spoiler] |
 |
|
vipinjha123
Starting Member
45 Posts |
Posted - 2012-06-01 : 03:57:20
|
NO RECORDS COMING |
 |
|
vipinjha123
Starting Member
45 Posts |
Posted - 2012-06-01 : 05:28:31
|
Dear Venu,when i am removing trans_type='a'that time i am getting all the recird axcept absent onequote: Originally posted by vipinjha123 thanks khtanas per your query i modified my query like belwoselect a.EMP_STAFFID ,a.trans_type, fr_date = a.ATTENDANCE_DATE, to_date = dateadd(day, 3, ATTENDANCE_DATE)from ATTENDANCE_REGISTER awhere TRANS_TYPE = 'A'and exists ( select x.EMP_STAFFID from ATTENDANCE_REGISTER x where x.EMP_STAFFID = a.EMP_STAFFID and x.ATTENDANCE_DATE >= a.ATTENDANCE_DATE and x.ATTENDANCE_DATE <= dateadd(day, 3, a.ATTENDANCE_DATE) and x.TRANS_TYPE = 'A' group by x.EMP_STAFFID having count(*) = 3 )but i ma not getting any data regards,Vipin jha
|
 |
|
vinu.vijayan
Posting Yak Master
227 Posts |
Posted - 2012-06-01 : 05:54:59
|
quote: Originally posted by vipinjha123 Dear Venu,when i am removing trans_type='a'that time i am getting all the recird axcept absent onequote: Originally posted by vipinjha123 thanks khtanas per your query i modified my query like belwoselect a.EMP_STAFFID ,a.trans_type, fr_date = a.ATTENDANCE_DATE, to_date = dateadd(day, 3, ATTENDANCE_DATE)from ATTENDANCE_REGISTER awhere TRANS_TYPE = 'A'and exists ( select x.EMP_STAFFID from ATTENDANCE_REGISTER x where x.EMP_STAFFID = a.EMP_STAFFID and x.ATTENDANCE_DATE >= a.ATTENDANCE_DATE and x.ATTENDANCE_DATE <= dateadd(day, 3, a.ATTENDANCE_DATE) and x.TRANS_TYPE = 'A' group by x.EMP_STAFFID having count(*) = 3 )but i ma not getting any data regards,Vipin jha
Which method are you talkin about??.,...mine or khtan's?N 28° 33' 11.93148"E 77° 14' 33.66384" |
 |
|
SQL Padawan
Starting Member
4 Posts |
Posted - 2012-06-01 : 13:47:41
|
What I came up with, can be slow so make u test.--Creating TableDECLARE @Ex table(empid varchar(10), attendencedate Date, stat char(2) )--Inserting Sample DataInsert Into @ExSelect 'M0000', '05/31/12', 'P' Union ALLSelect 'M0000', '05/30/12', 'P'Union ALLSelect 'M0000', '05/29/12', 'P' Union ALLSelect 'M0000', '05/28/12', 'P'Union ALLSelect 'M0000', '05/27/12', 'W' Union ALLSelect 'M0000', '05/26/12', 'W*' Union ALLSelect 'M0000', '05/26/12', 'W*' Union ALLSelect 'M0000', '05/25/12', 'P' Union ALLSelect 'M0000', '05/24/12', 'A'Union ALLSelect 'M0000', '05/23/12', 'A'Union ALLSelect 'M0000', '05/22/12', 'A'Union ALLSelect 'M0000', '05/21/12', 'A'Union ALLSelect 'M0000', '05/20/12', 'W'Union ALLSelect 'M0000', '05/19/12', 'W' Union ALLSelect 'M0000', '05/18/12', 'P' Union ALLSelect 'M0000', '05/17/12', 'P' Union ALLSelect 'M0000', '05/16/12', 'P' Union ALLSelect 'M0000', '05/15/12', 'P' Union ALLSelect 'M0000', '05/14/12', 'P' Union ALLSelect 'M0000', '05/13/12', 'W' Union ALLSelect 'M0000', '05/12/12', 'W' Union ALLSelect 'M0000', '05/11/12', 'P' Union ALLSelect 'M0000', '05/10/12', 'PL' Union ALLSelect 'M0000', '05/09/12', 'P'Union ALLSelect 'M0000', '05/08/12', 'P' Union ALLSelect 'M0000', '05/07/12', 'P' Union ALLSelect 'M0000', '05/06/12', 'W' Union ALLSelect 'M0000', '05/05/12', 'W' Union ALLSelect 'M0000', '05/04/12', 'P' Union ALLSelect 'M0000', '05/03/12', 'P' Union ALLSelect 'M0000', '05/02/12', 'P' Union ALLSelect 'M0000', '05/01/12', 'H'SELECT t1.empid, fromdate = t1.attendencedate, todate = t2.attendencedateFROM @ex t1 LEFT JOIN @ex t2 ON t1.empid = t2.empid AND t1.attendencedate < t2.attendencedate AND t2.stat = 'A'WHERE t1.stat = 'A'AND NOT EXISTS( SELECT 1 FROM @ex t3 WHERE t1.empid = t2.empid AND t3.attendencedate BETWEEN t1.attendencedate AND t2.attendencedate AND t3.stat <> 'A' )AND (DATEDIFF(d,t1.attendencedate,t2.attendencedate) + 1) = 3 |
 |
|
|
|
|