Skip to content
Advertisement

SQL: How to create a database view from tables with OneToMany Relationship?

There are three available tables and columns:

I want to create view from in following format:

  1. EmpID and EmpName comes from Core Table.
  2. ExternalID and ExternalName comes from External Table which is OneToOne with core table
  3. EmpPrimaryDept and EmpSecondaryDept comes from Position Table which is OneToMany with core table

For 1) and 2) following query works:

How to join Position table so that return comes in one row as described above?

  • There are 20,000 Employees data in core.
  • In question where you see 1 Emp ID those are linkage between two tables.
  • I am using Oracle Database 12c Release 12.1.0.1.0 – 64bit Production

Advertisement

Answer

One way is to use subqueries in the select clause:

You can also move them to the from clause:

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