Join several views at once to an explore (making code DRY)

bl5a
New Member

I have a model in which serveral explores repeat the same join logic.

Imagine I have this diagram:

table_a -> table_x
           table_x -> table_y
           table_x -> table_z

table_b -> table_x
           table_x -> table_y
           table_x -> table_z

My model looks like this:

explore: table_a {
  join: table_x {
    type: left_outer
    sql_on: ${table_a.id} = ${table_x.id} ;;
    relationship: one_to_one
  }

  join: table_y {
    type: left_outer
    sql_on: ${table_x.other_id} = ${table_y.other_id} ;;
    relationship: one_to_one
  }

  join: table_z {
    type: left_outer
    sql_on: ${table_x.yet_another_id} = ${table_z.yet_another_id} ;;
    relationship: many_to_one
  }
}

explore: table_b {
  join: table_x {
    type: left_outer
    sql_on: ${table_b.id} = ${table_x.id} ;;
    relationship: one_to_one
  }

  join: table_y {
    type: left_outer
    sql_on: ${table_x.other_id} = ${table_y.other_id} ;;
    relationship: one_to_one
  }

  join: table_z {
    type: left_outer
    sql_on: ${table_x.yet_another_id} = ${table_z.yet_another_id} ;;
    relationship: many_to_one
  }
}

You can see the code is not very DRY. How can we improve this? I don’t think I can use extends because my from table is changing (it’s table_a first, then table_b). Since the joins are all left_outer I don’t have guarantees that all the records will be in tables x, y, and z, so I can’t start from these and use an extend

0 5 723
5 REPLIES 5
Top Labels in this Space
Top Solution Authors