I have a table "company" with data like follows containing employees and their companies:
EmployeeID EmployeeName CompanyName CompanyID ParentCoID
------------------------------------------------------------------------------------------------
100 John A 500 NULL
100 John A.1 600 500
250 Paul B 800 NULL
250 Paul ABC 2000 NULL
350 Joe D 1000 5000
600 Tom E 700 NULL
600 Tom E.2 1500 700
I am trying to display rows where if the Parent Co ID value is in the Company ID, then show both rows for that employee. So basically select employees that are in both the parent company and the sub company and show both of those rows.
I've tried
SELECT *
FROM company
WHERE CompanyID IN (SELECT ParentCoID FROM company)
but this only returns the row where the CompanyID value matches and I'm not sure how to also include that second row also.
My desired output for the sample above would be:
EmployeeID EmployeeName CompanyName CompanyID ParentCoID
------------------------------------------------------------------------------------------------
100 John A 500 NULL
100 John A.1 600 500
600 Tom E 700 NULL
600 Tom E.2 1500 700
As from the result above, Company A.1 is a sub company of A, and same with company E and E.2. I am trying to select employees that are in both the main company and sub company and therefore need to refer to the ParentCoID and the CompanyID columns.