How to merge large Group resources

Hi, I’m somewhat new to FHIR, I’m working on a project that uses SmileCDR FHIR and I don’t know how to approach this problem.

We have a multi-tenant database, each tenant would have a special Group resource that can contain large amounts of Patients, two groups can contain the same patient. I’m trying to figure out how can I merge two Group resources (without affecting the source groups) and eliminate duplicate Patients as I need to process every unique patient.

Is there a way to merge multiple Group resources into one so I can perform a bulk export on this one group instead of exporting every Group and finding distinct Patients in code? I’m basically trying to do Outer Join Group By (some demographics) before I get the list to my app.

Thanks

As far as I know, there’s no functionality at all in the FHIR Api that addresses this - you have to fetch the resources and do this yourself. It might be a custom server side operation - that would be more efficient