sql - Count of dates over a period of time -
i have table1 employee data below
| empid | department | startdate | enddate | | 1 | d1 | 2014-02-05 | 2014-06-18 | | 1 | d3 | 2013-08-29 | 2014-02-05 | | 2 | d3 | 2014-05-07 | 2014-06-18 | | 2 | d4 | 2013-08-29 | 2014-05-06 | | 2 | d3 | 2014-06-19 | 2014-12-01 |
i have table table2 absence data
empid department absentcedate 1 d3 2013-09-24 1 d3 2013-09-30 1 d3 2013-10-25 1 d1 2014-02-06
1 d1 2014-02-08 2 d3 2013-08-30 2 d3 2013-09-30 2 d3 2013-10-30 2 d3 2014-11-11 2 d4 2014-05-10
i joined both tables find count of absence dates employee based on empid using teh following code:
select t1.empid empid, t1.department department, count(absencedate) numdaysabsent , startdate, enddate, table1 t1 join table2 t2 se on sa.empid = se.empid group t1.empid , t1.department startdate, enddate
my results below
empid department numdaysabsent startdat enddate 1 d1 5 2014-02-05 2014-06-18 1 d3 5 2013-08-29 2014-02-05 2 d3 5 2014-05-07 2014-06-18 2 d3 5 2014-06-19 2014-12-01 2 d4 5 2013-08-29 2014-05-06
instead want group numberofabsences department , diaply them as
empid department numdaysabsent startdate enddate 1 d1 2 2014-02-05 2014-06-18 1 d3 3 2013-08-29 2014-02-05 2 d3 3 2014-05-07 2014-06-18 2 d3 1 2014-06-19 2014-12-01 2 d4 1 2013-08-29 2014-05-06
how proceed? i've been trying figure out how achieve that. how group them startdate , enddate , excatly dipaly number of absences period in department? inputs? trying in sql server
i think need more conditions on join, including department , date. best guess this:
select t1.empid empid, t1.department department, count(absencedate) numdaysabsent , startdate, enddate, table1 t1 join table2 t2 on t1.empid = t2.empid , t1.department = t2.department , t2.absencedate between t1.startdate , t1.enddate group t1.empid, t1.department, startdate, enddate;
Comments
Post a Comment