I have a simple SQL query to count all Employees in a Department(within children) like:
With Temp(id) AS
(
Select d.id From DEPARTMENT d
Where d.id = 1
UNION ALL
Select d.id From DEPARTMENT d JOIN Temp te ON d.idDepartment = te.id
)
Select count(*) From
(
Select e.id From Employee e Join Temp te On e.idDepartment = te.id
)
But i give a error "StackOverflow", I dont know where is mistake, can you help me? there are some data for test case: Table Department :
ID----------departmentName-----------idDepartment(id parent)
1 A 0
2 B 1
Table Employee :
id----------employeeName------------idDepartment
1 E_1 1
2 E_2 1
3 E_3 2
So when I select quantity of Eployee in a Department(A)--> result : 3, if Department B --> result: 1 Thanks!