     select distinct d_combo as rd_combo, df_id, bn_id from r_existing 
   ) r using(rd_combo, df_id, bn_id)
 union -- get existing patterns
-  select qi_combo, ri_combo, qd_combo, rd_combo, df_code, df_id, dfg_id, bn_code, bn_id, quant_unit, quant_unit_id
+  select qi_combo, ri_combo, qd_combo, rd_combo, df_code, df_id, dfg_id, bn_code, bn_id, quant_unit, quant_unit_id, 0 as new_rec
   from x_pattern where df_code is not null and bn_code is not null
 )
 left join x_unit on unit_code=quant_unit and unit_id=quant_unit_id

  null as df_code, null as df_id, null as dfg_id, null as bn_code, null as bn_id, null as mf_code, null as mf_id, 
  quant_unit, quant_unit_id,
  case -- if the translation keeps the favorite quant_unit_id give it a better prec 
    when precedence is null then 19-- solid drug
    when precedence=1 then 19 -- quant_unit and quant_unit_id match according to prec
  else 20 end as prec
from extension_combo
left join x_unit on unit_code=quant_unit and unit_id=quant_unit_id
left join x_pattern using(qi_combo, ri_combo, qd_combo, rd_combo, quant_unit, quant_unit_id)

  null as bn_code, null as bn_id,
  null as mf_code, null as mf_id,
  null as quant_unit, null as quant_unit_id,
  31 as prec
from qr_ing
where qi_code not in (select qi_combo from x_pattern)
;
commit;

-- 32. and 33. All untranslatable are going to be added from extension_combo to x_pattern

-- Create individual translation tables all starting with x_, with one best record for each q *
-- Strategy: First use what's found in the patterns, then use precedences from relationship_to_concept and div between uds
