sql - Best way to 'flatten out' tables with relationship information? -


i have 1 table, relationships, structured

relationships  ------ relationship employer_id employee_id 10000        10020       10021 10001        10019       10020 10002        10021       10018 

where relationship between employer , employee uniquely identified relationship column. employee can have multiple employers, , employer can have multiple employees. each person has own unique id -- can appear multiple times in both employer , employee id fields.

and table, review, structured so.

meetings review  ------ meeting   attendee_id 10000     10020      10000     10019 10001     10018 10001     10021      

this table shows list of meetings , attendees. 2 people can attend meeting -- each meeting attendee, meeting, represented once in attendees field. ids in attendee field can joined ids in employer/employee field in relationships table.

i'd result set following -- each row unique meeting, , have identified relationship between 2 meeting attendees using 2 separate columns (employer_id , employee_id).

results ----- meeting employer_id employee_id  10000   10019       10020 10001   10021       10018 

my question -- conceptually, how go doing this?

my initial thought should use maximum , minimum on attendee_id, , make 2 derived tables -- 1 maximum attendee id (attendee_1) matches matches on employer id , minimum id (attendee_2) matches on employee id. then, use union query add result set derived table maximum attendee id (attendee_1) matches on employee id , minimum id (attendee)2) matches on employer id.

this appear give me results expect, there better way this?

if have 2 element each meeting think best solution second alias

meetings review  ------ meeting   attendee_id 10000     10020      10000     10019 10001     10018 10001     10021    

you can obtain result on single rows way

select a.meeting , a.employer_id b.employer_id  review   inner join review b  on a.meeting = b.meeting  , a.employer_id != b.employer_id 

Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -