I have 2 tables employeeAccount and employeeInfo. I am not allowed to modify the tables. Using Sybase databse.
employeeAccount
=============================
employeeID parentID
S1 P1
S2 P1
S3 P1
S4 P1
S5 P1
S6 P3
S6 P4
S7 P4
S8 P4
Second table: employeeInfo
==============================
employeeID Group Status
S1 0
S2 0 OPEN
S3 0
S4 1
S5 1
S6 0
S7 0
S8 1
I want to select an employee whose status is open and all its siblings which have same group.
Example- S2 status is open and S2 siblings are S1,S3,S4,S5. I want the data for S1, S2 and S3 only, because all are from same group No 0 and have same ParentID P1 .
In my approach first I find the employee for OPEN status and then find its all the siblings. Then I select the only siblings which have same group.
select DISTINCT emp.employeeID, emp.parentID from employeeAccount emp
JOIN (
SELECT DISTINCT e.employeeID, e.parentID, info.Group FROM employeeAccount e, employeeInfo info
WHERE e.employeeID = info.employeeID
AND info.status = 'OPEN'
) Details
ON Details.parentID = emp.parentID
WHERE Details.Group = (SELECT Group from employeeInfo where employeeID = emp.employeeID)
With this query I fetched S2 and his parent P1 because S2 status is OPEN then I fetched all the employees under parent P1 and then I select S1, S2 and S3 because they are from same group 0.
Please let me know if my approach is wrong or if any better approach is there.