You would describe the object to get its DescribeSObjectResult, then loop through each field, check its DisplayType to make sure it is a "reference", and if so, grab the relationship name and field name. See also the DescribeFieldResult class.
You could use record types to achieve this. On object ‘A’s’ open and active update the child record’s record type that has only open and active in the picklist values. Similarly when the status is Closed update the child record type that would have only Closed as an option. So you would need two different record types to accomplish this.
As mentioned in above comment, you can create many-to-many relationship through junction object. After creating all necessary objects and relationships, you can add related Patients to layout.
You can only have one related record per Lookup field. You can refer to below documentation.
This appears to be a Salesforce bug. What appears to be causing the issue is having an ORDER BY inside one of the sub-queries.
As a workaround to the bug you can remove the inner ORDER BY and that will allow you to query for all fields and retrieve the data correctly.
To make up for the fact that your query won't be correctly sorted anymore, you can ...