sql - Select Max Value from Group -


i've been stumped @ trying crack problem. i'm trying find employee has sick leave each department. if the're still current employee have no end date. i've tried tweaking every way can keep getting multiple results. want employee has hours within each department.

select h.departmentid, max(e.sickleavehours) maxsick, e.employeeid hr.department d,      hr.employeedepartmenthistory h,      hr.employee e e.employeeid = h.employeeid , d.departmentid = h.departmentid , h.enddate null group h.departmentid, e.employeeid  departmentid   maxhours        employeeid 7              61              8 1              22              9 7              64              10 1              23              11 1              20              12 

select temp.departmentid, temp.sickleavehours maxsick, eh.employeeid (select h.departmentid departmentid, max(e.sickleavehours) sickleavehours hr.department d, hr.employeedepartmenthistory h, hr.employee e e.employeeid = h.employeeid , d.departmentid = h.departmentid , h.enddate null group h.departmentid) temp, hr.employeedepartmenthistory eh, employee emp eh.departmentid = temp.departmentid , emp.sickleavehours = temp.sickleavehours , emp.employeeid = eh.employeeid 

the first step of solution find maximum sick leaves per department. answered inner query above.

the next step find employees department matching maximum number of hours, answered joins in outer query.

if more 1 employees have same maximum sick leave hours, query return such employees department.

don't know if relevant here or not, looks me sick leave hours somehow cumulative hours employee irrespective of department he/she has worked in since sick leave hours "attribute" of "employee" , not "employeedepartmenthistory". so, sick leave hours may been consumed/earned in previous department, appear in current department (where enddate null in employeedepartmenthistory) query.


Comments

Popular posts from this blog

javascript - jquery or ashx not working -

opencv - DataType<cv::detail::deriv_type>::depth what is it used for -

python 3.x - Mapping specific letters onto a list of words -