For your first problem, the following query works (unfortunately you do not have too many examples in your database, so it's hard to check the correctness). I'm pretty sure there might be a better solution.
SELECT D.PROVIDER_ID
FROM (
SELECT C.PROVIDER_ID, C.PATIENT_ID
FROM (...