Skip to content
Advertisement

How to expand categories by id in SQL

I have the following table:

I would like to have for each org_id, person_id, month a line for each category of consultation_mode and professional_domain, as per the below:

There are 3 consultation_mode: in person, telephone, home visit & 3 professional_domain: doctor, nurse, other. How can I do this in SQL?

Advertisement

Answer

First, you will need a small table or CTE containing the full set of consultation_mode + professional_domain combinations, then you will use a CROSS JOIN (i.e. a Cartesian product) between this table and the unique set of (org_id, person_id, month), then you will LEFT JOIN your starting table to that.

Something like this:

table raw_data:

table mode_domain:

query:

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement