| ID | Status |
|---|---|
| 1 | Active |
| 1 | Inactive |
| 2 | Active |
| 3 | Inactive |
| 4 | Active |
| 4 | Inactive |
In the above table when id has multiple status, need only the records with status as Active along with the remaining records.
| ID | Status |
|---|---|
| 1 | Active |
| 2 | Active |
| 3 | Inactive |
| 4 | Active |
| ID | Status |
|---|---|
| 1 | Active |
| 1 | Inactive |
| 2 | Active |
| 3 | Inactive |
| 4 | Active |
| 4 | Inactive |
In the above table when id has multiple status, need only the records with status as Active along with the remaining records.
| ID | Status |
|---|---|
| 1 | Active |
| 2 | Active |
| 3 | Inactive |
| 4 | Active |
You can also try, With an INDEX on [ID] and [Status] it would be quite fast
SELECT [ID],[Status] FROM tabl1 WHERE [Status] = 'Active' UNION ALL SELECT [ID],[Status] FROM tabl1 t1 WHERE NOT EXISTS(SELECT 1 FROM tabl1 WHERE [Status] = 'Active' AND [ID] = t1.[ID])ID | Status -: | :------- 1 | Active 2 | Active 4 | Active 3 | Inactive
db<>fiddle here