Pregunta

I have three tables like enter image description here I want to display the leave types with the count. For that I have written a query like

SELECT VM.vacation_id,
       VM.vacation_desc,
       isnull(sum(VR.total_hours_applied),0) AS totalCount
FROM EMPTYPE_VACATIONCONFIG VC
LEFT JOIN HR_Vacation_Master VM ON VC.VACATIONID=VM.vacation_id
INNER JOIN HR_Employee_Vacation_Request VR ON VR.vacation_id=VM.vacation_id
WHERE VR.employee_id=156
  AND VC.BRANCHID=20
GROUP BY VM.vacation_desc,
         VM.vacation_id

my query is working fine and giving results of existed vacationids only. like enter image description here I want third leave alos with zero total. If the employee not applied any leave(in second table), that record not coming in list. I want that record also with the totalCount zero. How can I do it

¿Fue útil?

Solución

This is because of VR.employee_id=156 you are not allowing null row.

You can do that :

SELECT VM.vacation_id,
       VM.vacation_desc,
       isnull(sum(VR.total_hours_applied),0) AS totalCount
FROM EMPTYPE_VACATIONCONFIG VC
LEFT JOIN HR_Vacation_Master VM ON VC.VACATIONID=VM.vacation_id
LEFT JOIN HR_Employee_Vacation_Request VR 
    ON VR.vacation_id=VM.vacation_id AND VR.employee_id=156
WHERE VC.BRANCHID=20
GROUP BY VM.vacation_desc,
         VM.vacation_id

Leave me a comment if this not works, I have some other ideas.

Otros consejos

If one employee didn't apply any leave, there shouldn't have a record with his(or her) employee id in table HR_Employee_Vacation_Request , right? So I think you should use HR_Vacation_Master left outer join table HR_Employee_Vacation_Request .

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top