← Back

Post Thread

Many-to-many (M2M) relationship support in #Snowflake Semantic Views is now in public preview, supporting both bridge tables and direct many-to-many relationships.

Count physicians once per health system, even when they have multiple affiliations.

Question: How many physicians are affiliated with each health system?
Synthetic input: Physician1 is affiliated with institutions I1 and I2; Physician2 with I1. Both institutions belong to North.


CREATE OR REPLACE
  SEMANTIC VIEW physician_network_sv
  TABLES (
    physicians AS physicians
      PRIMARY KEY (physician_id),
    institutions AS institutions
      PRIMARY KEY (institution_id),
    affiliations AS affiliations
      PRIMARY KEY (physician_id, institution_id)    
  )
  RELATIONSHIPS (
    affiliations(physician_id)
      references physicians(physician_id)
    , affiliations(institution_id)
      references institutions(institution_id)
  )
  DIMENSIONS (
    institutions.region AS region
 )

  metrics (
    physicians.physician_count
      as count(physician_id)
    , institutions.institution_count
      as count(institution_id)
  )


;


select region
  , agg(physician_count) -- returns 2
  , agg(institution_count) -- returns 2
from physician_network_sv
group by all;
0 Replies
0 Reposts