Проверьте это:
Схема (MySQL v5.7)
CREATE TABLE t (
`id` INTEGER,
`employee_id` INTEGER,
`status` VARCHAR(12)
);
INSERT INTO t
(`id`, `employee_id`, `status`)
VALUES
('1', '1000', 'failed'),
('2', '1001', 'failed'),
('3', '1002', 'failed'),
('4', '1005', 'failed'),
('5', '1006', 'failed'),
('6', '1005', 'pending<br/>'),
('7', '1004', 'pending<br/>'),
('8', '1001', 'pending<br/>'),
('9', '1002', 'pending<br/>'),
('10', '1006', 'pending<br/>');
Запрос # 1
select id, null x from t where status = 'failed'
union all
select t.id, t2.id from t
left join t t2
on t.employee_id = t2.employee_id and t2.status = 'failed'
where t.status != 'failed';
| id | x |
| --- | --- |
| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 8 | 2 |
| 9 | 3 |
| 6 | 4 |
| 10 | 5 |
| 7 | |
Показать на БД Fiddle